/** * @param string $hint * @param \ReflectionClass $class * @return Type */ public function fromTypeHint($hint, \ReflectionClass $class) { $resolver = new ClassResolver($class); if (strtolower(substr($hint, -3)) == '-id') { $target = $resolver->resolve(substr($hint, 0, -3)); return new IdentifierType($target, new StringType()); } else { if (strpos($hint, '::') && substr($hint, -1) == '*') { list($container, $constant) = explode('::', substr($hint, 0, -1)); if ($container == 'self') { $reflection = $class; } else { $reflection = new \ReflectionClass($resolver->resolve($container)); } $options = []; foreach ($reflection->getConstants() as $name => $value) { if (substr($name, 0, strlen($constant)) == $constant) { $options[$value] = ucfirst($value); } } return new EnumerationType($options, new StringType()); } else { if (preg_match('#\\[\\d+;\\d+(;\\d+)?\\]#', $hint)) { $parts = explode(';', substr($hint, 1, -1)); $min = array_shift($parts); $max = array_shift($parts); $step = $parts ? array_shift($parts) : 1; return new RangeType($min, $max, $step); } } } return parent::fromTypeHint($hint, $class); }
/** * @param string $hint * @param \ReflectionClass $class * @return Type */ public function fromTypeHint($hint, \ReflectionClass $class) { $resolver = new ClassResolver($class); if (strtolower(substr($hint, -3)) == '-id') { $target = $resolver->resolve(substr($hint, 0, -3)); return new IdentifierType($target, new StringType()); } else { if (strpos($hint, '::') && substr($hint, -1) == '*') { list($container, $constant) = explode('::', substr($hint, 0, -1)); if ($container == 'self') { $reflection = $class; } else { $reflection = new \ReflectionClass($resolver->resolve($container)); } $options = []; foreach ($reflection->getConstants() as $name => $value) { if (substr($name, 0, strlen($constant)) == $constant) { $options[$value] = ucfirst($value); } } return new EnumerationType($options, new StringType()); } } return parent::fromTypeHint($hint, $class); }
/** * @param \ReflectionParameter $param * @return null|string */ public function getTypeHint(\ReflectionParameter $param) { if ($param->getClass()) { return $param->getClass()->getName(); } $type = $this->match('/@param\\s+(\\S+)\\s+\\$' . $param->getName() . '/'); if (!$type) { return null; } $resolver = new ClassResolver($this->method->getDeclaringClass()); $resolved = $resolver->resolve($type); return $resolved ?: $type; }
private function injectProperty($targetObject, $propertyName, ClassResolver $resolver, $class) { $type = $resolver->resolve($class); $classReflection = new \ReflectionClass($targetObject); if (!$type) { if ($this->throwException) { throw new InjectionException("Could not find [{$class}]."); } else { return; } } $instance = call_user_func($this->injector, $type); if ($classReflection->hasProperty($propertyName)) { $reflectionProperty = $classReflection->getProperty($propertyName); $reflectionProperty->setAccessible(true); if ($reflectionProperty->getValue($targetObject) === null) { $reflectionProperty->setValue($targetObject, $instance); } } else { $targetObject->{$propertyName} = $instance; } }
/** * @param string $hint * @param \ReflectionClass $class * @return Type */ protected function resolveClassHint($hint, \ReflectionClass $class) { $resolver = new ClassResolver($class); $resolved = $resolver->resolve($hint); if (!$resolved) { return new type\UnknownType($hint); } return new type\ClassType($resolved); }