1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* This file is part of the Set package. |
5
|
|
|
* For the full copyright information please view the LICENCE file that was |
6
|
|
|
* distributed with this package. |
7
|
|
|
* |
8
|
|
|
* @copyright Simon Deeley 2017 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace simondeeley; |
12
|
|
|
|
13
|
|
|
use SplFixedArray; |
14
|
|
|
use simondeeley\Type\Type; |
15
|
|
|
use simondeeley\Type\SetType; |
16
|
|
|
use simondeeley\Type\TypeEquality; |
17
|
|
|
use simondeeley\ImmutableArrayTypeObject; |
18
|
|
|
use simondeeley\Helpers\TypeEqualityHelperMethods; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Set type object |
22
|
|
|
* |
23
|
|
|
* Implements a mathematical set in PHP. A mathematical set is an unordered list |
24
|
|
|
* of items where two lists are considered equal if both sets contain the same |
25
|
|
|
* items but not necessarily in the same order or in the same quantity. So, for |
26
|
|
|
* example: |
27
|
|
|
* Set(1, 2, 3) === Set(3, 1, 2); |
28
|
|
|
* Set(1, 2, 3) === Set(1, 2, 2, 3); |
29
|
|
|
* but |
30
|
|
|
* Set(1, 2) !== Set(1, 2, 3); |
31
|
|
|
* |
32
|
|
|
* @author Simon Deeley <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
abstract class Set extends ImmutableArrayTypeObject implements SetType, TypeEquality |
35
|
|
|
{ |
36
|
|
|
use TypeEqualityHelperMethods; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var SplFixedArray |
40
|
|
|
*/ |
41
|
|
|
protected $data; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Construct a new Set |
45
|
|
|
* |
46
|
|
|
* @final |
47
|
|
|
* @param mixed ...$items - accepts any number of items |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
final public function __construct(...$items) |
51
|
|
|
{ |
52
|
|
|
$this->data = SplFixedArray::fromArray($items); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Compare equality of two Sets |
57
|
|
|
* |
58
|
|
|
* @final |
59
|
|
|
* @param Type $set - The set to compare with |
60
|
|
|
* @param int $flags - Optional flags |
61
|
|
|
* @return bool |
62
|
|
|
* @throws InvalidArgumentException - thrown if $type is not of an instance |
63
|
|
|
* of {@link Set} |
64
|
|
|
*/ |
65
|
|
|
final public function equals(Type $set, int $flags = TypeEquality::IGNORE_OBJECT_IDENTITY): bool |
66
|
|
|
{ |
67
|
|
|
if (false === $set instanceof SetType) { |
68
|
|
|
throw new InvalidArgumentException(sprintf( |
|
|
|
|
69
|
|
|
'Cannot compare %s with %s as they are not of the same type', |
70
|
|
|
get_class($this), |
71
|
|
|
get_class($tuple) |
|
|
|
|
72
|
|
|
)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Loop through items in $this, if any do not exist in $set then the |
76
|
|
|
// two Sets are not equal. |
77
|
|
View Code Duplication |
foreach($this->data as $item) { |
|
|
|
|
78
|
|
|
if (false === in_array($item, $set->data->toArray(), true)) { |
|
|
|
|
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Loop through items in $set. If any do not exist in $this then they |
84
|
|
|
// they are not equal. |
85
|
|
View Code Duplication |
foreach ($set->data as $item) { |
|
|
|
|
86
|
|
|
if (false === in_array($item, $this->data->toArray(), true)) { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->isSameTypeAs($set, $flags) |
92
|
|
|
&& $this->isSameObjectAs($set, $flags); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return the type of the object |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public static function getType(): string |
101
|
|
|
{ |
102
|
|
|
return 'SET'; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|