These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * FluentDOM\Loaders is a list of loaders that allow to import data sources into |
||
4 | * a DOM document. |
||
5 | * |
||
6 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License |
||
7 | * @copyright Copyright (c) 2009-2014 Bastian Feder, Thomas Weinert |
||
8 | */ |
||
9 | |||
10 | namespace FluentDOM { |
||
11 | |||
12 | use FluentDOM\Loader\Result; |
||
13 | |||
14 | /** |
||
15 | * FluentDOM\Loaders is a list of loaders that allow to import data sources into |
||
16 | * a DOM document. |
||
17 | * |
||
18 | * The list is iterated until a valid document is returned by the loader |
||
19 | * |
||
20 | */ |
||
21 | class Loaders implements \IteratorAggregate, Loadable { |
||
22 | |||
23 | private $_list = array(); |
||
24 | |||
25 | /** |
||
26 | * Store the a list of loaders if provided. |
||
27 | * |
||
28 | * @param array|\Traversable $list |
||
29 | */ |
||
30 | 7 | public function __construct($list = NULL) { |
|
31 | 7 | if (is_array($list) || $list instanceOf \Traversable) { |
|
32 | 6 | foreach ($list as $loader) { |
|
33 | 6 | $this->add($loader); |
|
34 | 6 | } |
|
35 | 6 | } |
|
36 | 7 | } |
|
37 | |||
38 | /** |
||
39 | * Add a loader to the list |
||
40 | * |
||
41 | * @param Loadable $loader |
||
42 | */ |
||
43 | 7 | public function add(Loadable $loader) { |
|
44 | 7 | $this->_list[spl_object_hash($loader)] = $loader; |
|
45 | 7 | } |
|
46 | |||
47 | /** |
||
48 | * Remove a loader to the list |
||
49 | * |
||
50 | * @param Loadable $loader |
||
51 | */ |
||
52 | 1 | public function remove($loader) { |
|
53 | 1 | $key = spl_object_hash($loader); |
|
54 | 1 | if (isset($this->_list[$key])) { |
|
55 | 1 | unset($this->_list[$key]); |
|
56 | 1 | } |
|
57 | 1 | } |
|
58 | |||
59 | /** |
||
60 | * Allow to iterate all added loaders |
||
61 | * |
||
62 | * @return \Traversable |
||
63 | */ |
||
64 | 7 | public function getIterator() { |
|
65 | 7 | return new \ArrayIterator(array_values($this->_list)); |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * Validate if the list contains a loader that supports the given content type |
||
70 | * |
||
71 | * @param string $contentType |
||
72 | * @return boolean |
||
73 | */ |
||
74 | 3 | public function supports($contentType) { |
|
75 | 3 | foreach ($this as $loader) { |
|
76 | /** |
||
77 | * @var Loadable $loader |
||
78 | */ |
||
79 | 3 | if ($loader->supports($contentType)) { |
|
80 | 2 | return TRUE; |
|
81 | } |
||
82 | 1 | } |
|
83 | 1 | return FALSE; |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * Load a data source, the content type allows the loader to decide if it supports |
||
88 | * the data source |
||
89 | * |
||
90 | * @param mixed $source |
||
91 | * @param string $contentType |
||
92 | * @param array|\Traversable|Options $options |
||
93 | * @return \DOMDocument|Result|NULL |
||
94 | */ |
||
95 | 1 | View Code Duplication | public function load($source, $contentType, $options = []) { |
1 ignored issue
–
show
|
|||
96 | 1 | $dom = NULL; |
|
97 | 1 | foreach ($this as $loader) { |
|
98 | /** |
||
99 | * @var Loadable $loader |
||
100 | */ |
||
101 | 1 | if ($loader->supports($contentType) && ($dom = $loader->load($source, $contentType, $options))) { |
|
102 | 1 | break; |
|
103 | } |
||
104 | 1 | } |
|
105 | 1 | return ($dom instanceOf \DOMDocument || $dom instanceof Result) ? $dom : NULL; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Load a data source as a fragment, the content type allows the loader to decide if it supports |
||
110 | * the data source |
||
111 | * |
||
112 | * @param mixed $source |
||
113 | * @param string $contentType |
||
114 | * @param array|\Traversable|Options $options |
||
115 | * @return \DOMDocumentFragment|NULL |
||
116 | */ |
||
117 | 1 | View Code Duplication | public function loadFragment($source, $contentType, $options = []) { |
1 ignored issue
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
118 | 1 | $fragment = NULL; |
|
119 | 1 | foreach ($this as $loader) { |
|
120 | /** |
||
121 | * @var Loadable $loader |
||
122 | */ |
||
123 | if ( |
||
124 | 1 | $loader->supports($contentType) && |
|
125 | 1 | ($fragment = $loader->loadFragment($source, $contentType, $options)) |
|
126 | 1 | ) { |
|
127 | 1 | break; |
|
128 | } |
||
129 | 1 | } |
|
130 | 1 | return ($fragment instanceOf \DOMDocumentFragment) ? $fragment : NULL; |
|
131 | } |
||
132 | } |
||
133 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.