<?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 AnDomainDescriptionRelationshipOnetomany extends AnDomainDescriptionRelationshipAbstract
{
protected $_collection;
protected $_cardinality;
/**
*
* @return
* @param $options Object
*/
public function __construct($options)
{
$model = new KIdentifier( $options['mapper']->getModel());
$model->name = KInflector::singularize( $options['name'] );
$default = array(
'collection' => 'lib.anahita.domain.collection.queriable',
'cardinality' => 'many' ,
'parent_key' => null ,
'model' => $model ,
);
$options = array_merge($default, $options);
parent::__construct($options);
$this->_collection = $options['collection'];
$this->_cardinality = $options['cardinality'];
/*
if ( is_numeric($options['cardinality']) && (int) $options['cardinality'] > 0 ) {
$this->query()->limit($options['cardinality']);
}
$this->mixin( $this->query() );*/
}
/**
*
* @return
* @param $collection Object
*/
public function setCollection($collection)
{
$this->_collection;
}
public function getCollection()
{
return $this->_collection;
}
public function __call($method, $args)
{
if ( method_exists($this->query(), $method) ) {
call_user_func_array(array($this->query(), $method), $args);
return $this;
} else
return parent::__call($method, $args);
}
/**
*
* @return
* @param $data Object
*/
public function materialize($instance, $data)
{
$conditions = array();
$query = clone $this->query();
$child_key = $this->getChildKey();
$parent_key = $this->getParentKey();
if ( $property = $this->getTargetMapper()->getProperty($child_key) ) {
$parent_key_value = $instance;
} else if ( !$parent_key ) {
$parent_key = $this->getMapper()->getProperty('id')->getField();
}
if ( $parent_key ) {
$parent_key_value = @$data[str_replace('.','_',$parent_key)];
}
$this->_evalQuery($query, $instance, $data);
$query->where($child_key,'=',$parent_key_value);
$collection = KFactory::tmp($this->_collection, array('query'=>$query));
if ( is_numeric($this->_cardinality) )
$query->limit( $this->_cardinality );
$proxy = KFactory::tmp('lib.anahita.domain.proxy.collection',array(
'collection' => $collection
));
return $proxy;
}
/**
* Evalulate a query with the data passed in the from database
* @return
* @param $query Object
* @param $instance Object
*/
protected function _evalQuery($query, $instance, $data)
{
$wheres = array();
foreach($query->where as $where) {
$pos = strpos($where, '{');
if ( strpos($where, '{') > 0 || $pos === 0 ) {
$matches = array();
if ( !preg_match_all('/{(.*?)}/', $where, $matches) ) continue;
$matches = $matches[1];
foreach($matches as $match) {
if ( strpos('this.', $match) >= 0 ) {
$key = str_replace('this.','', $match);
$value = $instance->{$key};
$value = $instance->getMapper()->getProperty($key)->getSerializedValue($value);
} else {
$value = @$data[str_replace('.','_',$match)];
}
$where = str_replace('{'.$match.'}', $value, $where);
}
}
$wheres[] = $where;
}
$query->where = $wheres;
}
}