• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

chdemko / php-sorted-collections / 12390158994

18 Dec 2024 09:33AM UTC coverage: 100.0%. Remained the same
12390158994

push

github

chdemko
Fix style in benchmarks

754 of 754 relevant lines covered (100.0%)

113.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/SortedCollection/SubSet.php
1
<?php
2

3
/**
4
 * chdemko\SortedCollection\SubSet class
5
 *
6
 * @author    Christophe Demko <chdemko@gmail.com>
7
 * @copyright Copyright (C) 2012-2024 Christophe Demko. All rights reserved.
8
 *
9
 * @license BSD 3-Clause License
10
 *
11
 * This file is part of the php-sorted-collections package https://github.com/chdemko/php-sorted-collections
12
 */
13

14
// Declare chdemko\SortedCollection namespace
15
namespace chdemko\SortedCollection;
16

17
/**
18
 * Sub set
19
 *
20
 * @package    SortedCollection
21
 * @subpackage Set
22
 *
23
 * @since 1.0.0
24
 *
25
 * @property      mixed      $from           The from element
26
 * @property      boolean    $fromInclusive  The from inclusive flag
27
 * @property      mixed      $to             The to element
28
 * @property      boolean    $toInclusive    The to inclusive flag
29
 * @property-read callable   $comparator     The element comparison function
30
 * @property-read mixed      $first          The first element of the set
31
 * @property-read mixed      $last           The last element of the set
32
 * @property-read integer    $count          The number of elements in the set
33
 * @property-read SortedSet  $set            The underlying set
34
 */
35
class SubSet extends AbstractSet
36
{
37
    /**
38
     * When the from or to value is unused
39
     *
40
     * @since 1.0.0
41
     */
42
    private const UNUSED = 0;
43

44
    /**
45
     * When the from or to value is inclusive
46
     *
47
     * @since 1.0.0
48
     */
49
    private const INCLUSIVE = 1;
50

51
    /**
52
     * When the from or to value is exclusive
53
     *
54
     * @since 1.0.0
55
     */
56
    private const EXCLUSIVE = 2;
57

58
    /**
59
     * @var SortedSet  Internal set
60
     *
61
     * @since 1.0.0
62
     */
63
    private $set;
64

65
    /**
66
     * Magic get method
67
     *
68
     * @param string $property The property
69
     *
70
     * @return mixed The value associated to the property
71
     *
72
     * @since 1.0.0
73
     */
74
    public function __get($property)
75
    {
76
        switch ($property) {
77
            case 'from':
9✔
78
                return $this->getMap()->fromKey;
8✔
79
            case 'to':
9✔
80
                return $this->getMap()->toKey;
8✔
81
            case 'fromInclusive':
9✔
82
                return $this->getMap()->fromInclusive;
8✔
83
            case 'toInclusive':
8✔
84
                return $this->getMap()->toInclusive;
8✔
85
            case 'set':
1✔
86
                return $this->set;
1✔
87
            default:
88
                return parent::__get($property);
1✔
89
        }
90
    }
91

92
    /**
93
     * Magic set method
94
     *
95
     * @param string $property The property
96
     * @param mixed  $value    The new value
97
     *
98
     * @throws RuntimeException If the property does not exist
99
     *
100
     * @return void
101
     *
102
     * @since 1.0.0
103
     */
104
    public function __set($property, $value)
105
    {
106
        switch ($property) {
107
            case 'from':
1✔
108
                $this->getMap()->fromKey = $value;
1✔
109
                break;
1✔
110
            case 'to':
1✔
111
                $this->getMap()->toKey = $value;
1✔
112
                break;
1✔
113
            case 'fromInclusive':
1✔
114
                $this->getMap()->fromInclusive = $value;
1✔
115
                break;
1✔
116
            case 'toInclusive':
1✔
117
                $this->getMap()->toInclusive = $value;
1✔
118
                break;
1✔
119
            default:
120
                throw new \RuntimeException('Undefined property');
1✔
121
        }
122
    }
123

124
    /**
125
     * Magic unset method
126
     *
127
     * @param string $property The property
128
     *
129
     * @throws RuntimeException If the property does not exist
130
     *
131
     * @return void
132
     *
133
     * @since 1.0.0
134
     */
135
    public function __unset($property)
136
    {
137
        switch ($property) {
138
            case 'from':
1✔
139
                unset($this->getMap()->fromKey);
1✔
140
                break;
1✔
141
            case 'to':
1✔
142
                unset($this->getMap()->toKey);
1✔
143
                break;
1✔
144
            case 'fromInclusive':
1✔
145
                unset($this->getMap()->fromInclusive);
1✔
146
                break;
1✔
147
            case 'toInclusive':
1✔
148
                unset($this->getMap()->toInclusive);
1✔
149
                break;
1✔
150
            default:
151
                throw new \RuntimeException('Undefined property');
1✔
152
        }
153
    }
154

155
    /**
156
     * Magic isset method
157
     *
158
     * @param string $property The property
159
     *
160
     * @return boolean
161
     *
162
     * @since 1.0.0
163
     */
164
    public function __isset($property)
165
    {
166
        switch ($property) {
167
            case 'from':
9✔
168
                return isset($this->getMap()->fromKey);
9✔
169
            case 'to':
9✔
170
                return isset($this->getMap()->toKey);
9✔
171
            case 'fromInclusive':
1✔
172
                return isset($this->getMap()->fromInclusive);
1✔
173
            case 'toInclusive':
1✔
174
                return isset($this->getMap()->toInclusive);
1✔
175
            default:
176
                return false;
1✔
177
        }
178
    }
179

180
    /**
181
     * Constructor
182
     *
183
     * @param SortedSet $set        Internal set
184
     * @param mixed     $from       The from element
185
     * @param integer   $fromOption The option for from (SubSet::UNUSED, SubSet::INCLUSIVE or SubSet::EXCLUSIVE)
186
     * @param mixed     $to         The to element
187
     * @param integer   $toOption   The option for to (SubSet::UNUSED, SubSet::INCLUSIVE or SubSet::EXCLUSIVE)
188
     *
189
     * @since 1.0.0
190
     */
191
    protected function __construct(SortedSet $set, $from, $fromOption, $to, $toOption)
192
    {
193
        if ($fromOption == self::UNUSED) {
20✔
194
            if ($toOption == self::UNUSED) {
5✔
195
                $this->setMap(SubMap::view($set->getMap()));
2✔
196
            } else {
197
                $this->setMap(SubMap::head($set->getMap(), $to, $toOption == self::INCLUSIVE));
3✔
198
            }
199
        } elseif ($toOption == self::UNUSED) {
15✔
200
            $this->setMap(SubMap::tail($set->getMap(), $from, $fromOption == self::INCLUSIVE));
3✔
201
        } else {
202
            $this->setMap(
12✔
203
                SubMap::create($set->getMap(), $from, $to, $fromOption == self::INCLUSIVE, $toOption == self::INCLUSIVE)
12✔
204
            );
12✔
205
        }
206

207
        $this->set = $set;
20✔
208
    }
209

210
    /**
211
     * Create
212
     *
213
     * @param SortedSet $set           Internal set
214
     * @param mixed     $from          The from element
215
     * @param mixed     $to            The to element
216
     * @param boolean   $fromInclusive The inclusive flag for from
217
     * @param boolean   $toInclusive   The inclusive flag for to
218
     *
219
     * @return SubSet A new sub set
220
     *
221
     * @since 1.0.0
222
     */
223
    public static function create(SortedSet $set, $from, $to, $fromInclusive = true, $toInclusive = false)
224
    {
225
        return new static(
12✔
226
            $set,
12✔
227
            $from,
12✔
228
            $fromInclusive ? self::INCLUSIVE : self::EXCLUSIVE,
12✔
229
            $to,
12✔
230
            $toInclusive ? self::INCLUSIVE : self::EXCLUSIVE
12✔
231
        );
12✔
232
    }
233

234
    /**
235
     * Head
236
     *
237
     * @param SortedSet $set         Internal set
238
     * @param mixed     $to          The to element
239
     * @param boolean   $toInclusive The inclusive flag for to
240
     *
241
     * @return SubSet A new head set
242
     *
243
     * @since 1.0.0
244
     */
245
    public static function head(SortedSet $set, $to, $toInclusive = false)
246
    {
247
        return new static($set, null, self::UNUSED, $to, $toInclusive ? self::INCLUSIVE : self::EXCLUSIVE);
3✔
248
    }
249

250
    /**
251
     * Tail
252
     *
253
     * @param SortedSet $set           Internal set
254
     * @param mixed     $from          The from element
255
     * @param boolean   $fromInclusive The inclusive flag for from
256
     *
257
     * @return SubSet A new tail set
258
     *
259
     * @since 1.0.0
260
     */
261
    public static function tail(SortedSet $set, $from, $fromInclusive = true)
262
    {
263
        return new static($set, $from, $fromInclusive ? self::INCLUSIVE : self::EXCLUSIVE, null, self::UNUSED);
3✔
264
    }
265

266
    /**
267
     * View
268
     *
269
     * @param SortedSet $set Internal set
270
     *
271
     * @return SubSet A new sub set
272
     *
273
     * @since 1.0.0
274
     */
275
    public static function view(SortedSet $set)
276
    {
277
        return new static($set, null, self::UNUSED, null, self::UNUSED);
2✔
278
    }
279

280
    /**
281
     * Serialize the object
282
     *
283
     * @return array Array of values
284
     *
285
     * @since 1.0.0
286
     */
287
    public function jsonSerialize(): array
288
    {
289
        if (isset($this->from)) {
8✔
290
            if (isset($this->to)) {
6✔
291
                return array(
5✔
292
                    'SubSet' => array(
5✔
293
                        'set' => $this->set->jsonSerialize(),
5✔
294
                        'from' => $this->from,
5✔
295
                        'fromInclusive' => $this->fromInclusive,
5✔
296
                        'to' => $this->to,
5✔
297
                        'toInclusive' => $this->toInclusive,
5✔
298
                    )
5✔
299
                );
5✔
300
            } else {
301
                return array(
1✔
302
                    'TailSet' => array(
1✔
303
                        'set' => $this->set->jsonSerialize(),
1✔
304
                        'from' => $this->from,
1✔
305
                        'fromInclusive' => $this->fromInclusive,
1✔
306
                    )
1✔
307
                );
1✔
308
            }
309
        } else {
310
            if (isset($this->to)) {
2✔
311
                return array(
1✔
312
                    'HeadSet' => array(
1✔
313
                        'set' => $this->set->jsonSerialize(),
1✔
314
                        'to' => $this->to,
1✔
315
                        'toInclusive' => $this->toInclusive,
1✔
316
                    )
1✔
317
                );
1✔
318
            } else {
319
                return array(
1✔
320
                    'ViewSet' => array(
1✔
321
                        'set' => $this->set->jsonSerialize(),
1✔
322
                    )
1✔
323
                );
1✔
324
            }
325
        }
326
    }
327
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc