1
|
|
|
<?php |
2
|
|
|
namespace XmlSchemaValidator; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This is an utility class to keep a set of strings |
6
|
|
|
* Does not throw any error or warning |
7
|
|
|
* Convert objects to strings and any invalid argument is casted as an empty string |
8
|
|
|
* Does not allow to add an empty string |
9
|
|
|
* |
10
|
|
|
* @access private |
11
|
|
|
* @package XmlSchemaValidator |
12
|
|
|
*/ |
13
|
|
|
class SetStrings implements \Countable, \IteratorAggregate |
14
|
|
|
{ |
15
|
|
|
/** @var array */ |
16
|
|
|
protected $members; |
17
|
|
|
|
18
|
39 |
|
public function __construct(array $members = []) |
19
|
|
|
{ |
20
|
39 |
|
$this->clear(); |
21
|
39 |
|
$this->addAll($members); |
22
|
39 |
|
} |
23
|
|
|
|
24
|
39 |
|
public function clear() |
25
|
|
|
{ |
26
|
39 |
|
$this->members = []; |
27
|
39 |
|
} |
28
|
|
|
|
29
|
39 |
|
public function addAll(array $members) |
30
|
|
|
{ |
31
|
39 |
|
foreach ($members as $member) { |
32
|
14 |
|
$this->add($member); |
33
|
|
|
} |
34
|
39 |
|
} |
35
|
|
|
|
36
|
1 |
|
public function all() |
37
|
|
|
{ |
38
|
1 |
|
return array_keys($this->members); |
39
|
|
|
} |
40
|
|
|
|
41
|
18 |
|
public function add($member) |
42
|
|
|
{ |
43
|
18 |
|
$member = $this->cast($member); |
44
|
18 |
|
if ('' === $member) { |
45
|
14 |
|
return false; |
46
|
|
|
} |
47
|
6 |
|
if ($this->contains($member)) { |
48
|
1 |
|
return false; |
49
|
|
|
} |
50
|
6 |
|
$this->members[$member] = null; |
51
|
6 |
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
23 |
|
public function cast($member) |
55
|
|
|
{ |
56
|
23 |
|
if (is_object($member)) { |
57
|
|
|
$member = is_callable([$member, '__toString']) ? (string) $member : ''; |
58
|
|
|
} |
59
|
23 |
|
return strval($member); |
60
|
|
|
} |
61
|
|
|
|
62
|
6 |
|
public function contains($member) |
63
|
|
|
{ |
64
|
6 |
|
$member = $this->cast($member); |
65
|
6 |
|
return array_key_exists($member, $this->members); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public function remove($member) |
69
|
|
|
{ |
70
|
1 |
|
$member = $this->cast($member); |
71
|
1 |
|
unset($this->members[$member]); |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
public function getIterator() |
75
|
|
|
{ |
76
|
|
|
return new \ArrayIterator($this->all()); |
77
|
|
|
} |
78
|
|
|
|
79
|
10 |
|
public function count() |
80
|
|
|
{ |
81
|
10 |
|
return count($this->members); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|