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

Libxml::loadXmlDocument()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 15
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 19
ccs 14
cts 16
cp 0.875
crap 3.0175
rs 9.4285
1
<?php
2
3
namespace FluentDOM\Loader\Supports {
4
5
  use FluentDOM\Document;
6
  use FluentDOM\Loader\Options;
7
  use FluentDOM\Loader\Supports;
8
  use FluentDOM\Loader\Libxml\Errors;
9
10
  trait Libxml {
11
12
    use Supports;
13
14
    /**
15
     * @param array|\Traversable|Options $options
16
     * @return Options
17
     */
18 1
    public function getOptions($options) {
19 1
      $result = new Options(
20 1
        $options,
21
        [
22
          Options::CB_IDENTIFY_STRING_SOURCE => function($source) {
23 1
            return $this->startsWith($source, '<');
24
          }
25 1
        ]
26 1
      );
27 1
      $result['libxml'] = (int)$result['libxml'];
28 1
      return $result;
29
    }
30
31
    /**
32
     * @param string $source
33
     * @param string $contentType
34
     * @param array|\Traversable|Options $options
35
     * @return Document
36
     */
37 1
    private function loadXmlDocument($source, $contentType, $options) {
38 1
      return (new Errors())->capture(
39 1
        function () use ($source, $contentType, $options) {
40 1
          $document = new Document();
41 1
          $document->preserveWhiteSpace = FALSE;
42 1
          $settings = $this->getOptions($options);
43 1
          $settings->isAllowed($sourceType = $settings->getSourceType($source));
44
          switch ($sourceType) {
45 1
          case Options::IS_FILE :
46
            $document->load($source, $settings[Options::LIBXML_OPTIONS]);
47
            break;
48 1
          case Options::IS_STRING :
49 1
          default :
50 1
            $document->loadXML($source, $settings[Options::LIBXML_OPTIONS]);
51 1
          }
52 1
          return $document;
53
        }
54 1
      );
55
    }
56
  }
57
}