Completed
Push — master ( 3988b3...decf40 )
by Thomas
02:19
created

Xml::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 2
b 0
f 0
nc 2
nop 3
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
1
<?php
2
/**
3
 * Load a DOM document from a xml file or 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 file or string
17
   */
18
  class Xml implements Loadable {
19
20
    use Supports\Libxml;
21
22
    const LIBXML_OPTIONS = 'libxml';
23
24
    /**
25
     * @return string[]
26
     */
27 12
    public function getSupported() {
28 12
      return array('xml', 'application/xml', 'text/xml');
29
    }
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 6
    public function load($source, $contentType, $options = []) {
39 6
      if ($this->supports($contentType)) {
40 5
        return $this->loadXmlDocument($source, $contentType, $options);
41
      }
42 1
      return NULL;
43
    }
44
45
    /**
46
     * @see LoadableFragment::loadFragment
47
     * @param string $source
48
     * @param string $contentType
49
     * @param array|\Traversable|Options $options
50
     * @return DocumentFragment|NULL
51
     */
52 4
    public function loadFragment($source, $contentType, $options = []) {
53 4
      if ($this->supports($contentType)) {
54 3
        return (new Libxml\Errors())->capture(
55 3
          function() use ($source, $contentType, $options) {
56 3
            $dom = new Document();
57 3
            $fragment = $dom->createDocumentFragment();
58 3
            $fragment->appendXml($source);
59 3
            return $fragment;
60
          }
61 3
        );
62
      }
63 1
      return NULL;
64
    }
65
  }
66
}