1 | <?php |
||
5 | abstract class FluentDOM { |
||
6 | |||
7 | /** |
||
8 | * @var FluentDOM\Loadable |
||
9 | */ |
||
10 | private static $_loader; |
||
11 | |||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | private static $_xpathTransformers = []; |
||
16 | |||
17 | /** |
||
18 | * @var FluentDOM\Loadable |
||
19 | */ |
||
20 | private static $_defaultLoaders = []; |
||
21 | |||
22 | /** |
||
23 | * @var FluentDOM\Serializer\Factory\Group |
||
24 | */ |
||
25 | private static $_serializerFactories; |
||
26 | |||
27 | /** |
||
28 | * Load a data source into a FluentDOM\DOM\Document |
||
29 | * |
||
30 | * @param mixed $source |
||
31 | * @param string $contentType |
||
32 | * @param array $options |
||
33 | * @return \FluentDOM\DOM\Document |
||
34 | */ |
||
35 | 5 | public static function load($source, string $contentType = 'text/xml', array $options = []): \FluentDOM\DOM\Document { |
|
42 | |||
43 | /** |
||
44 | * Return a FluentDOM Creator instance, allow to create a DOM using nested function calls |
||
45 | * |
||
46 | * @param string $version |
||
47 | * @param string $encoding |
||
48 | * @return FluentDOM\Creator |
||
49 | */ |
||
50 | 2 | public static function create(string $version = '1.0', string $encoding = 'UTF-8'): \FluentDOM\Creator { |
|
53 | |||
54 | /** |
||
55 | * @param \DOMNode|FluentDOM\Query $node |
||
56 | * @param string $contentType |
||
57 | * @return string |
||
58 | * @throws \FluentDOM\Exceptions\NoSerializer |
||
59 | */ |
||
60 | 5 | public static function save($node, string $contentType = 'text/xml'): string { |
|
69 | |||
70 | /** |
||
71 | * Create an FluentDOM::Query instance and load the source into it. |
||
72 | * |
||
73 | * @param mixed $source |
||
74 | * @param string $contentType |
||
75 | * @param array $options |
||
76 | * @return FluentDOM\Query |
||
77 | * @throws \LogicException |
||
78 | * @throws \OutOfBoundsException |
||
79 | * @throws \InvalidArgumentException |
||
80 | * @throws \FluentDOM\Exceptions\InvalidSource\Variable |
||
81 | */ |
||
82 | 3 | public static function Query( |
|
91 | |||
92 | /** |
||
93 | * Create an FluentDOM::Query instance with a modified selector callback. |
||
94 | * This allows to use CSS selectors instead of Xpath expression. |
||
95 | * |
||
96 | * @param mixed $source |
||
97 | * @param string $contentType |
||
98 | * @param array $options |
||
99 | * @throws \LogicException |
||
100 | * @return FluentDOM\Query |
||
101 | * @throws \InvalidArgumentException |
||
102 | * @throws \OutOfBoundsException |
||
103 | * @throws \FluentDOM\Exceptions\InvalidSource\Variable |
||
104 | * @codeCoverageIgnore |
||
105 | */ |
||
106 | public static function QueryCss( |
||
117 | |||
118 | /** |
||
119 | * Set a loader used in FluentDOM::load(), NULL will reset the loader. |
||
120 | * If no loader is provided an FluentDOM\Loader\Standard() will be created. |
||
121 | * |
||
122 | * @param FluentDOM\Loadable|NULL $loader |
||
123 | * @throws \FluentDOM\Exceptions\InvalidArgument |
||
124 | */ |
||
125 | 3 | public static function setLoader($loader) { |
|
136 | |||
137 | /** |
||
138 | * Register an additional default loader |
||
139 | * |
||
140 | * @param FluentDOM\Loadable|callable $loader |
||
141 | * @param string[] $contentTypes |
||
142 | * @return FluentDOM\Loaders |
||
143 | * @throws \UnexpectedValueException |
||
144 | */ |
||
145 | 3 | public static function registerLoader($loader, string ...$contentTypes): FluentDOM\Loaders { |
|
161 | |||
162 | /** |
||
163 | * Standard loader + any registered loader. |
||
164 | * |
||
165 | * @return FluentDOM\Loaders |
||
166 | * @codeCoverageIgnore |
||
167 | */ |
||
168 | public static function getDefaultLoaders(): FluentDOM\Loaders { |
||
174 | |||
175 | /** |
||
176 | * Register a serializer factory for a specified content type(s). This can be |
||
177 | * a callable returning the create serializer. |
||
178 | * |
||
179 | * @param \FluentDOM\Serializer\Factory|callable $factory |
||
180 | * @param string[] $contentTypes |
||
181 | */ |
||
182 | 1 | public static function registerSerializerFactory($factory, string ...$contentTypes) { |
|
187 | |||
188 | /** |
||
189 | * Return registered serializer factories |
||
190 | * |
||
191 | * @return FluentDOM\Serializer\Factory\Group |
||
192 | */ |
||
193 | 9 | public static function getSerializerFactories(): FluentDOM\Serializer\Factory\Group { |
|
194 | 9 | if (!(self::$_serializerFactories instanceof FluentDOM\Serializer\Factory)) { |
|
195 | 6 | $xml = function($contentType, \DOMNode $node) { |
|
196 | 6 | return new FluentDOM\Serializer\Xml($node); |
|
197 | }; |
||
198 | 2 | $html = function($contentType, \DOMNode $node) { |
|
199 | 2 | return new FluentDOM\Serializer\Html($node); |
|
200 | }; |
||
201 | 1 | $json = function($contentType, \DOMNode $node) { |
|
202 | 1 | return new FluentDOM\Serializer\Json($node); |
|
203 | }; |
||
204 | 1 | self::$_serializerFactories = new FluentDOM\Serializer\Factory\Group( |
|
205 | [ |
||
206 | 1 | 'text/html' => $html, |
|
207 | 1 | 'html' => $html, |
|
208 | 1 | 'text/xml' => $xml, |
|
209 | 1 | 'xml' => $xml, |
|
210 | 1 | 'text/json' => $json, |
|
211 | 1 | 'json' => $json |
|
212 | ] |
||
213 | ); |
||
214 | } |
||
215 | 9 | return self::$_serializerFactories; |
|
216 | } |
||
217 | |||
218 | /** |
||
219 | * Get a xpath expression builder to convert css selectors to xpath |
||
220 | * |
||
221 | * @param string $errorMessage |
||
222 | * @return \FluentDOM\Xpath\Transformer |
||
223 | * @throws \LogicException |
||
224 | */ |
||
225 | 6 | public static function getXPathTransformer( |
|
241 | |||
242 | /** |
||
243 | * @param string|callable|FluentDOM\Xpath\Transformer $transformer |
||
244 | * @param bool $reset |
||
245 | */ |
||
246 | 6 | public static function registerXpathTransformer($transformer, bool $reset = FALSE) { |
|
252 | } |
||
253 | |||
268 |