Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
50.00% |
19 / 38 |
|
55.56% |
5 / 9 |
CRAP | |
0.00% |
0 / 1 |
| PersistentSet | |
50.00% |
19 / 38 |
|
55.56% |
5 / 9 |
76.12 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| add | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| addAll | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| contains | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| remove | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| removeAll | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| get | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| count | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\WikispeechSpeechDataCollector\Domain; |
| 4 | |
| 5 | /** |
| 6 | * @file |
| 7 | * @ingroup Extensions |
| 8 | * @license GPL-2.0-or-later |
| 9 | */ |
| 10 | |
| 11 | use Countable; |
| 12 | |
| 13 | /** |
| 14 | * A collection of unique {@link Persistent} instances. |
| 15 | * Instances are unique per class and identity. |
| 16 | * |
| 17 | * @since 0.1.0 |
| 18 | */ |
| 19 | class PersistentSet implements Countable { |
| 20 | |
| 21 | /** @var array */ |
| 22 | private $instancePerIdentityPerClass; |
| 23 | |
| 24 | /** @var int */ |
| 25 | private $count = 0; |
| 26 | |
| 27 | /** |
| 28 | * @since 0.1.0 |
| 29 | */ |
| 30 | public function __construct() { |
| 31 | $this->instancePerIdentityPerClass = []; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @param Persistent $instance |
| 36 | * @return bool True if added, false if an instance |
| 37 | * of the same class with the same identity already exists in the set. |
| 38 | * @since 0.1.0 |
| 39 | */ |
| 40 | public function add( Persistent $instance ): bool { |
| 41 | $class = get_class( $instance ); |
| 42 | if ( !array_key_exists( $class, $this->instancePerIdentityPerClass ) ) { |
| 43 | $this->instancePerIdentityPerClass[$class] = []; |
| 44 | } |
| 45 | if ( array_key_exists( $instance->getIdentity(), $this->instancePerIdentityPerClass[$class] ) ) { |
| 46 | return false; |
| 47 | } |
| 48 | $this->instancePerIdentityPerClass[$class][$instance->getIdentity()] = $instance; |
| 49 | $this->count++; |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @param Persistent[] $instances |
| 55 | * @since 0.1.0 |
| 56 | */ |
| 57 | public function addAll( array $instances ) { |
| 58 | foreach ( $instances as $instance ) { |
| 59 | $this->add( $instance ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @param Persistent $instance |
| 65 | * @return bool Whether or not the an instance |
| 66 | * of the same class with the same identity exists in the set. |
| 67 | * @since 0.1.0 |
| 68 | */ |
| 69 | public function contains( Persistent $instance ): bool { |
| 70 | $class = get_class( $instance ); |
| 71 | if ( !array_key_exists( $class, $this->instancePerIdentityPerClass ) ) { |
| 72 | return false; |
| 73 | } |
| 74 | return array_key_exists( $instance->getIdentity(), $this->instancePerIdentityPerClass[$class] ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @param Persistent $instance |
| 79 | * @return bool Whether or not an instance |
| 80 | * of the same class with the same identity was removed from the set. |
| 81 | * @since 0.1.0 |
| 82 | */ |
| 83 | public function remove( Persistent $instance ): bool { |
| 84 | $instancePerIdentity = $this->instancePerIdentityPerClass[ get_class( $instance ) ]; |
| 85 | if ( !$instancePerIdentity ) { |
| 86 | return false; |
| 87 | } |
| 88 | if ( !array_key_exists( $instancePerIdentity, $instance->getIdentity() ) ) { |
| 89 | return false; |
| 90 | } |
| 91 | unset( $instancePerIdentity[ $instance->getIdentity() ] ); |
| 92 | $this->count--; |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param Persistent[]|null $instances |
| 98 | * @since 0.1.0 |
| 99 | */ |
| 100 | public function removeAll( ?array $instances ) { |
| 101 | if ( $instances ) { |
| 102 | foreach ( $instances as $instance ) { |
| 103 | $this->remove( $instance ); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @param Persistent $instance |
| 110 | * @return Persistent|null |
| 111 | * @since 0.1.0 |
| 112 | */ |
| 113 | public function get( Persistent $instance ): ?Persistent { |
| 114 | $instancePerIdentity = $this->instancePerIdentityPerClass[ get_class( $instance ) ]; |
| 115 | if ( !$instancePerIdentity ) { |
| 116 | return null; |
| 117 | } |
| 118 | if ( array_key_exists( $instancePerIdentity, $instance->getIdentity() ) ) { |
| 119 | return null; |
| 120 | } |
| 121 | return $instancePerIdentity[ $instance->getIdentity() ]; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @return int |
| 126 | * @since 0.1.0 |
| 127 | */ |
| 128 | public function count(): int { |
| 129 | return $this->count; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @return array |
| 134 | * @since 0.1.0 |
| 135 | */ |
| 136 | public function toArray(): array { |
| 137 | $array = []; |
| 138 | foreach ( $this->instancePerIdentityPerClass as $class => $instances ) { |
| 139 | foreach ( $instances as $identity => $instance ) { |
| 140 | $array[] = $instance; |
| 141 | } |
| 142 | } |
| 143 | return $array; |
| 144 | } |
| 145 | |
| 146 | } |