MultipleAnnotation.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Di\Annotation;
  12. use Hyperf\Di\Exception\AnnotationException;
  13. class MultipleAnnotation implements MultipleAnnotationInterface
  14. {
  15. protected array $annotations = [];
  16. protected string $className;
  17. public function __construct(AnnotationInterface $annotation)
  18. {
  19. $this->annotations = [$annotation];
  20. $this->className = get_class($annotation);
  21. }
  22. public function __get(string $name)
  23. {
  24. if (count($this->annotations) > 1) {
  25. throw new AnnotationException('MultipleAnnotation[' . $this->className() . '] has more than one annotations.');
  26. }
  27. return $this->annotations[0]->{$name};
  28. }
  29. public function className(): string
  30. {
  31. return $this->className;
  32. }
  33. public function insert(AnnotationInterface $annotation): static
  34. {
  35. if (! $annotation instanceof $this->className) {
  36. throw new AnnotationException(get_class($annotation) . ' must instanceof ' . $this->className);
  37. }
  38. $this->annotations[] = $annotation;
  39. return $this;
  40. }
  41. public function toAnnotations(): array
  42. {
  43. return $this->annotations;
  44. }
  45. public function collectClass(string $className): void
  46. {
  47. throw new AnnotationException('MultipleAnnotation[' . $this->className() . '] does not support collectClass()');
  48. }
  49. public function collectClassConstant(string $className, ?string $target): void
  50. {
  51. throw new AnnotationException('MultipleAnnotation[' . $this->className() . '] does not support collectClassConstant()');
  52. }
  53. public function collectMethod(string $className, ?string $target): void
  54. {
  55. throw new AnnotationException('MultipleAnnotation[' . $this->className() . '] does not support collectMethod()');
  56. }
  57. public function collectProperty(string $className, ?string $target): void
  58. {
  59. throw new AnnotationException('MultipleAnnotation[' . $this->className() . '] does not support collectProperty()');
  60. }
  61. }