cycle /
annotated
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Cycle\Annotated\Annotation\Relation; |
||
| 6 | |||
| 7 | use Cycle\ORM\Relation; |
||
|
0 ignored issues
–
show
|
|||
| 8 | use Doctrine\Common\Annotations\Annotation\Enum; |
||
| 9 | use JetBrains\PhpStorm\ExpectedValues; |
||
| 10 | use Spiral\Attributes\NamedArgumentConstructor; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @Annotation |
||
| 14 | * @NamedArgumentConstructor |
||
| 15 | * @Target({"PROPERTY", "ANNOTATION"}) |
||
| 16 | */ |
||
| 17 | #[\Attribute(\Attribute::TARGET_PROPERTY), NamedArgumentConstructor] |
||
| 18 | final class Inverse |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @param non-empty-string $as Columns name that will represent relation |
||
|
0 ignored issues
–
show
|
|||
| 22 | * @param non-empty-string $type Relation type. |
||
| 23 | * @param int|non-empty-string|null $load Relation load approach. |
||
| 24 | */ |
||
| 25 | 144 | public function __construct( |
|
| 26 | private string $as, |
||
| 27 | /** |
||
| 28 | * @Enum({"hasOne", "belongsTo", "embedded", "hasMany", "manyToMany", "refersTo"} |
||
| 29 | */ |
||
| 30 | #[ExpectedValues(values: ['hasOne', 'belongsTo', 'embedded', 'hasMany', 'manyToMany', 'refersTo'])] |
||
| 31 | private string $type, |
||
| 32 | /** |
||
| 33 | * @Enum({"eager", "lazy", "promise"} |
||
| 34 | */ |
||
| 35 | #[ExpectedValues(values: ['eager', 'lazy', 'promise', Relation::LOAD_EAGER, Relation::LOAD_PROMISE])] |
||
| 36 | private string|int|null $load = null, |
||
| 37 | ) { |
||
| 38 | 144 | } |
|
| 39 | |||
| 40 | 144 | public function getType(): string |
|
| 41 | { |
||
| 42 | 144 | return $this->type; |
|
| 43 | } |
||
| 44 | |||
| 45 | 144 | public function getName(): string |
|
| 46 | { |
||
| 47 | 144 | return $this->as; |
|
| 48 | } |
||
| 49 | |||
| 50 | 144 | public function getLoadMethod(): ?int |
|
| 51 | { |
||
| 52 | 144 | return match ($this->load) { |
|
| 53 | 'eager', Relation::LOAD_EAGER => Relation::LOAD_EAGER, |
||
| 54 | 12 | 'promise', 'lazy', Relation::LOAD_PROMISE => Relation::LOAD_PROMISE, |
|
| 55 | 144 | default => null |
|
| 56 | }; |
||
| 57 | } |
||
| 58 | } |
||
| 59 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: