/** * Copy value or method return of source property to destination property * * @param t41_Data_Object $do * @return boolean */ public function execute(t41_Data_Object $do) { try { $prep = array(); foreach ($this->_source as $elem) { if (in_array($elem, array('+', '-', '/', '*'))) { $prep[] = $elem; } else { if (strpos($elem, '.') !== false) { $props = explode('.', $elem); $value = $do; foreach ($props as $prop) { if ($value instanceof \t41\ObjectModel\DataObject) { $value = $value->getProperty($prop)->getValue(); } else { if ($value instanceof ObjectModel\BaseObject) { $value = $value->getProperty($prop); } else { if ($value instanceof Property\PropertyAbstract) { $value = $value->getValue(); } } } } $prep[] = $value instanceof Property\PropertyAbstract ? $value->getValue() : $value; } else { $prep[] = $do->getProperty($elem)->getValue(); } } } $value = (double) eval(sprintf('return %s;', implode($prep))); /* set destination value with source value */ if (is_array($this->_destination)) { $do->getProperty($this->_destination[0])->getProperty($this->_destination[1])->setValue($value); } else { $do->getProperty($this->_destination)->setValue($value); } } catch (Exception $e) { echo $e->getTraceAsString(); /* @todo log exception */ return false; } return true; }
public function setRules(array $rules = array()) { if (count($rules) == 0) { $rules = (array) \t41\ObjectModel::getRules(get_class($this)); } $doRules = array(); foreach ($rules as $key => $rule) { /* if $rule is an array, targeted property contains an object and we need to delegate logic to data object */ if (is_array($rule)) { $doRules[] = $rule; unset($rules[$key]); } } if (count($doRules) > 0) { $this->_dataObject->delegateRules($doRules); } $this->_rules = $rules; return $this; }
/** * Update record data in the backend with passed data object properties values * * @param t41_Data_Object $do * @return boolean */ public function update(t41_Data_Object $do) { $uri = $do->getUri(); // get table to use, from mapper if available, else from data object $table = $this->_getTableFromUri($uri); // Properties mapping (to array) if ($this->_mapper) { $data = $do->map($this->_mapper, $this); $data = $data['data']; } foreach ($data as $key => $value) { if ($value instanceof ObjectModel\ObjectUri) { $data[$key] = $value->asString($this->_uri); } } $pkey = $this->_mapper ? $this->_mapper->getPrimaryKey($uri->getClass()) : 'id'; $this->_setLastQuery('update', $data, array('id' => $uri->getIdentifier())); return (bool) $this->_ressource->update($table, $data, $this->_ressource->quoteInto("{$pkey} = ?", $uri->getIdentifier())); }
/** * Delete record in backend * * @param t41_Data_Object $do * @return boolean */ public function delete(t41_Data_Object $do) { // @todo add a try/catch block return (bool) $this->_ressource->delete($do->getUri()->getIdentifier()); }
/** * Return a key/value array of all backend keys needed to build a query for a unique record * * @param t41_Data_Object $do * @return array */ protected function _preparePrimaryKeyClauses(t41_Data_Object $do) { /* no mapper or no pkey definition in mapper */ if (!$this->_mapper || !$this->_mapper->getPrimaryKey($do->getUri()->getClass())) { return array(Backend::DEFAULT_PKEY => $do->getUri()->getIdentifier()); } $array = array(); // example of mapper definition: <mapper id="myid" pkey="key1:string,key2:integer">...</mapper> $pkeyVals = explode(\t41\Mapper::VALUES_SEPARATOR, $do->getUri()->getIdentifier()); /* @var $obj t41_Backend_Key */ foreach ($this->_mapper->getPrimaryKey($do->getClass()) as $key => $obj) { if (!isset($pkeyVals[$key])) { continue; } $array[$obj->getName()] = $obj->castValue($pkeyVals[$key]); } return $array; }