diff --git a/src/Collections/TUniqueValues.php b/src/Collections/TUniqueValues.php new file mode 100644 index 0000000..e4c4dbd --- /dev/null +++ b/src/Collections/TUniqueValues.php @@ -0,0 +1,36 @@ + + */ +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; + } +} \ No newline at end of file diff --git a/src/Collections/UniqueCollection.php b/src/Collections/UniqueCollection.php new file mode 100644 index 0000000..905f7b4 --- /dev/null +++ b/src/Collections/UniqueCollection.php @@ -0,0 +1,15 @@ + + * @template-extends TUniqueValues + */ +class UniqueCollection extends Collection { + use TUniqueValues; +} \ No newline at end of file diff --git a/src/Collections/UniqueImmutableCollection.php b/src/Collections/UniqueImmutableCollection.php new file mode 100644 index 0000000..6ce640d --- /dev/null +++ b/src/Collections/UniqueImmutableCollection.php @@ -0,0 +1,15 @@ + + * @template-extends TUniqueValues + */ +class UniqueImmutableCollection extends ImmutableCollection { + use TUniqueValues; +} \ No newline at end of file