Beispiel #1
0
 /**
  * @param Reflector $refl
  * @param object $context
  */
 public function run(Reflector $refl, $context)
 {
     if ($refl instanceof \ReflectionProperty) {
         $type = Helper::getPropertyType($refl);
         $isPrimitiveType = in_array($type, Constants::$php_default_types) || in_array($type, Constants::$php_pseudo_types);
         if (!($refl instanceof ReflectionProperty || method_exists($refl, 'getAnnotation'))) {
             $refl = new ReflectionProperty($refl->getDeclaringClass()->getName(), $refl->getName());
         }
         $serviceName = ($qualifier = $refl->getAnnotation(Qualifier::class)) ? $qualifier->value : null;
         if (($type === null || $isPrimitiveType) && $serviceName === null) {
             throw new RuntimeException("Must set the property type by @var annotation or you must use @Quealifier annotation to define the service");
         }
         if (empty($serviceName)) {
             $serviceName = $type;
         }
         $service = BeanFactory::getInstance()->getBean($serviceName);
         $refl->setAccessible(true);
         $refl->setValue($context, $service);
     }
 }
Beispiel #2
0
 public function run(Reflector $refl, $context)
 {
     if ($refl instanceof ReflectionProperty) {
         $annotation = Helper::getAnnotation($refl, Config::class);
         $path = explode('.', $annotation->value);
         $config = $this->config;
         while (!empty($path)) {
             $config = $config->{array_shift($path)};
         }
         $type = Helper::getPropertyType($refl);
         $isPrimitiveType = in_array($type, Constants::$php_default_types) || in_array($type, Constants::$php_pseudo_types);
         $refl->setAccessible(true);
         if ($type !== null && $isPrimitiveType) {
             if (gettype($config) === $type) {
                 $refl->setValue($context, $config);
             } elseif ($type === 'array' && method_exists($config, 'toArray')) {
                 $refl->setValue($context, $config->toArray());
             }
         } else {
             $refl->setValue($context, $config);
         }
     }
 }
 private function fillForm($form, $request = null)
 {
     $class = new ReflectionClass($form);
     /* @var $property ReflectionProperty */
     foreach ($class->getProperties() as $property) {
         $value = array_key_exists($property->getName(), (array) $request) ? $request[$property->getName()] : null;
         $type = Helper::getPropertyType($property);
         if (!$this->isPrimitive($type)) {
             $value = $this->fillForm(ClassInvoker::getNewInstance($type), $value);
         }
         $this->setFormFieldValue($property, $form, $value);
     }
     return $form;
 }