Stratadox /
TableLoader
| 1 | <?php |
||||
| 2 | declare(strict_types=1); |
||||
| 3 | |||||
| 4 | namespace Stratadox\TableLoader\Loader; |
||||
| 5 | |||||
| 6 | use Stratadox\TableLoader\Loader\ContainsResultingObjects as Result; |
||||
|
0 ignored issues
–
show
|
|||||
| 7 | |||||
| 8 | /** |
||||
| 9 | * Wires a relationship together. |
||||
| 10 | * |
||||
| 11 | * @author Stratadox |
||||
| 12 | */ |
||||
| 13 | final class Wire implements WiresObjects |
||||
| 14 | { |
||||
| 15 | private $from; |
||||
| 16 | private $to; |
||||
| 17 | private $relation; |
||||
| 18 | private $setter; |
||||
| 19 | |||||
| 20 | private function __construct( |
||||
| 21 | KnowsWhereToLookFrom $from, |
||||
| 22 | KnowsWhereToLook $to, |
||||
| 23 | MakesConnections $relation |
||||
| 24 | ) { |
||||
| 25 | $this->from = $from; |
||||
| 26 | $this->to = $to; |
||||
| 27 | $this->relation = $relation; |
||||
| 28 | // @todo use hydrator! |
||||
| 29 | $this->setter = function (string $property, $value): void { |
||||
| 30 | $this->$property = $value; |
||||
| 31 | }; |
||||
| 32 | } |
||||
| 33 | |||||
| 34 | /** |
||||
| 35 | * Makes a new object that wires a relationship together. |
||||
| 36 | * |
||||
| 37 | * @param KnowsWhereToLookFrom $from Identification for the source entity. |
||||
| 38 | * @param KnowsWhereToLook $to Identification for the target entity. |
||||
| 39 | * @param MakesConnections $relation The type of relationship. |
||||
| 40 | * @return WiresObjects The wiring object. |
||||
| 41 | */ |
||||
| 42 | public static function it( |
||||
| 43 | KnowsWhereToLookFrom $from, |
||||
| 44 | KnowsWhereToLook $to, |
||||
| 45 | MakesConnections $relation |
||||
| 46 | ): WiresObjects { |
||||
| 47 | return new Wire($from, $to, $relation); |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | /** @inheritdoc */ |
||||
| 51 | public function wire(Result $objects, array $data): void |
||||
| 52 | { |
||||
| 53 | $relationships = $this->relation->load( |
||||
| 54 | $this->from, |
||||
| 55 | $data, |
||||
| 56 | $this->to, |
||||
| 57 | $objects |
||||
| 58 | ); |
||||
| 59 | foreach ($relationships as $property => $relations) { |
||||
| 60 | $this->writeTo($objects[$this->from->label()], $property, $relations); |
||||
|
0 ignored issues
–
show
$objects[$this->from->label()] of type iterable is incompatible with the type array expected by parameter $objects of Stratadox\TableLoader\Loader\Wire::writeTo().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 61 | } |
||||
| 62 | } |
||||
| 63 | |||||
| 64 | private function writeTo( |
||||
| 65 | array $objects, |
||||
| 66 | string $property, |
||||
| 67 | array $relations |
||||
| 68 | ): void { |
||||
| 69 | foreach ($relations as $id => $relation) { |
||||
| 70 | if ($this->from->hereToo($objects[$id])) { |
||||
| 71 | $this->setter->call($objects[$id], $property, $relation); |
||||
| 72 | } |
||||
| 73 | } |
||||
| 74 | } |
||||
| 75 | } |
||||
| 76 |
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: