1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace dlindberg\BlobChunk; |
6
|
|
|
|
7
|
|
|
use dlindberg\BlobChunk\Manager\CheckNode; |
8
|
|
|
use dlindberg\BlobChunk\Manager\Manager; |
9
|
|
|
use dlindberg\BlobChunk\Manager\NodeCheck; |
10
|
|
|
use dlindberg\BlobChunk\Parser\Parse; |
11
|
|
|
use dlindberg\BlobChunk\Parser\Parser; |
12
|
|
|
use dlindberg\DOMDocumentFactory\DOMDocumentFactory; |
13
|
|
|
use dlindberg\BlobChunk\Manager\Config as ManagerConfig; |
14
|
|
|
|
15
|
|
|
class Config |
16
|
|
|
{ |
17
|
|
|
private static $default = [ |
18
|
|
|
'parents' => ['tags' => ['div'],], |
19
|
|
|
'specials' => ['tags' => ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'strong', 'em', 'b', 'i', 'a'],], |
20
|
|
|
'rowCol' => [ |
21
|
|
|
[ |
22
|
|
|
'parent' => ['type' => 'tag', 'value' => 'table',], |
23
|
|
|
'colDefs' => ['type' => 'tag', 'value' => 'thead',], |
24
|
|
|
'rowsGroup' => ['type' => 'tag', 'value' => 'tbody',], |
25
|
|
|
'row' => ['type' => 'tag', 'value' => 'tr',], |
26
|
|
|
], |
27
|
|
|
], |
28
|
|
|
'recursive' => ['parents' => ['tags' => ['ul', 'ol',]], 'children' => ['tags' => ['li',],],], |
29
|
|
|
'pairs' => [ |
30
|
|
|
[ |
31
|
|
|
'parent' => ['type' => 'tag', 'value' => 'dl',], |
32
|
|
|
'a' => ['type' => 'tag', 'value' => 'dt',], |
33
|
|
|
'b' => ['type' => 'tag', 'value' => 'dd',], |
34
|
|
|
], |
35
|
|
|
], |
36
|
|
|
'splits' => ['what' => ['tags' => ['p'],], 'on' => ['. ', '? ', '! ']], |
37
|
|
|
'docFactory' => null, |
38
|
|
|
'manager' => null, |
39
|
|
|
'parser' => null, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private $config; |
46
|
|
|
/** |
47
|
|
|
* @var ManagerConfig |
48
|
|
|
*/ |
49
|
|
|
private $managerConfig; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var CheckNode |
53
|
|
|
*/ |
54
|
|
|
public $manager; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var Parse |
58
|
|
|
*/ |
59
|
|
|
public $parser; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var DOMDocumentFactory |
63
|
|
|
*/ |
64
|
|
|
public $docFactory; |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
private function __construct(array $config) |
68
|
|
|
{ |
69
|
|
|
$this->config = $config; |
70
|
|
|
$this->setDocFactory($this->getKeyObject('docFactory')); |
71
|
|
|
$this->setManager($this->getKeyObject('manager')); |
72
|
|
|
$this->setParser($this->getKeyObject('Parser')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function createConfig(array $config = null): Config |
76
|
|
|
{ |
77
|
|
|
return new Config(\is_array($config) ? $config : self::$default); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function setManagerConfig(): void |
81
|
|
|
{ |
82
|
|
|
$this->managerConfig = new ManagerConfig($this->getManagerConfigArray()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getManagerConfig(): ManagerConfig |
86
|
|
|
{ |
87
|
|
|
$this->setManagerConfig(); |
88
|
|
|
|
89
|
|
|
return $this->managerConfig; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function setManager(?object $manager = null): void |
93
|
|
|
{ |
94
|
|
|
$this->manager = $manager instanceof NodeCheck ? $manager : new Manager($this->getManagerConfig()); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function setDocFactory(?object $factory = null): void |
98
|
|
|
{ |
99
|
|
|
$this->docFactory = $factory instanceof DOMDocumentFactory ? $factory : new DOMDocumentFactory(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function setParser(?object $parser = null): void |
103
|
|
|
{ |
104
|
|
|
$this->parser = $parser instanceof Parse ? $parser : new Parser($this->manager); |
105
|
|
|
if (\method_exists($this->parser, 'setDocFactory')) { |
106
|
|
|
$this->parser->setDocFactory($this->docFactory); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
private function getKeyArray(string $key): array |
111
|
|
|
{ |
112
|
|
|
return $this->hasKey($key) && \is_array($this->config[$key]) ? $this->config[$key] : []; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function getKeyObject(string $key): ?object |
116
|
|
|
{ |
117
|
|
|
return $this->haskey($key) && \is_object($this->config[$key]) ? $this->config[$key] : null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function hasKey(string $key): bool |
121
|
|
|
{ |
122
|
|
|
return \array_key_exists($key, $this->config); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function getManagerConfigArray(): array |
126
|
|
|
{ |
127
|
|
|
return [ |
128
|
|
|
\array_merge(['group' => 'parents',], $this->getKeyArray('parents')), |
129
|
|
|
\array_merge(['group' => 'specials',], $this->getKeyArray('specials')), |
130
|
|
|
\array_merge(['group' => 'rowCol',], $this->getKeyArray('rowCol')), |
131
|
|
|
\array_merge(['group' => 'recursive',], $this->getKeyArray('recursive')), |
132
|
|
|
\array_merge(['group' => 'pairs',], $this->getKeyArray('pairs')), |
133
|
|
|
\array_merge(['group' => 'splits',], $this->getKeyArray('splits')), |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.