public function publicModifier()
 {
     $this->assertTrue(\lang\reflect\Modifiers::isPublic(MODIFIER_PUBLIC));
 }
예제 #2
0
 /**
  * Convert value based on type
  *
  * @param   lang.Type type
  * @param   [:var] value
  * @return  var
  */
 public function unmarshal($type, $value)
 {
     if (null === $type || $type->equals(Type::$VAR)) {
         // No conversion
         return $value;
     } else {
         if (null === $value) {
             // Valid for any type
             return null;
         } else {
             if ($type instanceof Primitive) {
                 return $type->cast($this->valueOf($value));
             } else {
                 if ($type->equals(XPClass::forName('util.Date'))) {
                     return $type->newInstance($value);
                 } else {
                     if ($type instanceof XPClass) {
                         if ($type->isInstance($value)) {
                             return $value;
                         }
                         foreach ($this->marshallers->keys() as $t) {
                             if ($t->isAssignableFrom($type)) {
                                 return $this->marshallers[$t]->unmarshal($type, $value, $this);
                             }
                         }
                         // Check if a public static valueOf() method exists
                         if ($type->hasMethod('valueOf')) {
                             $valueOf = $type->getMethod('valueOf');
                             if (Modifiers::isStatic($valueOf->getModifiers()) && Modifiers::isPublic($valueOf->getModifiers())) {
                                 if (1 === $valueOf->numParameters()) {
                                     return $valueOf->invoke(null, [$this->unmarshal($this->paramType($valueOf->getParameter(0)), $value)]);
                                 } else {
                                     $param = 0;
                                     $args = [];
                                     foreach ($this->iterableOf($value) as $value) {
                                         $args[] = $this->unmarshal($this->paramType($valueOf->getParameter($param++)), $value);
                                     }
                                     return $valueOf->invoke(null, $args);
                                 }
                             }
                         }
                         // Generic approach
                         $return = $type->newInstance();
                         foreach ($this->iterableOf($value) as $name => $value) {
                             foreach ($this->variantsOf($name) as $variant) {
                                 if ($type->hasField($variant)) {
                                     $field = $type->getField($variant);
                                     $m = $field->getModifiers();
                                     if ($m & MODIFIER_STATIC) {
                                         continue;
                                     } else {
                                         if ($m & MODIFIER_PUBLIC) {
                                             if (null !== ($fType = $field->getType())) {
                                                 $field->set($return, $this->unmarshal($fType, $value));
                                             } else {
                                                 $field->set($return, $value);
                                             }
                                             continue 2;
                                         }
                                     }
                                 }
                                 if ($type->hasMethod('set' . $variant)) {
                                     $method = $type->getMethod('set' . $variant);
                                     if ($method->getModifiers() & MODIFIER_PUBLIC) {
                                         if (null !== ($param = $method->getParameter(0))) {
                                             $method->invoke($return, [$this->unmarshal($param->getType(), $value)]);
                                         } else {
                                             $method->invoke($return, [$value]);
                                         }
                                         continue 2;
                                     }
                                 }
                             }
                         }
                         return $return;
                     } else {
                         if ($type instanceof \lang\ArrayType) {
                             $return = [];
                             foreach ($this->iterableOf($value) as $element) {
                                 $return[] = $this->unmarshal($type->componentType(), $element);
                             }
                             return $return;
                         } else {
                             if ($type instanceof \lang\MapType) {
                                 $return = [];
                                 foreach ($this->iterableOf($value) as $key => $element) {
                                     $return[$key] = $this->unmarshal($type->componentType(), $element);
                                 }
                                 return $return;
                             } else {
                                 if ($type->equals(Type::$ARRAY)) {
                                     $return = [];
                                     foreach ($this->iterableOf($value) as $key => $element) {
                                         $return[$key] = $element;
                                     }
                                     return $return;
                                 } else {
                                     throw new \lang\FormatException('Cannot convert to ' . \xp::stringOf($type));
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
예제 #3
0
 public function publicModifier()
 {
     $this->assertTrue(Modifiers::isPublic(MODIFIER_PUBLIC));
 }