<?php
/**
* @version 1.0.0
* @category Anahita Social Engineâ¢
* @copyright Copyright (C) 2008 - 2010 rmdStudio Inc. and Peerglobe Technology Inc. All rights reserved.
* @license GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
* @link http://www.anahitapolis.com
*/
class AnDomainRepositoryAbstract extends KEventDispatcher implements KFactoryIdentifiable
{
protected $_db;
protected $_identifier;
protected $_collection;
protected $_context;
protected $_mapper;
public function __construct($options)
{
$this->_identifier = $options['identifier'];
// Initialize the options
$options = $this->_initialize($options);
$this->_db = $options['adapter'];
$this->_collection = $options['collection'];
$this->_context = $options['context'];
$this->_mapper = $options['mapper'];
// Mixin a command chain
$this->mixin(new KMixinCommandchain(array('mixer' => $this, 'command_chain' => $options['command_chain'])));
//Mixin a filter
$this->mixin(new KMixinCommand(array('mixer' => $this, 'command_chain' => $this->getCommandChain())));
parent::__construct($options);
}
protected function _initialize(array $options)
{
$defaults = array(
'adapter' => KFactory::get('lib.koowa.database') ,
'command_chain' => new KCommandChain() ,
'context' => KFactory::get('lib.anahita.domain.context'),
'collection' => 'lib.anahita.domain.collection.page',
'query' => 'lib.anahita.domain.query',
'identifier' => null,
'mapper' => null
);
return array_merge($defaults, $options);
}
public function setCollection($collection)
{
$this->_collection = $collection;
}
public function getCollection()
{
return $this->_collection;
}
public function setAdapter($adapter)
{
$this->_db = $adapter;
}
public function getAdapter()
{
return $this->_db;
}
public function fetch(AnDomainQuery $query)
{
$query->limit(1);
$object = $this->_context->exists($query);
$model = $query->getModel();
$arg = new ArrayObject();
$arg['query'] = $query;
$arg['model'] = $model;
$arg['object'] = $object;
$arg['notifier'] = $this;
$name = new KIdentifier($model);
$name = ucfirst(KInflector::pluralize($name->name));
$this->dispatch('onBeforeFetch'.$name, $arg);
if ( !$object)
$object = KFactory::get('lib.anahita.domain.factory.mapper')->get($model)->fetch($arg['query']);
$arg['result'] = array($object);
$this->dispatch('onAfterFetch'.$name, $arg);
return $object;
}
public function fetchAll(AnDomainQuery $query)
{
$model = $query->getModel();
$arg = new ArrayObject();
$arg['query'] = $query;
$arg['model'] = $model;
$arg['notifier'] = $this;
$name = new KIdentifier($model);
$name = ucfirst(KInflector::pluralize($name->name));
$this->dispatch('onBeforeFetch'.$name, $arg);
$objects = KFactory::tmp($this->getCollection(), array('query' => $arg['query']));
//$objects->load();
$arg['result'] = $objects;
$this->dispatch('onAfterFetch'.$name, $arg);
return $objects;
}
public function setMapper($mapper)
{
$this->_mapper = $mapper;
}
public function getMapper()
{
return $this->_mapper;
}
public function getTotal($query)
{
$model = $query->getModel();
$arg = new ArrayObject();
$arg['query'] = $query;
$arg['model'] = $model;
$arg['notifier'] = $this;
$name = new KIdentifier($model);
$name = ucfirst(KInflector::pluralize($name->name));
$this->dispatch('onBeforeFetch'.$name, $arg);
$arg['query']->limit(0, 0);
$arg['query']->count();
$total = $this->_db->fetchResult($arg['query']);
return $total;
}
public function commit($entity)
{
}
public function getDomainContext()
{
return $this->_context;
}
public function getEntityIdentifier()
{
return $this->_model;
}
public function getIdentifier()
{
return $this->_identifier;
}
}