| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace LaravelUi5\OData\Edm\Annotation; |
| 6: | |
| 7: | use LaravelUi5\OData\Edm\Annotation\Annotation; |
| 8: | use LaravelUi5\OData\Edm\Contracts\Annotation\AnnotationInterface; |
| 9: | use LaravelUi5\OData\Edm\Contracts\Annotation\AnnotationValueInterface; |
| 10: | |
| 11: | /** |
| 12: | * Shared implementation for classes generated from OData vocabulary Terms. |
| 13: | * |
| 14: | * Every class generated by the vocabulary generator uses this trait. |
| 15: | * It provides: |
| 16: | * - getQualifier() — reads $this->qualifier declared by the using class |
| 17: | * - toAnnotation() — constructs an Annotation from TERM, qualifier, and value |
| 18: | * - buildAnnotationValue() (abstract) — each generated class maps its own typed |
| 19: | * constructor arguments to ConstantAnnotationValue, |
| 20: | * RecordAnnotationValue, or CollectionAnnotationValue |
| 21: | * |
| 22: | * The trait never inspects the using class's properties directly other than |
| 23: | * $qualifier. All typed property access is delegated to buildAnnotationValue(). |
| 24: | * |
| 25: | * @see TypedAnnotationInterface |
| 26: | */ |
| 27: | trait TypedAnnotationTrait |
| 28: | { |
| 29: | /** |
| 30: | * Returns the optional annotation qualifier, or null when absent. |
| 31: | * |
| 32: | * Reads $this->qualifier which every generated class declares as |
| 33: | * `public readonly ?string $qualifier = null` in its constructor. |
| 34: | */ |
| 35: | public function getQualifier(): ?string |
| 36: | { |
| 37: | return $this->qualifier; |
| 38: | } |
| 39: | |
| 40: | /** |
| 41: | * Converts this typed annotation into the generic AnnotationInterface |
| 42: | * used throughout the EDM contract layer. |
| 43: | * |
| 44: | * The term name is taken from the class constant TERM; the qualifier |
| 45: | * from getQualifier(); the value from buildAnnotationValue(). |
| 46: | */ |
| 47: | final public function toAnnotation(): AnnotationInterface |
| 48: | { |
| 49: | return new Annotation( |
| 50: | static::TERM, |
| 51: | $this->getQualifier(), |
| 52: | $this->buildAnnotationValue(), |
| 53: | ); |
| 54: | } |
| 55: | |
| 56: | /** |
| 57: | * Maps the typed constructor arguments of the using class to the |
| 58: | * appropriate annotation value object. |
| 59: | * |
| 60: | * Implementations return a ConstantAnnotationValue for primitive |
| 61: | * terms, a RecordAnnotationValue for record terms, a |
| 62: | * CollectionAnnotationValue for collection terms, and null for |
| 63: | * Boolean marker terms whose presence alone carries the meaning. |
| 64: | */ |
| 65: | abstract protected function buildAnnotationValue(): ?AnnotationValueInterface; |
| 66: | } |
| 67: |