1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebTheory\Collection\Kernel\Factory; |
4
|
|
|
|
5
|
|
|
use WebTheory\Collection\Comparison\PropertyBasedCollectionComparator; |
6
|
|
|
use WebTheory\Collection\Comparison\PropertyBasedObjectComparator; |
7
|
|
|
use WebTheory\Collection\Comparison\RuntimeIdBasedCollectionComparator; |
8
|
|
|
use WebTheory\Collection\Comparison\RuntimeIdBasedObjectComparator; |
9
|
|
|
use WebTheory\Collection\Contracts\ArrayDriverInterface; |
10
|
|
|
use WebTheory\Collection\Contracts\CollectionComparatorInterface; |
11
|
|
|
use WebTheory\Collection\Contracts\ObjectComparatorInterface; |
12
|
|
|
use WebTheory\Collection\Contracts\PropertyResolverInterface; |
13
|
|
|
use WebTheory\Collection\Driver\AutoKeyedMap; |
14
|
|
|
use WebTheory\Collection\Driver\IdentifiableItemList; |
15
|
|
|
use WebTheory\Collection\Driver\StandardList; |
16
|
|
|
use WebTheory\Collection\Resolution\PropertyResolver; |
17
|
|
|
|
18
|
|
|
class CollectionKernelSubsystemFactory |
19
|
|
|
{ |
20
|
|
|
protected ?string $identifier; |
21
|
|
|
|
22
|
|
|
protected array $accessors; |
23
|
|
|
|
24
|
|
|
protected bool $mapToIdentifier; |
25
|
|
|
|
26
|
|
|
protected PropertyResolverInterface $propertyResolver; |
27
|
|
|
|
28
|
|
|
protected ObjectComparatorInterface $objectComparator; |
29
|
|
|
|
30
|
216 |
|
public function __construct(?string $identifier, array $accessors, bool $mapToIdentifier) |
31
|
|
|
{ |
32
|
216 |
|
$this->identifier = $identifier; |
33
|
216 |
|
$this->accessors = $accessors; |
34
|
216 |
|
$this->mapToIdentifier = $mapToIdentifier; |
35
|
|
|
|
36
|
216 |
|
$this->propertyResolver = new PropertyResolver($accessors); |
37
|
|
|
|
38
|
216 |
|
$this->objectComparator = $identifier |
39
|
216 |
|
? new PropertyBasedObjectComparator($this->propertyResolver, $identifier) |
40
|
75 |
|
: new RuntimeIdBasedObjectComparator(); |
41
|
|
|
} |
42
|
|
|
|
43
|
216 |
|
public function getPropertyResolver(): PropertyResolverInterface |
44
|
|
|
{ |
45
|
216 |
|
return $this->propertyResolver; |
46
|
|
|
} |
47
|
|
|
|
48
|
216 |
|
public function getCollectionComparator(): CollectionComparatorInterface |
49
|
|
|
{ |
50
|
216 |
|
return $this->identifier |
51
|
216 |
|
? new PropertyBasedCollectionComparator($this->propertyResolver, $this->identifier) |
52
|
216 |
|
: new RuntimeIdBasedCollectionComparator(); |
53
|
|
|
} |
54
|
|
|
|
55
|
216 |
|
public function getArrayDriver(): ArrayDriverInterface |
56
|
|
|
{ |
57
|
216 |
|
if ($this->identifier && $this->mapToIdentifier) { |
58
|
33 |
|
$driver = new AutoKeyedMap( |
59
|
33 |
|
$this->identifier, |
60
|
33 |
|
$this->propertyResolver, |
61
|
33 |
|
$this->objectComparator |
62
|
|
|
); |
63
|
216 |
|
} elseif ($this->identifier) { |
64
|
216 |
|
$driver = new IdentifiableItemList( |
65
|
216 |
|
$this->identifier, |
66
|
216 |
|
$this->propertyResolver, |
67
|
216 |
|
$this->objectComparator |
68
|
|
|
); |
69
|
|
|
} else { |
70
|
75 |
|
$driver = new StandardList($this->objectComparator); |
71
|
|
|
} |
72
|
|
|
|
73
|
216 |
|
return $driver; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|