Exception
3 years ago
Map
3 years ago
Tool
3 years ago
AbstractArray.php
3 years ago
AbstractCollection.php
3 years ago
AbstractSet.php
3 years ago
ArrayInterface.php
3 years ago
Collection.php
3 years ago
CollectionInterface.php
3 years ago
DoubleEndedQueue.php
3 years ago
DoubleEndedQueueInterface.php
3 years ago
GenericArray.php
3 years ago
Queue.php
3 years ago
QueueInterface.php
3 years ago
Set.php
3 years ago
Set.php
70 lines
| 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 | |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace Ramsey\Collection; |
| 16 | |
| 17 | /** |
| 18 | * A set is a collection that contains no duplicate elements. |
| 19 | * |
| 20 | * Great care must be exercised if mutable objects are used as set elements. |
| 21 | * The behavior of a set is not specified if the value of an object is changed |
| 22 | * in a manner that affects equals comparisons while the object is an element in |
| 23 | * the set. |
| 24 | * |
| 25 | * Example usage: |
| 26 | * |
| 27 | * ``` php |
| 28 | * $foo = new \My\Foo(); |
| 29 | * $set = new Set(\My\Foo::class); |
| 30 | * |
| 31 | * $set->add($foo); // returns TRUE, the element don't exists |
| 32 | * $set->add($foo); // returns FALSE, the element already exists |
| 33 | * |
| 34 | * $bar = new \My\Foo(); |
| 35 | * $set->add($bar); // returns TRUE, $bar !== $foo |
| 36 | * ``` |
| 37 | * |
| 38 | * @template T |
| 39 | * @extends AbstractSet<T> |
| 40 | */ |
| 41 | class Set extends AbstractSet |
| 42 | { |
| 43 | /** |
| 44 | * The type of elements stored in this set |
| 45 | * |
| 46 | * A set's type is immutable. For this reason, this property is private. |
| 47 | * |
| 48 | * @var string |
| 49 | */ |
| 50 | private $setType; |
| 51 | |
| 52 | /** |
| 53 | * Constructs a set object of the specified type, optionally with the |
| 54 | * specified data. |
| 55 | * |
| 56 | * @param string $setType The type (FQCN) associated with this set. |
| 57 | * @param array<array-key, T> $data The initial items to store in the set. |
| 58 | */ |
| 59 | public function __construct(string $setType, array $data = []) |
| 60 | { |
| 61 | $this->setType = $setType; |
| 62 | parent::__construct($data); |
| 63 | } |
| 64 | |
| 65 | public function getType(): string |
| 66 | { |
| 67 | return $this->setType; |
| 68 | } |
| 69 | } |
| 70 |