|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Part of the Fusion.Collection package. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Fusion\Collection; |
|
12
|
|
|
|
|
13
|
|
|
use Fusion\Collection\Contracts\CollectionValidationInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Factory class to ease instantiation of collection and dictionary objects. |
|
17
|
|
|
* |
|
18
|
|
|
* @since 2.0.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class CollectionFactory |
|
21
|
|
|
{ |
|
22
|
|
|
private static $cachedValidator; |
|
23
|
|
|
|
|
24
|
97 |
|
private static function getValidator(): CollectionValidationInterface |
|
25
|
|
|
{ |
|
26
|
97 |
|
if (self::$cachedValidator === null || (self::$cachedValidator instanceof CollectionValidationInterface) === false) |
|
27
|
|
|
{ |
|
28
|
1 |
|
self::$cachedValidator = new CollectionValidator(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
97 |
|
return self::$cachedValidator; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Creates a new `Collection` with an optional set of starter items. |
|
36
|
|
|
* |
|
37
|
|
|
* @see \Fusion\Collection\Collection::__construct() |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $starterItems |
|
40
|
|
|
* |
|
41
|
|
|
* @return \Fusion\Collection\Collection |
|
42
|
|
|
* |
|
43
|
|
|
* @throws \Fusion\Collection\Exceptions\CollectionException |
|
44
|
|
|
*/ |
|
45
|
50 |
|
public static function newCollection(array $starterItems = []): Collection |
|
46
|
|
|
{ |
|
47
|
50 |
|
return new Collection(self::getValidator(), $starterItems); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Creates a new `TypedCollection` with an optional set of starter items. |
|
52
|
|
|
* |
|
53
|
|
|
* @see \Fusion\Collection\TypedCollection::__construct() |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $acceptedType |
|
56
|
|
|
* @param array $starterItems |
|
57
|
|
|
* |
|
58
|
|
|
* @return \Fusion\Collection\TypedCollection |
|
59
|
|
|
* |
|
60
|
|
|
* @throws \Fusion\Collection\Exceptions\CollectionException |
|
61
|
|
|
*/ |
|
62
|
14 |
|
public static function newTypedCollection(string $acceptedType, array $starterItems = []): TypedCollection |
|
63
|
|
|
{ |
|
64
|
14 |
|
return new TypedCollection(self::getValidator(), $acceptedType, $starterItems); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Creates a new `Dictionary` with an optional set of starter items. |
|
69
|
|
|
* |
|
70
|
|
|
* @see \Fusion\Collection\Dictionary::__construct() |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $starterItems |
|
73
|
|
|
* |
|
74
|
|
|
* @return \Fusion\Collection\Dictionary |
|
75
|
|
|
* |
|
76
|
|
|
* @throws \Fusion\Collection\Exceptions\CollectionException |
|
77
|
|
|
*/ |
|
78
|
22 |
|
public static function newDictionary(array $starterItems = []): Dictionary |
|
79
|
|
|
{ |
|
80
|
22 |
|
return new Dictionary(self::getValidator(), $starterItems); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Creates a new `TypedDictionary` with an optional set of starter items. |
|
85
|
|
|
* |
|
86
|
|
|
* @see \Fusion\Collection\TypedDictionary::__construct() |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $acceptedType |
|
89
|
|
|
* @param array $starterItems |
|
90
|
|
|
* |
|
91
|
|
|
* @return \Fusion\Collection\TypedDictionary |
|
92
|
|
|
* @throws \Fusion\Collection\Exceptions\CollectionException |
|
93
|
|
|
*/ |
|
94
|
11 |
|
public static function newTypedDictionary(string $acceptedType, array $starterItems = []): TypedDictionary |
|
95
|
|
|
{ |
|
96
|
11 |
|
return new TypedDictionary(self::getValidator(), $acceptedType, $starterItems); |
|
97
|
|
|
} |
|
98
|
|
|
} |