<?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 Display extends AbstractEntity
{
    public function __construct(Model $model)
    {
        parent::__construct('Display', $model);
        $this->_vars = ['display_name' => null, 'display_bundle_name' => null, 'display_type' => null, 'display_data' => null, 'display_id' => 0, 'display_created' => 0, 'display_updated' => 0];
    }

    public function __clone()
    {
        $this->_vars = ['display_id' => 0, 'display_created' => 0, 'display_updated' => 0] + $this->_vars;
    }

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

    public function addElement(Element $entity)
    {
        $entity->Display = $this;

        return $this;
    }

    public function removeElement(Element $entity)
    {
        $this->removeElementById($entity->id);

        return $this;
    }

    public function removeElementById($id)
    {
        $this->_removeEntityById('element_id', 'Element', $id);

        return $this;
    }

    public function createElement()
    {
        return $this->_createEntity('Element');
    }

    public function removeElements()
    {
        $this->_removeEntities('Element');

        return $this;
    }

    public function __get($name)
    {
        if ($name === 'name')
            return $this->_vars['display_name'];
        elseif ($name === 'bundle_name')
            return $this->_vars['display_bundle_name'];
        elseif ($name === 'type')
            return $this->_vars['display_type'];
        elseif ($name === 'data')
            return $this->_vars['display_data'];
        elseif ($name === 'id')
            return $this->_vars['display_id'];
        elseif ($name === 'created')
            return $this->_vars['display_created'];
        elseif ($name === 'updated')
            return $this->_vars['display_updated'];
        elseif ($name === 'Elements')
            return $this->_fetchEntities('Element', 'Elements');
        else
            return $this->fetchObject($name);
    }

    public function __set($name, $value)
    {
        if ($name === 'name')
            $this->_setVar('display_name', $value);
        elseif ($name === 'bundle_name')
            $this->_setVar('display_bundle_name', $value);
        elseif ($name === 'type')
            $this->_setVar('display_type', $value);
        elseif ($name === 'data')
            $this->_setVar('display_data', $value);
        elseif ($name === 'id')
            $this->_setVar('display_id', $value);
        elseif ($name === 'Elements') {
            $this->removeElements();
            foreach (array_keys($value) as $i) $this->addElement($value[$i]);
        }
        else
            $this->assignObject($name, $value);
    }

    protected function _initVar($name, $value)
    {
        if ($name === 'display_data')
            $this->_vars['display_data'] = @unserialize($value);
        elseif ($name === 'display_id')
            $this->_vars['display_id'] = (int)$value;
        elseif ($name === 'display_created')
            $this->_vars['display_created'] = (int)$value;
        elseif ($name === 'display_updated')
            $this->_vars['display_updated'] = (int)$value;
        else
            $this->_vars[$name] = $value;
    }
}

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

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

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

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

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