/** * Creates a new object of the given type, using the optional parameters. * When pseudo-namespace support is enabled class names can become very long, and this function * provides an alternative way to create objects that is more readable. * * @param string $type the type of object to create * @param array $params parameters to pass into the constructor, as either * flat array in the correct order for the constructor or as an associative array from parameter name to value * @return mixed a new instance of a class that represents that type */ public function Create($type, $params = null) { if (array_key_exists($type, $this->options['classmap'])) { $class = $this->options['classmap'][$type]; $reflectionClass = new \ReflectionClass($class); if (isset($params)) { if (MapUtils::IsMap($params)) { $params = MapUtils::MapToMethodParameters($params, $reflectionClass->getConstructor()); } return $reflectionClass->newInstanceArgs($params); } else { return $reflectionClass->newInstance(); } } else { trigger_error('Unknown type: ' . $type, E_USER_ERROR); } }
/** * Converts an object to an DOMElement. * * @param Object $object the object to serialize * @param string $elementName the name of the element to serialize * @param \DOMDocument $document the document that the element will be added to * @return DOMElement the element representing the object */ private static function ConvertObjectToElement($object, $elementName, $document) { if (!isset($object)) { return null; } $element = $document->createElement($elementName); $children = array(); if (is_array($object) && MapUtils::IsMap($object)) { $object = (object) $object; } if (is_object($object)) { foreach (get_object_vars($object) as $field => $value) { if (is_array($value) && !MapUtils::IsMap($value)) { foreach ($value as $item) { $children[] = self::ConvertObjectToElement($item, $field, $document); } } else { $children[] = self::ConvertObjectToElement($value, $field, $document); } } foreach ($children as $child) { if (isset($child)) { $element->appendChild($child); } } } else { $element->nodeValue = self::ConvertObjectToNodeValue($object); } return $element; }