| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the ramsey/collection library |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
* |
| 9 |
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> |
| 10 |
* @license http://opensource.org/licenses/MIT MIT |
| 11 |
*/ |
| 12 |
declare (strict_types=1); |
| 13 |
namespace Dudlewebs\WPMCS\Ramsey\Collection; |
| 14 |
|
| 15 |
/** |
| 16 |
* A set is a collection that contains no duplicate elements. |
| 17 |
* |
| 18 |
* Great care must be exercised if mutable objects are used as set elements. |
| 19 |
* The behavior of a set is not specified if the value of an object is changed |
| 20 |
* in a manner that affects equals comparisons while the object is an element in |
| 21 |
* the set. |
| 22 |
* |
| 23 |
* Example usage: |
| 24 |
* |
| 25 |
* ``` php |
| 26 |
* $foo = new \My\Foo(); |
| 27 |
* $set = new Set(\My\Foo::class); |
| 28 |
* |
| 29 |
* $set->add($foo); // returns TRUE, the element don't exists |
| 30 |
* $set->add($foo); // returns FALSE, the element already exists |
| 31 |
* |
| 32 |
* $bar = new \My\Foo(); |
| 33 |
* $set->add($bar); // returns TRUE, $bar !== $foo |
| 34 |
* ``` |
| 35 |
* |
| 36 |
* @template T |
| 37 |
* @extends AbstractSet<T> |
| 38 |
*/ |
| 39 |
class Set extends AbstractSet |
| 40 |
{ |
| 41 |
/** |
| 42 |
* The type of elements stored in this set |
| 43 |
* |
| 44 |
* A set's type is immutable. For this reason, this property is private. |
| 45 |
*/ |
| 46 |
private string $setType; |
| 47 |
/** |
| 48 |
* Constructs a set object of the specified type, optionally with the |
| 49 |
* specified data. |
| 50 |
* |
| 51 |
* @param string $setType The type (FQCN) associated with this set. |
| 52 |
* @param array<array-key, T> $data The initial items to store in the set. |
| 53 |
*/ |
| 54 |
public function __construct(string $setType, array $data = []) |
| 55 |
{ |
| 56 |
$this->setType = $setType; |
| 57 |
parent::__construct($data); |
| 58 |
} |
| 59 |
public function getType(): string |
| 60 |
{ |
| 61 |
return $this->setType; |
| 62 |
} |
| 63 |
} |
| 64 |
|