/** * @param array $map * @param string $prefix */ public function init(array $map = array(), $prefix = '') { if (empty($map)) { return; } $map = PPUtils::lowerKeys($map); foreach (get_object_vars($this) as $property => $defaultValue) { if (array_key_exists($propKey = strtolower($prefix . $property), $map) && $this->isBuiltInType($type = PPUtils::propertyType($this, $property))) { $type = PPUtils::propertyType($this, $property); $this->{$property} = urldecode($map[$propKey]); continue; // string } elseif (!($filtered = PPUtils::filterKeyPrefix($map, $propKey))) { continue; // NULL } $type = PPUtils::propertyType($this, $property); if (!$this->isBuiltInType($type) && !class_exists($type)) { trigger_error("Class {$type} not found.", E_USER_NOTICE); continue; // just ignore } if (is_array($defaultValue) || PPUtils::isPropertyArray($this, $property)) { // array of objects if ($this->isBuiltInType($type)) { // Array of simple types foreach ($filtered as $key => $value) { $this->{$property}[trim($key, "()")] = urldecode($value); } } else { // Array of complex objects $delim = '.'; for ($i = 0; $itemValues = PPUtils::filterKeyPrefix($filtered, "({$i})"); $i++) { $this->{$property}[$i] = $item = new $type(); $item->init(PPUtils::filterKeyPrefix($itemValues, ".")); if (array_key_exists("", $itemValues)) { $item->value = urldecode($itemValues[""]); } } // Handle cases where we have a list of objects // with just the value present and all attributes values are null foreach ($filtered as $key => $value) { $idx = trim($key, "()"); if (is_numeric($idx) && (is_null($this->{$property}) || !array_key_exists($idx, $this->{$property}))) { $this->{$property}[$idx] = new $type(); $this->{$property}[$idx]->value = urldecode($value); } } } } else { // one object $this->{$property} = new $type(); $this->{$property}->init(PPUtils::filterKeyPrefix($filtered, '.')); // unprefix if (array_key_exists("", $filtered)) { $this->{$property}->value = urldecode($filtered[""]); } } } }
/** * @param array $map * @param string $isRoot */ public function init(array $data = array()) { foreach ($data[0]['children'] as $c) { if ($c['name'] == 'detail') { $map = $c['children'][0]['children']; break; } } if (!isset($map)) { return; } if (($first = reset($map)) && !is_array($first) && !is_numeric(key($map))) { parent::init($map, false); return; } $propertiesMap = PPUtils::objectProperties($this); $arrayCtr = array(); foreach ($map as $element) { if (empty($element) || empty($element['name'])) { continue; } elseif (!array_key_exists($property = strtolower($element['name']), $propertiesMap)) { if (!preg_match('~^(.+)[\\[\\(](\\d+)[\\]\\)]$~', $property, $m)) { continue; } $element['name'] = $m[1]; $element['num'] = $m[2]; } $element['name'] = $propertiesMap[strtolower($element['name'])]; if (PPUtils::isPropertyArray($this, $element['name'])) { $arrayCtr[$element['name']] = isset($arrayCtr[$element['name']]) ? $arrayCtr[$element['name']] + 1 : 0; $element['num'] = $arrayCtr[$element['name']]; } if (!empty($element["attributes"]) && is_array($element["attributes"])) { foreach ($element["attributes"] as $key => $val) { $element["children"][] = array('name' => $key, 'text' => $val); } if (isset($element['text'])) { $element["children"][] = array('name' => 'value', 'text' => $element['text']); } $this->fillRelation($element['name'], $element); } elseif (!empty($element['text'])) { $this->{$element['name']} = $element['text']; } elseif (!empty($element["children"]) && is_array($element["children"])) { $this->fillRelation($element['name'], $element); } } }
/** * @test */ public function testGetPropertyType() { $this->assertEquals('string', PPUtils::propertyType('MockReflectionTestType', 'noAnnotations')); $this->assertEquals('SomeType', PPUtils::propertyType('MockReflectionTestType', 'value')); $this->assertEquals(true, PPUtils::isPropertyArray('MockReflectionTestType', 'arrayMember')); $this->assertEquals(false, PPUtils::isPropertyArray('MockReflectionTestType', 'value')); }