__data = $data; parent::__construct($data); return $this; } /** * * @param array $data * @return $this */ public function setData(array $data) { $this->__data = $data; return $this; } /** * @param string $key * @return mixed */ private function _getValue($key){ return isset($this->__data[$key]) ? $this->__data[$key] : null; } public function __get($key){ return isset($this->__data[$key]) ? $this->__data[$key] : null; } /** * * @param string $key * @param mixed $value * @return $this */ public function setValue($key, $value){ $this->__data[$key] = $value; return $this; } /** * * @param string $method * @param string[] $args * @return mixed */ public function __call($method, $args) { if(($p = strpos($method, 'get')) !== false && $p === 0){ $fieldname = (str_replace('get', '', $method)); if(!empty($args)){ switch ($fieldname) { case 'Values': case 'Value': if(count($args) === 1) return $this->_getValue($args[0]); $values = []; foreach($args as $arg){ $values[$arg] = $this->_getValue($arg); } return $values; break; default: # code... break; } } //Convert data to insensitive case $lower_array_object = new \Phacil\Framework\ArrayClass\CaseInsensitiveArray( $this->__data ); return $lower_array_object[$fieldname]; } } /** * Convert object data into XML string * * @param string $rootName (Optional) root node name * @param bool $addOpenTag (Optional) flag that allow to add initial xml node * @param bool $addCdata (Optional) flag that require wrap all values in CDATA * @return string */ public function toXml($rootName = 'item', $addOpenTag = true, $addCdata = true) { $xml = ''; //$data = $this->__data; foreach ($this->__data as $fieldName => $fieldValue) { if ($addCdata === true) { $fieldValue = ""; } else { $fieldValue = $fieldValue !== null ? str_replace( ['&', '"', "'", '<', '>'], ['&', '"', ''', '<', '>'], $fieldValue ) : ''; } $xml .= "<{$fieldName}>{$fieldValue}\n"; } if ($rootName) { $xml = "<{$rootName}>\n{$xml}\n"; } if ($addOpenTag) { $xml = '' . "\n" . $xml; } return $xml; } }