forked from php-gettext/Gettext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlags.php
More file actions
98 lines (79 loc) · 1.85 KB
/
Flags.php
File metadata and controls
98 lines (79 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
declare(strict_types = 1);
namespace Gettext;
use ArrayIterator;
use Countable;
use IteratorAggregate;
use JsonSerializable;
use ReturnTypeWillChange;
/**
* Class to manage the flags of a translation.
*
* @phpstan-consistent-constructor
*/
class Flags implements JsonSerializable, Countable, IteratorAggregate
{
protected $flags = [];
public static function __set_state(array $state): Flags
{
return new static(...$state['flags']);
}
public function __construct(string ...$flags)
{
if (!empty($flags)) {
$this->add(...$flags);
}
}
public function __debugInfo()
{
return $this->toArray();
}
public function add(string ...$flags): self
{
foreach ($flags as $flag) {
if (!$this->has($flag)) {
$this->flags[] = $flag;
}
}
sort($this->flags);
return $this;
}
public function delete(string ...$flags): self
{
foreach ($flags as $flag) {
$key = array_search($flag, $this->flags);
if (is_int($key)) {
array_splice($this->flags, $key, 1);
}
}
return $this;
}
public function has(string $flag): bool
{
return in_array($flag, $this->flags, true);
}
#[ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->toArray();
}
#[ReturnTypeWillChange]
public function getIterator()
{
return new ArrayIterator($this->flags);
}
public function count(): int
{
return count($this->flags);
}
public function toArray(): array
{
return $this->flags;
}
public function mergeWith(Flags $flags): Flags
{
$merged = clone $this;
$merged->add(...$flags->flags);
return $merged;
}
}