/** * @param DOMNode $node * @param ProxyObject $proxy */ private function process(DOMNode $node, ProxyObject $proxy) { $proxy->setName($node->nodeName); if ($node->hasAttributes()) { for ($i = 0; $i < $node->attributes->length; $i++) { $attribute = $node->attributes->item($i); $proxy->set($attribute->name, $attribute->value); } } if ($node->hasChildNodes()) { $nodeTypes = array(); foreach ($node->childNodes as $childNode) { if ($childNode->nodeName === '#text') { $proxy->setValue($childNode->nodeValue); } else { $childProxy = new ProxyObject(); $this->process($childNode, $childProxy); $nodeTypes[$childProxy->getName()][] = $childProxy; } } foreach ($nodeTypes as $tagName => $nodes) { $proxy->set($tagName, $nodes); } } }
/** * @param $data * @param ProxyObject $proxy */ private function process($data, ProxyObject $proxy) { if (is_object($data)) { foreach ($data as $key => $value) { $childProxy = new ProxyObject(); $this->process($value, $childProxy); $proxy->{$key} = $childProxy; } } elseif (is_array($data)) { $children = array(); foreach ($data as $value) { $childProxy = new ProxyObject(); $this->process($value, $childProxy); $children[] = $childProxy; } $proxy->setValue($children); } else { $proxy->setValue($data); } }
/** * @param ProxyObject $proxy * @param $fieldString * @return array */ private function getProxyFieldValues(ProxyObject $proxy, $fieldString) { $returnValues = array(); // only do when $fieldString === '' since attributes etc have to be processed first if ($fieldString === '') { if ($proxy->isArray()) { foreach ($proxy as $field) { $returnValues[] = $field(); } } else { $returnValues[] = $proxy(); } return $returnValues; } $fields = explode('.', $fieldString); $field = array_shift($fields); $fieldString = implode('.', $fields); // have to return an array of values to match for has_many relationships preg_match('/^(\\w+)\\[(\\w+)=([a-zA-Z-_ ]([0-9a-zA-Z-_ ]+)?)\\]$/', $field, $matches); if (!empty($matches)) { $field = $matches[1]; $attribute = $matches[2]; $value = $matches[3]; if ($proxy->{$field}->isArray()) { foreach ($proxy->{$field} as $option) { if ($option->{$attribute}() == $value) { $values = $this->getProxyFieldValues($option, $fieldString); $returnValues = array_merge($returnValues, $values); } } } else { if ($proxy->{$attribute} == $value) { $values = $this->getProxyFieldValues($proxy->{$field}, $fieldString); $returnValues = array_merge($returnValues, $values); } } } else { if ($proxy->{$field}->isArray()) { foreach ($proxy->{$field} as $option) { $values = $this->getProxyFieldValues($option, $fieldString); $returnValues = array_merge($returnValues, $values); } } else { $values = $this->getProxyFieldValues($proxy->{$field}, $fieldString); $returnValues = array_merge($returnValues, $values); } } return $returnValues; }
/** * */ public function testGetAndSet_CaseSensitivity_IgnoresCase() { $proxyObject = new ProxyObject(); $proxyObject->Greeting = 'Hello World'; $proxyObject->Greeting->speaker = 'chris'; $this->assertEquals('Hello World', $proxyObject->Greeting()); $this->assertEquals('Hello World', $proxyObject->greeting()); $this->assertEquals('chris', $proxyObject->greeting->speaker()); $this->assertEquals('chris', $proxyObject->greeting->Speaker()); $this->assertTrue(isset($proxyObject->Greeting)); $this->assertTrue(isset($proxyObject->greeting)); $this->assertTrue(isset($proxyObject->greeting->speaker)); $this->assertTrue(isset($proxyObject->greeting->Speaker)); $this->assertFalse(isset($proxyObject->greeting->Hello)); unset($proxyObject->Greeting->Speaker); $this->assertFalse(isset($proxyObject->greeting->speaker)); }