<?php
/* This file has been auto-generated. Do not edit this file directly. */
namespace SabaiApps\Directories\Component\Display\Model\Base;

use SabaiApps\Framework\Model\Model;
use SabaiApps\Framework\Criteria\AbstractCriteria;
use SabaiApps\Framework\DB\AbstractRowset;
use SabaiApps\Framework\Model\AbstractEntity;
use SabaiApps\Framework\Model\AbstractEntityRepository;

abstract class Element extends AbstractEntity
{
    public function __construct(Model $model)
    {
        parent::__construct('Element', $model);
        $this->_vars = ['element_name' => null, 'element_weight' => 0, 'element_data' => null, 'element_parent_id' => 0, 'element_system' => false, 'element_element_id' => 0, 'element_id' => 0, 'element_created' => 0, 'element_updated' => 0, 'element_display_id' => 0];
    }

    public function __clone()
    {
        $this->_vars = ['element_id' => 0, 'element_created' => 0, 'element_updated' => 0] + $this->_vars;
    }

    public function __toString()
    {
        return $this->__get('name');
    }

    public function __get($name)
    {
        if ($name === 'name')
            return $this->_vars['element_name'];
        elseif ($name === 'weight')
            return $this->_vars['element_weight'];
        elseif ($name === 'data')
            return $this->_vars['element_data'];
        elseif ($name === 'parent_id')
            return $this->_vars['element_parent_id'];
        elseif ($name === 'system')
            return $this->_vars['element_system'];
        elseif ($name === 'element_id')
            return $this->_vars['element_element_id'];
        elseif ($name === 'id')
            return $this->_vars['element_id'];
        elseif ($name === 'created')
            return $this->_vars['element_created'];
        elseif ($name === 'updated')
            return $this->_vars['element_updated'];
        elseif ($name === 'display_id')
            return $this->_vars['element_display_id'];
        elseif ($name === 'Display')
            return $this->_fetchEntity('Display', 'display_id');
        else
            return $this->fetchObject($name);
    }

    public function __set($name, $value)
    {
        if ($name === 'name')
            $this->_setVar('element_name', $value);
        elseif ($name === 'weight')
            $this->_setVar('element_weight', $value);
        elseif ($name === 'data')
            $this->_setVar('element_data', $value);
        elseif ($name === 'parent_id')
            $this->_setVar('element_parent_id', $value);
        elseif ($name === 'system')
            $this->_setVar('element_system', $value);
        elseif ($name === 'element_id')
            $this->_setVar('element_element_id', $value);
        elseif ($name === 'id')
            $this->_setVar('element_id', $value);
        elseif ($name === 'display_id')
            $this->_assignEntityById('Display', $value, 'element_display_id');
        elseif ($name === 'Display') {
            $_value = is_array($value) ? $value[0] : $value;
            if (is_object($_value)) {
                $this->_assignEntity($_value, 'element_display_id');
            } else {
                $this->_assignEntityById('Display', $_value, 'element_display_id');
            }
        }
        else
            $this->assignObject($name, $value);
    }

    protected function _initVar($name, $value)
    {
        if ($name === 'element_weight')
            $this->_vars['element_weight'] = (int)$value;
        elseif ($name === 'element_data')
            $this->_vars['element_data'] = @unserialize($value);
        elseif ($name === 'element_parent_id')
            $this->_vars['element_parent_id'] = (int)$value;
        elseif ($name === 'element_system')
            $this->_vars['element_system'] = (bool)$value;
        elseif ($name === 'element_element_id')
            $this->_vars['element_element_id'] = (int)$value;
        elseif ($name === 'element_id')
            $this->_vars['element_id'] = (int)$value;
        elseif ($name === 'element_created')
            $this->_vars['element_created'] = (int)$value;
        elseif ($name === 'element_updated')
            $this->_vars['element_updated'] = (int)$value;
        elseif ($name === 'element_display_id')
            $this->_vars['element_display_id'] = (int)$value;
        else
            $this->_vars[$name] = $value;
    }
}

abstract class ElementRepository extends AbstractEntityRepository
{
    public function __construct(Model $model)
    {
        parent::__construct('Element', $model);
    }

    public function fetchByDisplay($id, $limit = 0, $offset = 0, $sort = null, $order = null)
    {
        return $this->_fetchByForeign('element_display_id', $id, $limit, $offset, $sort, $order);
    }

    public function paginateByDisplay($id, $perpage = 10, $sort = null, $order = null)
    {
        return $this->_paginateByEntity('Display', $id, $perpage, $sort, $order);
    }

    public function countByDisplay($id)
    {
        return $this->_countByForeign('element_display_id', $id);
    }

    public function fetchByDisplayAndCriteria($id, AbstractCriteria $criteria, $limit = 0, $offset = 0, $sort = null, $order = null)
    {
        return $this->_fetchByForeignAndCriteria('element_display_id', $id, $criteria, $limit, $offset, $sort, $order);
    }

    public function paginateByDisplayAndCriteria($id, AbstractCriteria $criteria, $perpage = 10, $sort = null, $order = null)
    {
        return $this->_paginateByEntityAndCriteria('Display', $id, $criteria, $perpage, $sort, $order);
    }

    public function countByDisplayAndCriteria($id, AbstractCriteria $criteria)
    {
        return $this->_countByForeignAndCriteria('element_display_id', $id, $criteria);
    }

    protected function _getCollectionByRowset(AbstractRowset $rs)
    {
        return new ElementsByRowset($rs, $this->_model->create('Element'), $this->_model);
    }

    public function createCollection(array $entities = [])
    {
        return new Elements($this->_model, $entities);
    }
}

class ElementsByRowset extends \SabaiApps\Framework\Model\EntityCollection\RowsetEntityCollection
{
    public function __construct(AbstractRowset $rs, Element $emptyEntity, Model $model)
    {
        parent::__construct('Elements', $rs, $emptyEntity, $model);
    }
}

class Elements extends \SabaiApps\Framework\Model\EntityCollection\ArrayEntityCollection
{
    public function __construct(Model $model, array $entities = [])
    {
        parent::__construct($model, 'Elements', $entities);
    }
}