Exemplo n.º 1
0
 /**
  * This method assigns any property values to the specified object.
  *
  * @access protected
  * @param \SimpleXMLElement $element                        a reference to the "object" node
  * @param mixed &$object                                    a reference to the object
  * @throws Throwable\Parse\Exception                        indicates that a problem occurred
  *                                                          when parsing
  */
 protected function getProperties(Spring\Object\Parser $parser, \SimpleXMLElement $element, &$object)
 {
     $class = new \ReflectionClass($object);
     $element->registerXPathNamespace('spring', Spring\Data\XML::NAMESPACE_URI);
     $fields = $element->xpath('./spring:property');
     foreach ($fields as $field) {
         $attributes = $field->attributes();
         if (!isset($attributes['name'])) {
             throw new Throwable\Parse\Exception('Unable to process Spring XML. Tag ":tag" is missing ":attribute" attribute.', array(':tag' => 'property', ':attribute' => 'name'));
         }
         $name = $parser->valueOf($attributes['name']);
         if (!$parser->isPropertyName($name)) {
             throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid property name, but got ":name".', array(':name' => $name));
         }
         $value = null;
         $children = $parser->getElementChildren($field, null);
         if (!empty($children)) {
             foreach ($children as $child) {
                 $value = $parser->getObjectFromElement($child);
             }
         } else {
             if (isset($attributes['expression'])) {
                 $expression = $parser->valueOf($attributes['expression']);
                 $value = null;
                 /*
                 @eval('$value = ' . $expression . ';');
                 if (isset($attributes['type'])) {
                 	$type = $parser->valueOf($attributes['type']);
                 	if (!$parser->isPrimitiveType($type)) {
                 		throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid primitive type, but got ":type".', array(':type' => $type));
                 	}
                 	if (!isset($value)) {
                 		$type = 'NULL';
                 		$value = null;
                 	}
                 	$value = Core\Convert::changeType($value, $type);
                 }
                 */
             } else {
                 if (isset($attributes['ref'])) {
                     $value = $parser->getObjectFromIdRef($parser->valueOf($attributes['ref']));
                 } else {
                     if (isset($attributes['value'])) {
                         $value = $parser->valueOf($attributes['value']);
                         if (isset($attributes['type'])) {
                             $type = $parser->valueOf($attributes['type']);
                             if (!$parser->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);
                         }
                     } else {
                         throw new Throwable\Parse\Exception('Unable to process Spring XML. Tag ":tag" is missing ":attribute" attribute.', array(':tag' => 'property', ':attribute' => 'value'));
                     }
                 }
             }
         }
         if ($class->hasProperty($name)) {
             $property = $class->getProperty($name);
             if (!$property->isPublic()) {
                 throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid property name, but got ":name".', array(':name' => $name));
             }
             $property->setValue($object, $value);
         } else {
             if ($object instanceof \stdClass) {
                 $object->{$name} = $value;
             } else {
                 if ($class->hasMethod('__set')) {
                     $method = $class->getMethod('__set');
                     if ($method->isAbstract() || !$method->isPublic()) {
                         throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid property name, but got ":name".', array(':name' => $name));
                     }
                     $method->invoke($object, $name, $value);
                 } else {
                     throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid property name, but got ":name" for class ":class".', array(':class' => get_class($object), ':name' => $name));
                 }
             }
         }
     }
 }