You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PhpCollections/src/Collections/Collection.php

100 lines
2.5 KiB
PHP

<?php
namespace jrosset\Collections;
/**
* A collection
*
* @psalm-template TKey of array-key
* @template-covariant TValue
* @template-extends ImmutableCollection<TKey, TValue>
* @template-implements ICollection<TKey, TValue>
*/
class Collection extends ImmutableCollection implements ICollection {
/**
* @inheritDoc
*/
public function set (int|string $key, mixed $value): self {
return $this->_set($key, $value);
}
/**
* @inheritDoc
*/
public function merge (IImmutableCollection ...$collections): ICollection {
return $this->_merge(...$collections);
}
/**
* @inheritDoc
*/
public function add (mixed ...$values): self {
return $this->_add(...$values);
}
/**
* @inheritDoc
*/
public function addCollection (IImmutableCollection ...$collections): ICollection {
return $this->_addCollection(...$collections);
}
/**
* @inheritDoc
*/
public function prepend (mixed ...$values): ICollection {
array_unshift($this->elements, ...$values);
return $this;
}
/**
* @inheritDoc
*/
public function prependCollection (IImmutableCollection ...$collections): ICollection {
$prepend = array_merge(
...array_map(
function (IImmutableCollection $collection): array {
return $collection->values()->toArray();
},
$collections
)
);
return $this->prepend(...$prepend);
}
/**
* @inheritDoc
*/
public function clear (): self {
$this->_initialize();
return $this;
}
/**
* @inheritDoc
*/
public function remove (int|string $key): self {
unset($this->elements[$this->_normalizeKey($key)]);
return $this;
}
/**
* @inheritDoc
*/
public function removeValue (mixed $value, bool $strict = false): self {
foreach ($this->elements as $currentKey => $currentValue) {
if (($strict && $value === $currentValue) || (!$strict && $value == $currentValue)) {
unset($this->elements[$currentKey]);
}
}
return $this;
}
/**
* @inheritDoc
*/
public function offsetSet (mixed $offset, mixed $value): void {
$this->set($offset, $value);
}
/**
* @inheritDoc
*/
public function offsetUnset (mixed $offset): void {
$this->remove($offset);
}
}