<?php
class AnDomainDescriptionAttribute extends AnDomainDescriptionAttributeAbstract
{
/**
* The field that the property maps to
*
* @var string
*/
protected $_field;
/**
* A filter that sanitize the value of the field upon assignment
*
* @var string
*/
protected $_filter;
/**
*
* @return
* @param $options Object[optional]
*/
public function __construct($options=array())
{
parent::__construct($options);
$default = array(
'field' => KInflector::underscore($this->getName()) ,
'filter' => null
);
$options = array_merge($default, $options);
$this->_filter = $options['filter'];
$this->_field = $options['field'];
}
public function validatePresenceOf($value)
{
return !is_null($value) && $value != '';
}
/**
*
* @return
* @param $value Object
*/
public function getSerializedValue($value)
{
return $value;
}
/**
*
* @return
*/
public function getField()
{
return $this->_field;
}
/**
*
* @return
*/
public function getAttrTree()
{
return array($this->getName() => $this->getField());
}
/**
*
* @return
* @param $value Object
*/
public function serialize($value)
{
return array($this->getField() => $value);
}
public function getTableFields()
{
return array($this->getField());
}
public function getTableValues($value)
{
return array($value);
}
/**
*
* @return
* @param $data Object
*/
public function materialize($data)
{
$key = str_replace('.','_', $this->getField());
if ( !isset($data[$key]) ) {
$value = null; //new AnDomainProxyProperty(array('property'=>$this));
//return a proxy of the property
} else {
$value = $data[$key];
}
return $value;
}
/**
* Set filter for attribute
*
* @return
* @param $filter Object
*/
public function setFilter($filter)
{
$this->_filter = $filter;
return $this;
}
/**
* Get attribute filter
*
* @return Filter
*/
public function getFilter()
{
if(!isset($this->_filter)) {
//@TODO maybe we can filter based on the field type
return null;
}
if(!($this->_filter instanceof KFilterInterface))
{
$names = (array) $this->_filter;
$name = array_shift($names);
$filter = $this->_createFilter($name);
foreach($names as $name) {
$filter->addFilter($this->_createFilter($name));
}
$this->_filter = $filter;
}
return $this->_filter;
}
/**
* Create the filter object using the filter name
*
* @return
* @param $filter Filter
*/
protected static function _createFilter($filter)
{
// Convert 'foo' to 'lib.koowa.filter.foo'
if(is_string($filter) && strpos($filter, '.') === false ) {
$filter = 'lib.koowa.filter.'.$filter;
}
try {
$filter = KFactory::get($filter);
} catch(KFactoryAdapterException $e) {
throw new InvalidArgumentException('Invalid filter: '.$filter);
}
return $filter;
}
}