Project.php 3.44 KB
<?php

namespace App\Model;

use App\Mvc\DateTrackingModel;
use PhalconApi\Constants\ErrorCodes;
use PhalconApi\Exception;

class Project extends DateTrackingModel
{
    public $id;
    public $name;
    public $user_id;
    public $ga_view_id;
    public $group;

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

    public function columnMap()
    {
        return parent::columnMap() + [
            'id'            => 'id',
            'name'          => 'name',
            'ga_view_id'    => 'ga_view_id',
            'user_id'       => 'user_id',
            'group'         => 'group'
        ];
    }

    public function initialize() {

        $this->belongsTo('user_id', User::class, 'id', [
            'alias' => 'User',
        ]);

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

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

        /** validation: non-empty, 4+ letters *
         * @param $name
         * @throws Exception
         */
        public function setName($name)
        {
            if (empty($name))
            {
                $msg = 'Post-data is invalid, trying to use empty value of `name`';
                throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['name' => $name]);
            }
            elseif (strlen($name) < 4)
            {
                $msg = 'Post-data is invalid, value of `name` should be more than 4 letters';
                throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['name' => $name]);
            }

            $this->name = $name;
        }

        /** validation: non-empty, integer *
         * @param $ga_view_id
         * @throws Exception
         */
        public function setGa_view_id($ga_view_id)
        {
            if (empty($ga_view_id))
            {
                $msg = 'Post-data is invalid, trying to use empty value of `ga_view_id`';
                throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['ga_view_id' => $ga_view_id]);
            }
            elseif (!is_integer($ga_view_id))
            {
                $msg = 'Post-data is invalid, type of `ga_view_id` should be integer';
                throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['ga_view_id' => $ga_view_id]);
            }

            $this->ga_view_id = $ga_view_id;
        }

        /** validation: non-empty, integer *
         * @param $group
         * @throws Exception
         */
        public function setGroup($group)
        {
            if (empty($group))
            {
                $msg = 'Post-data is invalid, trying to use empty value of `group`';
                throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['group' => $group]);
            }
            elseif (!is_integer($group))
            {
                $msg = 'Post-data is invalid, type of `group` should be integer';
                throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['group' => $group]);
            }

            $this->group = $group;
        }

        public function getName()
        {
            return $this->name;
        }

        public function getGa_view_id()
        {
            return $this->ga_view_id;
        }

        public function getGroup()
        {
            return $this->group;
        }

    /** ----------------------------------- **/
}