/** * This method retypes any objects matching either the type mappings or name mappings. * * @access public * @static * @param \SimpleXMLElement $xml the Spring XML to be processed * @param array $mappings the type and name mappings */ public static function retype(\SimpleXMLElement $xml, array $mappings) { if (!empty($mappings)) { $xml->registerXPathNamespace('spring', Spring\Data\XML::NAMESPACE_URI); $objects = $xml->xpath('//spring:object'); array_walk($objects, function (\SimpleXMLElement &$object) use($mappings) { $attributes = $object->attributes(); if (isset($attributes['type'])) { $type = Spring\Data\XML::valueOf($attributes['type']); if (isset($mappings['types'][$type])) { $attributes['type'] = $mappings['types'][$type]; } } if (isset($attributes['name'])) { $names = preg_split('/(,|;|\\s)+/', Spring\Data\XML::valueOf($attributes['name'])); foreach ($names as $name) { if (isset($mappings['ids'][$name])) { $attributes['type'] = $mappings['ids'][$name]; } } } if (isset($attributes['id'])) { $id = Spring\Data\XML::valueOf($attributes['id']); if (isset($mappings['ids'][$id])) { $attributes['type'] = $mappings['ids'][$id]; } } }); } }
/** * This method parses a "value" node. * * @access protected * @param \SimpleXMLElement $root a reference to the root node * @param \SimpleXMLElement $node a reference to the "value" node * @return mixed the value * @throws Throwable\Parse\Exception indicates that problem occurred while * parsing */ protected function parseValueElement(\SimpleXMLElement $root, \SimpleXMLElement $node) { $children = $node->children(); if (count($children) > 0) { $value = ''; foreach ($children as $child) { $name = $child->getName(); switch ($name) { case 'null': $value = $this->parseNullElement($root, $child); break; default: throw new Throwable\Parse\Exception('Unable to process Spring XML. Tag ":tag" has invalid child node ":child".', array(':tag' => 'value', ':child' => $name)); break; } } return $value; } else { $attributes = $node->attributes(); $value = dom_import_simplexml($node)->textContent; if (isset($attributes['type'])) { $type = Spring\Data\XML::valueOf($attributes['type']); if (!Spring\Data\XML\Syntax::isPrimitiveType($type)) { throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid primitive type, but got ":type".', array(':type' => $type)); } $value = Core\Convert::changeType($value, $type); } if (is_string($value)) { $attributes = $node->attributes('xml', true); if (isset($attributes['space'])) { $space = Spring\Data\XML::valueOf($attributes['space']); if (!Spring\Data\XML\Syntax::isSpacePreserved($space)) { throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid "space" token, but got ":token".', array(':token' => $space)); } } else { $value = trim($value); } } return $value; } }
/** * This method evaluates whether the specified string is a valid idref. * * @access public * @static * @param string $token the string to be evaluated * @param \SimpleXMLElement $resource the resource to query * @return boolean whether the specified string is a valid * idref * @throws Throwable\InvalidArgument\Exception indicates that an argument is incorrect * * @see http://stackoverflow.com/questions/1257867/regular-expressions-and-xpath-query */ public static function isIdref($token, \SimpleXMLElement $resource = null) { if ($resource !== null) { if (!static::isId($token)) { throw new Throwable\InvalidArgument\Exception('Invalid argument detected (id: :id).', array(':id' => $token)); } $resource->registerXPathNamespace('spring', Spring\Data\XML::NAMESPACE_URI); $nodes = $resource->xpath("/spring:objects/spring:object[@id='{$token}' or contains(@name,'{$token}')]"); $nodes = array_filter($nodes, function (\SimpleXMLElement &$node) use($token) { $attributes = $node->attributes(); return isset($attributes['id']) && Spring\Data\XML::valueOf($attributes['id']) == $token || isset($attributes['name']) && in_array($token, preg_split('/(,|;|\\s)+/', Spring\Data\XML::valueOf($attributes['name']))); }); return !empty($nodes); } return static::isId($token); }
/** * This method returns the first value associated with the specified object. * * @access public * @param mixed $value the object to be processed * @param string $source_encoding the source encoding * @param string $target_encoding the target encoding * @return mixed the value that was wrapped by * the object */ public function valueOf($value, $source_encoding = 'UTF-8', $target_encoding = 'UTF-8') { return Spring\Data\XML::valueOf($value, $source_encoding, $target_encoding); }