Completed
Push — master ( 96b297...3db3a5 )
by Thomas
02:30
created

Namespaces::validatePrefix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace FluentDOM {
4
5
  class Namespaces implements NamespaceResolver, \ArrayAccess, \IteratorAggregate, \Countable {
6
7
    /**
8
     * @var array
9
     */
10
    private $_namespaces = [];
11
12
    /**
13
     * @var array
14
     */
15
    private $_reserved = [
16
      'xml' => 'http://www.w3.org/XML/1998/namespace',
17
      'xmlns' => 'http://www.w3.org/2000/xmlns/'
18
    ];
19
20
    /**
21
     * Namespaces constructor.
22
     * @param null|array|\Traversable $namespaces
23
     */
24 34
    public function __construct($namespaces = NULL) {
25 34
      if (isset($namespaces)) {
26 3
        $this->assign($namespaces);
27 3
      }
28 34
    }
29
30 11
    public function resolveNamespace($prefix) {
31 11
      return $this[$prefix];
32
    }
33
34 2
    public function isReservedPrefix($prefix) {
35 2
      return array_key_exists($prefix, $this->_reserved);
36
    }
37
38 4
    public function offsetExists($prefix) {
39 4
      return array_key_exists($prefix, $this->_reserved) || array_key_exists($prefix, $this->_namespaces);
40
    }
41
42 8
    public function offsetSet($prefix, $namespaceUri) {
43 8
      $prefix = $this->validatePrefix($prefix);
44 8
      if (isset($this->_reserved[$prefix])) {
45 1
        throw new \LogicException(
46 1
          sprintf('Can not register reserved namespace prefix "%s".', $prefix)
47 1
        );
48
      }
49 7
      $this->_namespaces[$prefix] = $namespaceUri;
50 7
    }
51
52 11
    public function offsetGet($prefix) {
53 11
      $prefix = $this->validatePrefix($prefix);
54 11
      if (array_key_exists($prefix, $this->_reserved)) {
55 1
        return $this->_reserved[$prefix];
56 10
      } elseif (array_key_exists($prefix, $this->_namespaces)) {
57 4
        return $this->_namespaces[$prefix];
58 6
      } elseif ($prefix === '#default') {
59 5
        return '';
60
      }
61 1
      throw new \LogicException(
62 1
        sprintf('Unknown namespace prefix "%s".', $prefix)
63 1
      );
64
    }
65
66 1
    public function offsetUnset($prefix) {
67 1
      $prefix = $this->validatePrefix($prefix);
68 1
      if (array_key_exists($prefix, $this->_namespaces)) {
69 1
        unset($this->_namespaces[$prefix]);
70 1
      }
71 1
    }
72
73 7
    public function getIterator() {
74 7
      return new \ArrayIterator($this->_namespaces);
75
    }
76
77 1
    public function count() {
78 1
      return count($this->_namespaces);
79
    }
80
81
    /**
82
     * @param array|\Traversable $namespaces
83
     */
84 3
    public function assign($namespaces) {
85 3
      $this->_namespaces = [];
86 3
      foreach ($namespaces as $prefix => $namespaceUri) {
87 3
        $this->offsetSet($prefix, $namespaceUri);
88 3
      }
89 3
    }
90
91
    /**
92
     * @param string $prefix
93
     * @return string
94
     */
95 15
    private function validatePrefix($prefix) {
96 15
      return empty($prefix) ? '#default' : $prefix;
97
    }
98
  }
99
}