<?php

declare(strict_types=1);

namespace prj\ex\logic\app;

use \tts\safer_io as SafeIO;

class home_logic {
    
    public static function name_demo(array & $input): void {
        $model = new \prj\ex\models\app\home_model(\tts\registry::get('db')); 
        
        $submitted = true;
        foreach(SafeIO::logic_sanitize($input) as $data) {
            if (SafeIO::required_fields_were_NOT_all_submitted($data)) {
                $submitted = false;
                break;
            }
        }
        
        $input['submitted'] = $submitted;
        
        if ($submitted) {
            $model->init_name_demo_table(); // Create Table if NOT exists!
            $model->populate(10); // INSERT 10 random rows of data

            $success = $model->save_new_user($input); // Save data from Request Data
            $input['model'] = ($success===true) ? 
                $model->get_users(15) : 
                $model->get_users(0);
        } else {
            $input['model'] = $model->get_users(0);
        }        
    }
    
}
