<?php
/* This file has been auto-generated. Do not edit this file directly. */
namespace SabaiApps\Directories\Component\Voting\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 Vote extends AbstractEntity {

	public function __construct( Model $model ) {
		parent::__construct( 'Vote', $model );
		$this->_vars = array(
			'vote_bundle_name'  => null,
			'vote_entity_id'    => 0,
			'vote_value'        => 0,
			'vote_field_name'   => null,
			'vote_name'         => null,
			'vote_comment'      => null,
			'vote_reference_id' => 0,
			'vote_hash'         => null,
			'vote_level'        => 0,
			'vote_marked'       => 0,
			'vote_id'           => 0,
			'vote_created'      => 0,
			'vote_updated'      => 0,
			'vote_user_id'      => 0,
		);
	}

	public function __clone() {
		$this->_vars = array(
			'vote_id'      => 0,
			'vote_created' => 0,
			'vote_updated' => 0,
		) + $this->_vars;
	}

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

	protected function _assignUser( $user, $markDirty = true ) {
		$this->_setVar( 'vote_user_id', $user->id, $markDirty );
	}

	protected function _fetchUser() {
		if ( ! isset( $this->_objects['User'] ) ) {
			$this->_objects['User'] = $this->_model->UserIdentity( $this->_vars['vote_user_id'] );
		}

		return $this->_objects['User'];
	}

	public function isOwnedBy( $user ) {
		return $this->user_id && $this->user_id == $user->id;
	}

	public function __get( $name ) {
		if ( $name === 'bundle_name' ) {
			return $this->_vars['vote_bundle_name'];
		} elseif ( $name === 'entity_id' ) {
			return $this->_vars['vote_entity_id'];
		} elseif ( $name === 'value' ) {
			return $this->_vars['vote_value'];
		} elseif ( $name === 'field_name' ) {
			return $this->_vars['vote_field_name'];
		} elseif ( $name === 'name' ) {
			return $this->_vars['vote_name'];
		} elseif ( $name === 'comment' ) {
			return $this->_vars['vote_comment'];
		} elseif ( $name === 'reference_id' ) {
			return $this->_vars['vote_reference_id'];
		} elseif ( $name === 'hash' ) {
			return $this->_vars['vote_hash'];
		} elseif ( $name === 'level' ) {
			return $this->_vars['vote_level'];
		} elseif ( $name === 'marked' ) {
			return $this->_vars['vote_marked'];
		} elseif ( $name === 'id' ) {
			return $this->_vars['vote_id'];
		} elseif ( $name === 'created' ) {
			return $this->_vars['vote_created'];
		} elseif ( $name === 'updated' ) {
			return $this->_vars['vote_updated'];
		} elseif ( $name === 'user_id' ) {
			return $this->_vars['vote_user_id'];
		} elseif ( $name === 'User' ) {
			return $this->_fetchUser();
		} else {
			return $this->fetchObject( $name );
		}
	}

	public function __set( $name, $value ) {
		if ( $name === 'bundle_name' ) {
			$this->_setVar( 'vote_bundle_name', $value );
		} elseif ( $name === 'entity_id' ) {
			$this->_setVar( 'vote_entity_id', $value );
		} elseif ( $name === 'value' ) {
			$this->_setVar( 'vote_value', $value );
		} elseif ( $name === 'field_name' ) {
			$this->_setVar( 'vote_field_name', $value );
		} elseif ( $name === 'name' ) {
			$this->_setVar( 'vote_name', $value );
		} elseif ( $name === 'comment' ) {
			$this->_setVar( 'vote_comment', $value );
		} elseif ( $name === 'reference_id' ) {
			$this->_setVar( 'vote_reference_id', $value );
		} elseif ( $name === 'hash' ) {
			$this->_setVar( 'vote_hash', $value );
		} elseif ( $name === 'level' ) {
			$this->_setVar( 'vote_level', $value );
		} elseif ( $name === 'marked' ) {
			$this->_setVar( 'vote_marked', $value );
		} elseif ( $name === 'id' ) {
			$this->_setVar( 'vote_id', $value );
		} elseif ( $name === 'user_id' ) {
			$this->_setVar( 'vote_user_id', $value );
		} elseif ( $name === 'User' ) {
			$this->_assignUser( $value );
		} else {
			$this->assignObject( $name, $value );
		}
	}

	protected function _initVar( $name, $value ) {
		if ( $name === 'vote_entity_id' ) {
			$this->_vars['vote_entity_id'] = (int) $value;
		} elseif ( $name === 'vote_value' ) {
			$this->_vars['vote_value'] = (float) $value;
		} elseif ( $name === 'vote_reference_id' ) {
			$this->_vars['vote_reference_id'] = (int) $value;
		} elseif ( $name === 'vote_level' ) {
			$this->_vars['vote_level'] = (int) $value;
		} elseif ( $name === 'vote_marked' ) {
			$this->_vars['vote_marked'] = (int) $value;
		} elseif ( $name === 'vote_id' ) {
			$this->_vars['vote_id'] = (int) $value;
		} elseif ( $name === 'vote_created' ) {
			$this->_vars['vote_created'] = (int) $value;
		} elseif ( $name === 'vote_updated' ) {
			$this->_vars['vote_updated'] = (int) $value;
		} elseif ( $name === 'vote_user_id' ) {
			$this->_vars['vote_user_id'] = (int) $value;
		} else {
			$this->_vars[ $name ] = $value;
		}
	}
}

abstract class VoteRepository extends AbstractEntityRepository {

	public function __construct( Model $model ) {
		parent::__construct( 'Vote', $model );
	}

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

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

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

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

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

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

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

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

class VotesByRowset extends \SabaiApps\Framework\Model\EntityCollection\RowsetEntityCollection {

	public function __construct( AbstractRowset $rs, Vote $emptyEntity, Model $model ) {
		parent::__construct( 'Votes', $rs, $emptyEntity, $model );
	}
}

class Votes extends \SabaiApps\Framework\Model\EntityCollection\ArrayEntityCollection {

	public function __construct( Model $model, array $entities = array() ) {
		parent::__construct( $model, 'Votes', $entities );
	}
}
