/** * */ public function testIterator_SetSingleElement_IteratesProxyValues() { $values = array('This is a string'); $proxyObject = new ProxyObject($values); $this->assertTrue($proxyObject->isArray()); $this->assertEquals(1, $proxyObject->count()); $count = 0; foreach ($proxyObject as $key => $value) { $count++; $this->assertEquals($values[0], $value()); } $this->assertEquals(1, $count); }
/** * @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; }