Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
10 / 10 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
TreenodeFactory | |
100.00% |
10 / 10 |
|
100.00% |
5 / 5 |
7 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isInitialized | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
initialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getTreenodeCollectionFactory | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
makeNode | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * @author: Doug Wilbourne (dougwilbourne@gmail.com) |
5 | */ |
6 | declare(strict_types=1); |
7 | |
8 | namespace pvc\struct\tree\node; |
9 | |
10 | use pvc\interfaces\struct\tree\node\TreenodeCollectionFactoryInterface; |
11 | use pvc\interfaces\struct\tree\node\TreenodeFactoryInterface; |
12 | use pvc\interfaces\struct\tree\node\TreenodeInterface; |
13 | use pvc\interfaces\struct\tree\tree\TreeInterface; |
14 | use pvc\interfaces\validator\ValTesterInterface; |
15 | use pvc\struct\tree\err\ChildCollectionException; |
16 | use pvc\struct\tree\err\TreenodeFactoryNotInitializedException; |
17 | |
18 | /** |
19 | * Class TreenodeFactory |
20 | * @template PayloadType |
21 | * @implements TreenodeFactoryInterface<PayloadType> |
22 | * |
23 | * Tree and TreenodeFactory are mutually dependent. The constructors are set up so that you create |
24 | * TreenodeFactory first without its tree dependency, use TreenodeFactory in the construction of a new tree, |
25 | * and then go back and set the tree property in TreenodeFactory. Tree factory does all this in the method |
26 | * makeTree. |
27 | */ |
28 | class TreenodeFactory implements TreenodeFactoryInterface |
29 | { |
30 | /** |
31 | * @var TreeInterface<PayloadType> |
32 | */ |
33 | protected TreeInterface $tree; |
34 | |
35 | /** |
36 | * @param TreenodeCollectionFactoryInterface<PayloadType> $treenodeCollectionFactory |
37 | * @param ValTesterInterface<PayloadType>|null $payloadTester |
38 | */ |
39 | public function __construct( |
40 | protected TreenodeCollectionFactoryInterface $treenodeCollectionFactory, |
41 | protected ?ValTesterInterface $payloadTester = null |
42 | ) { |
43 | } |
44 | |
45 | public function isInitialized(): bool |
46 | { |
47 | return isset($this->tree); |
48 | } |
49 | |
50 | /** |
51 | * @param TreeInterface<PayloadType> $tree |
52 | * @return void |
53 | */ |
54 | public function initialize(TreeInterface $tree): void |
55 | { |
56 | $this->tree = $tree; |
57 | } |
58 | |
59 | /** |
60 | * @return TreenodeCollectionFactoryInterface<PayloadType> |
61 | */ |
62 | public function getTreenodeCollectionFactory(): TreenodeCollectionFactoryInterface |
63 | { |
64 | if (!$this->isInitialized()) { |
65 | throw new TreeNodeFactoryNotInitializedException(); |
66 | } |
67 | return $this->treenodeCollectionFactory; |
68 | } |
69 | |
70 | /** |
71 | * @return TreenodeInterface<PayloadType> |
72 | * @throws ChildCollectionException|TreenodeFactoryNotInitializedException |
73 | */ |
74 | public function makeNode(): TreenodeInterface |
75 | { |
76 | if (!$this->isInitialized()) { |
77 | throw new TreeNodeFactoryNotInitializedException(); |
78 | } |
79 | /** @var TreenodeCollection<covariant PayloadType> $treenodeCollection */ |
80 | $treenodeCollection = $this->treenodeCollectionFactory->makeTreenodeCollection(); |
81 | return new Treenode($treenodeCollection, $this->tree, $this->payloadTester); |
82 | } |
83 | } |