67 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			67 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace kernel;
 | 
						|
 | 
						|
use MadeSimple\Validator\Validator;
 | 
						|
 | 
						|
class FormModel
 | 
						|
{
 | 
						|
    protected Validator $validator;
 | 
						|
 | 
						|
    protected array $data = [];
 | 
						|
 | 
						|
    public function __construct()
 | 
						|
    {
 | 
						|
        $this->validator = new Validator();
 | 
						|
    }
 | 
						|
 | 
						|
    public function rules(): array
 | 
						|
    {
 | 
						|
        return [];
 | 
						|
    }
 | 
						|
 | 
						|
    public function load(array $array): void
 | 
						|
    {
 | 
						|
        $rules = $this->rules();
 | 
						|
        foreach ($array as $key => $item) {
 | 
						|
            if (isset($rules[$key])) {
 | 
						|
                $this->data[$key] = $item;
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public function getData(): array
 | 
						|
    {
 | 
						|
        return $this->data;
 | 
						|
    }
 | 
						|
 | 
						|
    public function setItem(string $name, string|int $value): void
 | 
						|
    {
 | 
						|
        $this->data[$name] = $value;
 | 
						|
    }
 | 
						|
 | 
						|
    public function getItem(string $name)
 | 
						|
    {
 | 
						|
        if (isset($this->data[$name])){
 | 
						|
            return $this->data[$name];
 | 
						|
        }
 | 
						|
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
    public function validate(): bool
 | 
						|
    {
 | 
						|
        $res = $this->validator->validate($this->data, $this->rules());
 | 
						|
        if (!$res) {
 | 
						|
            return true;
 | 
						|
        }
 | 
						|
 | 
						|
        return false;
 | 
						|
    }
 | 
						|
 | 
						|
    public function getErrors(): array
 | 
						|
    {
 | 
						|
        return $this->validator->getProcessedErrors();
 | 
						|
    }
 | 
						|
 | 
						|
} |