1
|
|
|
<?php |
2
|
|
|
namespace CfdiUtils\Nodes; |
3
|
|
|
|
4
|
|
|
use CfdiUtils\Utils\Xml; |
5
|
|
|
|
6
|
|
|
class Attributes implements \Countable, \IteratorAggregate, \ArrayAccess |
7
|
|
|
{ |
8
|
|
|
/** @var string[] */ |
9
|
|
|
private $attributes = []; |
10
|
|
|
|
11
|
844 |
|
public function __construct(array $attributes = []) |
12
|
|
|
{ |
13
|
844 |
|
$this->importArray($attributes); |
14
|
843 |
|
} |
15
|
|
|
|
16
|
664 |
|
public function get(string $name): string |
17
|
|
|
{ |
18
|
664 |
|
if (! array_key_exists($name, $this->attributes)) { |
19
|
242 |
|
return ''; |
20
|
|
|
} |
21
|
629 |
|
return $this->attributes[$name]; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Set a value in the collection |
26
|
|
|
* |
27
|
|
|
* @param string $name |
28
|
|
|
* @param string|null $value If null then it will remove the value instead of setting to empty string |
29
|
|
|
* @return self |
30
|
|
|
*/ |
31
|
769 |
|
public function set(string $name, string $value = null): self |
32
|
|
|
{ |
33
|
769 |
|
if (null === $value) { |
34
|
118 |
|
$this->remove($name); |
35
|
118 |
|
return $this; |
36
|
|
|
} |
37
|
752 |
|
if (! Xml::isValidXmlName($name)) { |
38
|
9 |
|
throw new \UnexpectedValueException(sprintf('Cannot set attribute with an invalid xml name: "%s"', $name)); |
39
|
|
|
} |
40
|
743 |
|
$this->attributes[$name] = $value; |
41
|
743 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
130 |
|
public function remove(string $name): self |
45
|
|
|
{ |
46
|
130 |
|
unset($this->attributes[$name]); |
47
|
130 |
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
5 |
|
public function removeAll(): self |
51
|
|
|
{ |
52
|
5 |
|
$this->attributes = []; |
53
|
5 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
359 |
|
public function exists(string $name): bool |
57
|
|
|
{ |
58
|
359 |
|
return array_key_exists($name, $this->attributes); |
59
|
|
|
} |
60
|
|
|
|
61
|
844 |
|
public function importArray(array $attributes): self |
62
|
|
|
{ |
63
|
844 |
|
foreach ($attributes as $key => $value) { |
64
|
731 |
|
$this->set($key, $this->castValueToString($key, $value)); |
65
|
|
|
} |
66
|
843 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
public function exportArray(): array |
70
|
|
|
{ |
71
|
4 |
|
return $this->attributes; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $key |
76
|
|
|
* @param mixed $value |
77
|
|
|
* @return string|null |
78
|
|
|
*/ |
79
|
758 |
|
private function castValueToString(string $key, $value) |
80
|
|
|
{ |
81
|
758 |
|
if (null === $value) { |
82
|
118 |
|
return null; |
83
|
|
|
} |
84
|
741 |
|
if (is_scalar($value)) { |
85
|
739 |
|
return strval($value); |
86
|
|
|
} |
87
|
2 |
|
if (is_object($value) && is_callable([$value, '__toString'])) { |
88
|
1 |
|
return strval($value); |
89
|
|
|
} |
90
|
1 |
|
throw new \InvalidArgumentException(sprintf('Cannot convert value of attribute %s to string', $key)); |
91
|
|
|
} |
92
|
|
|
|
93
|
36 |
|
public function getIterator() |
94
|
|
|
{ |
95
|
36 |
|
return new \ArrayIterator($this->attributes); |
96
|
|
|
} |
97
|
|
|
|
98
|
356 |
|
public function offsetExists($offset) |
99
|
|
|
{ |
100
|
356 |
|
return $this->exists((string) $offset); |
101
|
|
|
} |
102
|
|
|
|
103
|
659 |
|
public function offsetGet($offset) |
104
|
|
|
{ |
105
|
659 |
|
return $this->get((string) $offset); |
106
|
|
|
} |
107
|
|
|
|
108
|
153 |
|
public function offsetSet($offset, $value) |
109
|
|
|
{ |
110
|
153 |
|
$offset = strval($offset); |
111
|
153 |
|
$this->set($offset, $this->castValueToString($offset, $value)); |
112
|
153 |
|
} |
113
|
|
|
|
114
|
11 |
|
public function offsetUnset($offset) |
115
|
|
|
{ |
116
|
11 |
|
$this->remove((string) $offset); |
117
|
11 |
|
} |
118
|
|
|
|
119
|
7 |
|
public function count() |
120
|
|
|
{ |
121
|
7 |
|
return count($this->attributes); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|