Untitled

 avatar
unknown
plain_text
3 years ago
1.8 kB
1
Indexable
<?php
    namespace model;

    include_once (dirname(__DIR__) . "/Autoload.php");    
    
    use api\Autoload;

    use controller\CompanyController;

    use model\Model;
    use model\User;

    final class Company extends Model
    {        
        public $name;
        public $code;

        public $users;

        public $created_at;
        public $updated_at;


        
        final protected function find() : void
        {
            $controller = new CompanyController();

            $model = $controller->read($this->id());

            $this->name = $model["name"];
            $this->code = $model["code"];

            $this->created_at = $model["created_at"];
            $this->updated_at = $model["updated_at"];

            $this->users = $controller->readUsers($this->id());

        }

        public function setUsers()
        {
            $i = 0;

            if(isset($this->users) && isset($this->users[$i]))
            {
                if($this->users[$i] instanceof User)
                {
                    return;
                }
    
                foreach($this->users as $user)
                {
                    $this->users[$i] = new User($user);
    
                    $i++;
                }
            }
        }

        public function getByCode(string $code)
        {
            $controller = new CompanyController();

            $id = $controller->readByCode($code);

            return (isset($id) ? new Company($id["id"]) : $id);
        }

        final public function asArray() : array
        {
            return [
                "id" => $this->id()
            ];
        }
    }    

    Autoload::unload(__FILE__);
?>