Completed
Push — master ( dac95d...cf9e7d )
by Thomas
02:37
created

src/FluentDOM/Loader/Html.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Load a DOM document from a xml string
4
 *
5
 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
6
 * @copyright Copyright (c) 2009-2014 Bastian Feder, Thomas Weinert
7
 */
8
9
namespace FluentDOM\Loader {
10
11
  use FluentDOM\Document;
12
  use FluentDOM\DocumentFragment;
13
  use FluentDOM\Loadable;
14
15
  /**
16
   * Load a DOM document from a xml string
17
   */
18
  class Html implements Loadable {
19
20
    use Supports\Libxml;
21
22
    const IS_FRAGMENT = 'is_fragment';
23
24
    /**
25
     * @return string[]
26
     */
27 12
    public function getSupported() {
28 12
      return array('html', 'text/html', 'html-fragment', 'text/html-fragment');
29 8
    }
30
31
    /**
32
     * @see Loadable::load
33
     * @param string $source
34
     * @param string $contentType
35
     * @param array|\Traversable|Options $options
36
     * @return Document|Result|NULL
37
     */
38 8
    public function load($source, $contentType, $options = []) {
39 8
      if ($this->supports($contentType)) {
40 7
        return (new Libxml\Errors())->capture(
41
          function() use ($source, $contentType, $options) {
42 7
            $selection = false;
43 7
            $document = new Document();
44 7
            $settings = $this->getOptions($options);
45 7
            if ($this->isFragment($contentType, $settings)) {
46 1
              $this->loadFragmentIntoDom($document, $source, $settings[Options::LIBXML_OPTIONS]);
47 1
              $selection = $document->evaluate('/*');
48 1
            } else {
49 6
              $settings->isAllowed($sourceType = $settings->getSourceType($source));
50 View Code Duplication
              switch ($sourceType) {
0 ignored issues
show
This code seems to be duplicated across 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...
51 5
              case Options::IS_FILE :
52 2
                $document->loadHTMLFile($source, $settings[Options::LIBXML_OPTIONS]);
53 2
                break;
54 3
              case Options::IS_STRING :
55 3
              default :
56 3
                $document->loadHTML($source, $settings[Options::LIBXML_OPTIONS]);
57 3
              }
58
            }
59 6
            return new Result($document, 'text/html', $selection);
60
          }
61 7
        );
62
      }
63 1
      return NULL;
64
    }
65
66
    /**
67
     * @see LoadableFragment::loadFragment
68
     * @param string $source
69
     * @param string $contentType
70
     * @param array|\Traversable|Options $options
71
     * @return DocumentFragment|NULL
72
     */
73 2
    public function loadFragment($source, $contentType, $options = []) {
74 2
      if ($this->supports($contentType)) {
75 1
        $options = $this->getOptions($options);
76 1
        return (new Libxml\Errors())->capture(
77 1
          function() use ($source, $options) {
78 1
            $document = new Document();
79 1
            $fragment = $document->createDocumentFragment();
80 1
            $document->loadHTML('<html-fragment>'.$source.'</html-fragment>', $options[Options::LIBXML_OPTIONS]);
81 1
            $nodes = $document->evaluate('//html-fragment[1]/node()');
82 1
            foreach ($nodes as $node) {
83 1
              $fragment->append($node);
84 1
            }
85 1
            return $fragment;
86
          }
87 1
        );
88
      }
89 1
      return NULL;
90
    }
91
92 7
    private function isFragment($contentType, $options) {
93
      return (
94 7
        $contentType == 'html-fragment' ||
95 7
        $contentType == 'text/html-fragment' ||
96 6
        $options[self::IS_FRAGMENT]
97 7
      );
98
    }
99
100 1
    private function loadFragmentIntoDom(\DOMDocument $document, $source, $loadOptions) {
101 1
      $htmlDom = new Document();
102 1
      $htmlDom->loadHTML('<html-fragment>'.$source.'</html-fragment>', $loadOptions);
103 1
      $nodes = $htmlDom->evaluate('//html-fragment[1]/node()');
104 1
      foreach ($nodes as $node) {
105 1
        if ($importedNode = $document->importNode($node, TRUE)) {
106 1
          $document->appendChild($importedNode);
107 1
        }
108 1
      }
109 1
    }
110
  }
111
}