xref: /plugin/pureldap/vendor/freedsx/ldap/src/FreeDSx/Ldap/Entry/Options.php (revision 6d90d5c87387cafcb884bda8c1b3c7ab80656146)
1<?php
2/**
3 * This file is part of the FreeDSx LDAP package.
4 *
5 * (c) Chad Sikorra <Chad.Sikorra@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace FreeDSx\Ldap\Entry;
12
13/**
14 * Represents a collection of attribute options.
15 *
16 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
17 */
18class Options implements \Countable, \IteratorAggregate
19{
20    /**
21     * @var Option[]
22     */
23    protected $options;
24
25    /**
26     * @param string|Option ...$options
27     */
28    public function __construct(...$options)
29    {
30        $this->set(...$options);
31    }
32
33    /**
34     * @param string|Option ...$options
35     * @return $this
36     */
37    public function add(...$options)
38    {
39        foreach ($options as $option) {
40            $this->options[] = $option instanceof Option ? $option : new Option($option);
41        }
42
43        return $this;
44    }
45
46    /**
47     * @param string|Option ...$options
48     * @return $this
49     */
50    public function set(...$options)
51    {
52        $this->options = [];
53        foreach ($options as $i => $option) {
54            if ($option instanceof Option) {
55                $this->options[] = $option;
56            } else {
57                $this->options[] = new Option($option);
58            }
59        }
60
61        return $this;
62    }
63
64    /**
65     * @param string|Option $option
66     * @return bool
67     */
68    public function has($option): bool
69    {
70        $option = $option instanceof Option ? $option : new Option($option);
71
72        foreach ($this->options as $opt) {
73            if ($opt->equals($option)) {
74                return true;
75            }
76        }
77
78        return false;
79    }
80
81    /**
82     * @param string|Option ...$options
83     * @return $this
84     */
85    public function remove(...$options)
86    {
87        foreach ($options as $option) {
88            $option = $option instanceof Option ? $option : new Option($option);
89            foreach ($this->options as $i => $opt) {
90                if ($opt->equals($option)) {
91                    unset($this->options[$i]);
92                }
93            }
94        }
95
96        return $this;
97    }
98
99    /**
100     * Retrieve the first option, if it exists.
101     *
102     * @return Option|null
103     */
104    public function first(): ?Option
105    {
106        $option = reset($this->options);
107
108        return $option === false ? null : $option;
109    }
110
111    /**
112     * Retrieve the last option, if it exists.
113     *
114     * @return Option|null
115     */
116    public function last(): ?Option
117    {
118        $option = end($this->options);
119        reset($this->options);
120
121        return $option === false ? null : $option;
122    }
123
124    /**
125     * @param bool $sortedlc Used for comparison, as both case and order of options are irrelevant for options equality.
126     * @return string
127     */
128    public function toString(bool $sortedlc = false): string
129    {
130        $opts = $this->options;
131        if ($sortedlc) {
132            \sort($opts);
133        }
134
135        $options = '';
136        foreach ($opts as $option) {
137            $options .= ($options === '') ? $option->toString($sortedlc) : ';' . $option->toString($sortedlc);
138        }
139
140        return $options;
141    }
142
143    /**
144     * @return array
145     */
146    public function toArray(): array
147    {
148        return $this->options;
149    }
150
151    /**
152     * @return string
153     */
154    public function __toString()
155    {
156        return $this->toString();
157    }
158
159    /**
160     * @return \Traversable
161     */
162    public function getIterator()
163    {
164        return new \ArrayIterator($this->options);
165    }
166
167    /**
168     * @return int
169     */
170    public function count()
171    {
172        return \count($this->options);
173    }
174}
175