1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebTheory\Collection\Kernel\Builder; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use WebTheory\Collection\Contracts\JsonSerializerInterface; |
7
|
|
|
use WebTheory\Collection\Kernel\CollectionKernel; |
8
|
|
|
use WebTheory\Collection\Resolution\PropertyResolver; |
9
|
|
|
|
10
|
|
|
class CollectionKernelBuilder |
11
|
|
|
{ |
12
|
|
|
protected array $items = []; |
13
|
|
|
|
14
|
|
|
protected Closure $factory; |
15
|
|
|
|
16
|
|
|
protected array $accessors = []; |
17
|
|
|
|
18
|
|
|
protected ?string $identifier = null; |
19
|
|
|
|
20
|
|
|
protected bool $mapToIdentifier = false; |
21
|
|
|
|
22
|
|
|
protected JsonSerializerInterface $jsonSerializer; |
23
|
|
|
|
24
|
|
|
protected PropertyResolver $propertyResolver; |
25
|
|
|
|
26
|
|
|
public function withItems(array $items): self |
27
|
|
|
{ |
28
|
|
|
$this->items = $items; |
29
|
|
|
|
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function withFactory(callable $factory): self |
34
|
|
|
{ |
35
|
|
|
$this->factory = $factory; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function withAccessors(array $accessors): self |
41
|
|
|
{ |
42
|
|
|
$this->accessors = $accessors; |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function withIdentifier(string $identifier): self |
48
|
|
|
{ |
49
|
|
|
$this->identifier = $identifier; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function withMapped(bool $map): self |
55
|
|
|
{ |
56
|
|
|
$this->mapToIdentifier = $map; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function withJsonSerializer(JsonSerializerInterface $jsonSerializer): self |
62
|
|
|
{ |
63
|
|
|
$this->jsonSerializer = $jsonSerializer; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function withPropertyResolver(PropertyResolver $propertyResolver): self |
69
|
|
|
{ |
70
|
|
|
$this->propertyResolver = $propertyResolver; |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function build(): CollectionKernel |
76
|
|
|
{ |
77
|
|
|
return new CollectionKernel( |
78
|
|
|
$this->items, |
79
|
|
|
$this->factory, |
80
|
|
|
$this->identifier, |
81
|
|
|
$this->accessors, |
82
|
|
|
$this->mapToIdentifier, |
83
|
|
|
$this->jsonSerializer, |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|