/** * @param string|object $object Class name or object instance * * @return ObjectValidator */ public function getObjectValidator($object) { $validator = new ObjectValidator(); $classComment = $this->annotationReader->readClass($object); foreach ($classComment->getAnnotations() as $class => $annotations) { foreach ($annotations as $annotation) { if ($annotation instanceof Rule) { $validator->addRule($annotation); } } } $methods = $this->annotationReader->readMethods($object); foreach ($methods as $methodName => $comment) { /** @var Comment $comment */ foreach ($comment->getAnnotations() as $class => $annotations) { foreach ($annotations as $annotation) { if ($annotation instanceof Rule) { $validator->addMethodRule($methodName, $annotation); } } } } $properties = $this->annotationReader->readProperties($object); foreach ($properties as $propertyName => $comment) { /** @var Comment $comment */ foreach ($comment->getAnnotations() as $class => $annotations) { foreach ($annotations as $annotation) { if ($annotation instanceof Rule) { $validator->addPropertyRule($propertyName, $annotation); } } } } return $validator; }
/** * @param $class * @param bool $isParent * * @throws AnnotationException * * @return AnnotationMetadata */ private function readClassMetadata($class, $isParent = false) { if (!isset($this->annotations[$class])) { $reflector = $this->getClassReflector($class); $metadata = $this->createAnnotationMetadataInstance($reflector); $comment = $this->reader->readClass($class); if (!$comment->has('Annotation')) { if ($isParent) { return $metadata; } throw new AnnotationException("Class {$class} has not been marked with @Annotation"); } $this->collectAttributeMetadata($comment, $metadata); $this->collectTargetMetadata($comment, $metadata); $this->collectDefaultAttributeMetadata($comment, $metadata); $this->getConstructorInfo($reflector, $metadata); $this->annotations[$class] = $metadata; } return $this->annotations[$class]; }
public function testReadMethods() { $result = $this->object->readMethods(TestClass::class); $this->assertEquals(['method'], array_keys($result)); }