User.php 3.58 KB
<?php

namespace App\Model;

use App\Constants\AclRoles;
use App\Mvc\DateTrackingModel;
use App\User\Service;
use PhalconApi\Exception;
use PhalconApi\Constants\ErrorCodes;

class User extends DateTrackingModel
{
    public $id;
    public $role;
    public $username;
    public $password;
    public $email;

    public function getSource()
    {
        return 'user';
    }

    public function columnMap()
    {
        return parent::columnMap() + [
            'id' => 'id',
            'role' => 'role',
            'email' => 'email',
            'username' => 'username',
            'password' => 'password'
        ];
    }

    public function initialize()
    {
        $this->hasMany(
            'id',
            Project::class,
            'user_id',
            [
                'alias' => 'Projects',
            ]
        );

        $this->hasManyToMany(
            'id',
            UserProject::class,
            'user_id',
            'project_id',
            Project::class,
            'id',
            array('alias' => 'projects')
        );
    }

    /** ------- Getters and Setters ------- **/

        /** validation: unique, non-empty, 4+ letters *
         * @param $username
         * @throws Exception
         */
        public function setUsername($username)
        {

            $same_user = User::find(["username = '".$username."'"]);
            if (isset($same_user[0]) && !empty($same_user[0]->username))
            {
                $msg = 'Post-data is invalid, trying to use non-unique value `'.$username.'` of `username`';
                throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['username' => $username]);
            }
            elseif (empty($username))
            {
                $msg = 'Post-data is invalid, trying to use empty value of `username`';
                throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['username' => $username]);
            }
            elseif (strlen($username) < 4)
            {
                $msg = 'Post-data is invalid, value of `username` should be more than 4 letters';
                throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['username' => $username]);
            }

            $this->username = $username;
        }

        /** validation: FILTER_VALIDATE_EMAIL *
         * @param $email
         * @throws Exception
         */
        public function setEmail($email)
        {
            if (!filter_var($email, FILTER_VALIDATE_EMAIL))
            {
                $msg = 'Post-data is invalid, bad email value';
                throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['email' => $email]);
            }

            $this->email = $email;
        }

        /** validation: constant value *
         * @param $role
         * @throws Exception
         */
        public function setRole($role)
        {
            if (!in_array($role, AclRoles::ALL_REAL_ROLES))
            {
                $msg = 'Post-data is invalid, bad `role` value';
                throw new Exception(
                    ErrorCodes::POST_DATA_INVALID,
                    $msg,
                    ['role' => $role, 'valid values' => AclRoles::ALL_REAL_ROLES]
                );
            }

            $this->role = $role;
        }


        public function getUsername()
        {
            return $this->username;
        }

        public function getEmail()
        {
            return $this->email;
        }

        public function getRole()
        {
            $service = new Service();
            return $service->getRole();
        }

    /** ----------------------------------- **/

}