<?php
/* This file has been auto-generated. Do not edit this file directly. */
namespace SabaiApps\Directories\Component\Entity\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 Bundle extends AbstractEntity
{
    public function __construct(Model $model)
    {
        parent::__construct('Bundle', $model);
        $this->_vars = ['id' => null, 'bundle_name' => null, 'bundle_type' => null, 'bundle_component' => null, 'bundle_info' => null, 'bundle_entitytype_name' => null, 'bundle_group' => null, 'bundle_created' => 0, 'bundle_updated' => 0];
    }

    public function __clone()
    {
        $this->_vars = ['id' => null, 'bundle_name' => null, 'bundle_created' => 0, 'bundle_updated' => 0] + $this->_vars;
    }

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

    public function initVars(array $arr)
    {
        parent::initVars($arr);
        $this->_vars['id'] = $this->_vars['bundle_name'];
    }

    public function addField(Field $entity)
    {
        $entity->Bundle = $this;

        return $this;
    }

    public function removeField(Field $entity)
    {
        $this->removeFieldById($entity->id);

        return $this;
    }

    public function removeFieldById($id)
    {
        $this->_removeEntityById('field_id', 'Field', $id);

        return $this;
    }

    public function createField()
    {
        return $this->_createEntity('Field');
    }

    public function removeFields()
    {
        $this->_removeEntities('Field');

        return $this;
    }

    public function __get($name)
    {
        if ($name === 'id')
            return $this->_vars['id'];
        elseif ($name === 'name')
            return $this->_vars['bundle_name'];
        elseif ($name === 'type')
            return $this->_vars['bundle_type'];
        elseif ($name === 'component')
            return $this->_vars['bundle_component'];
        elseif ($name === 'info')
            return $this->_vars['bundle_info'];
        elseif ($name === 'entitytype_name')
            return $this->_vars['bundle_entitytype_name'];
        elseif ($name === 'group')
            return $this->_vars['bundle_group'];
        elseif ($name === 'created')
            return $this->_vars['bundle_created'];
        elseif ($name === 'updated')
            return $this->_vars['bundle_updated'];
        elseif ($name === 'Fields')
            return $this->_fetchEntities('Field', 'Fields');
        else
            return $this->fetchObject($name);
    }

    public function __set($name, $value)
    {
        if ($name === 'id')
            $this->_setVar('id', $value);
        elseif ($name === 'name')
            $this->_setVar('bundle_name', $value);
        elseif ($name === 'type')
            $this->_setVar('bundle_type', $value);
        elseif ($name === 'component')
            $this->_setVar('bundle_component', $value);
        elseif ($name === 'info')
            $this->_setVar('bundle_info', $value);
        elseif ($name === 'entitytype_name')
            $this->_setVar('bundle_entitytype_name', $value);
        elseif ($name === 'group')
            $this->_setVar('bundle_group', $value);
        elseif ($name === 'Fields') {
            $this->removeFields();
            foreach (array_keys($value) as $i) $this->addField($value[$i]);
        }
        else
            $this->assignObject($name, $value);
    }

    protected function _initVar($name, $value)
    {
        if ($name === 'bundle_info')
            $this->_vars['bundle_info'] = @unserialize($value);
        elseif ($name === 'bundle_created')
            $this->_vars['bundle_created'] = (int)$value;
        elseif ($name === 'bundle_updated')
            $this->_vars['bundle_updated'] = (int)$value;
        else
            $this->_vars[$name] = $value;
    }
}

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

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

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

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

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