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
|
|
|
use Fusion\Collection\Exceptions\CollectionException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Factory class to ease instantiation of collection and dictionary objects. |
18
|
|
|
* |
19
|
|
|
* @since 2.0.0 |
20
|
|
|
*/ |
21
|
|
|
class CollectionFactory |
22
|
|
|
{ |
23
|
89 |
|
private static function getValidator(): CollectionValidationInterface |
24
|
|
|
{ |
25
|
89 |
|
return new CollectionValidator(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Creates a new `Collection` with an optional set of starter items. |
30
|
|
|
* |
31
|
|
|
* @param array $starterItems |
32
|
|
|
* |
33
|
|
|
* @return Collection |
34
|
|
|
* |
35
|
|
|
* @throws CollectionException |
36
|
|
|
* @see \Fusion\Collection\Collection::__construct() |
37
|
|
|
* |
38
|
|
|
*/ |
39
|
43 |
|
public static function newCollection(array $starterItems = []): Collection |
40
|
|
|
{ |
41
|
43 |
|
return new Collection(self::getValidator(), $starterItems); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Creates a new `TypedCollection` with an optional set of starter items. |
46
|
|
|
* |
47
|
|
|
* @param string $acceptedType |
48
|
|
|
* @param array $starterItems |
49
|
|
|
* |
50
|
|
|
* @return TypedCollection |
51
|
|
|
* |
52
|
|
|
* @throws CollectionException |
53
|
|
|
* @see \Fusion\Collection\TypedCollection::__construct() |
54
|
|
|
* |
55
|
|
|
*/ |
56
|
14 |
|
public static function newTypedCollection(string $acceptedType, array $starterItems = []): TypedCollection |
57
|
|
|
{ |
58
|
14 |
|
return new TypedCollection(self::getValidator(), $acceptedType, $starterItems); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Creates a new `Dictionary` with an optional set of starter items. |
63
|
|
|
* |
64
|
|
|
* @param array $starterItems |
65
|
|
|
* |
66
|
|
|
* @return Dictionary |
67
|
|
|
* |
68
|
|
|
* @throws CollectionException |
69
|
|
|
* @see \Fusion\Collection\Dictionary::__construct() |
70
|
|
|
* |
71
|
|
|
*/ |
72
|
21 |
|
public static function newDictionary(array $starterItems = []): Dictionary |
73
|
|
|
{ |
74
|
21 |
|
return new Dictionary(self::getValidator(), $starterItems); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Creates a new `TypedDictionary` with an optional set of starter items. |
79
|
|
|
* |
80
|
|
|
* @param string $acceptedType |
81
|
|
|
* @param array $starterItems |
82
|
|
|
* |
83
|
|
|
* @return TypedDictionary |
84
|
|
|
* @throws CollectionException |
85
|
|
|
* @see \Fusion\Collection\TypedDictionary::__construct() |
86
|
|
|
* |
87
|
|
|
*/ |
88
|
11 |
|
public static function newTypedDictionary(string $acceptedType, array $starterItems = []): TypedDictionary |
89
|
|
|
{ |
90
|
11 |
|
return new TypedDictionary(self::getValidator(), $acceptedType, $starterItems); |
91
|
|
|
} |
92
|
|
|
} |