parent
3da6845b05
commit
d293b3fe7b
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace jrosset\Collections;
|
||||
|
||||
/**
|
||||
* Implementation for unique collection
|
||||
*
|
||||
* @psalm-template TKey of array-key
|
||||
* @template-covariant TValue
|
||||
* @template-extends ImmutableCollection<TKey, TValue>
|
||||
*/
|
||||
trait TUniqueValues {
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function _set ($key, $value): self {
|
||||
if ($this->contains($value)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->elements[$this->_normalizeKey($key)] = $value;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function _add (...$values): self {
|
||||
foreach ($values as $value) {
|
||||
if ($this->contains($value)) {
|
||||
continue;
|
||||
}
|
||||
$this->elements[] = $value;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace jrosset\Collections;
|
||||
|
||||
/**
|
||||
* A collection with unique values
|
||||
*
|
||||
* @psalm-template TKey of array-key
|
||||
* @template-covariant TValue
|
||||
* @template-extends Collection<TKey, TValue>
|
||||
* @template-extends TUniqueValues<TKey, TValue>
|
||||
*/
|
||||
class UniqueCollection extends Collection {
|
||||
use TUniqueValues;
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace jrosset\Collections;
|
||||
|
||||
/**
|
||||
* An immutable collection with unique values
|
||||
*
|
||||
* @psalm-template TKey of array-key
|
||||
* @template-covariant TValue
|
||||
* @template-extends ImmutableCollection<TKey, TValue>
|
||||
* @template-extends TUniqueValues<TKey, TValue>
|
||||
*/
|
||||
class UniqueImmutableCollection extends ImmutableCollection {
|
||||
use TUniqueValues;
|
||||
}
|
Loading…
Reference in New Issue