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

66 lines
1.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 ($key, $value): self {
return $this->_set($key, $value);
}
/**
* @inheritDoc
*/
public function add ($value): self {
$this->elements[] = $value;
return $this;
}
/**
* @inheritDoc
*/
public function clear (): self {
$this->_initialize();
return $this;
}
/**
* @inheritDoc
*/
public function remove ($key): self {
unset($this->elements[$this->_normalizeKey($key)]);
return $this;
}
/**
* @inheritDoc
*/
public function removeValue ($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 ($offset, $value): void {
$this->set($offset, $value);
}
/**
* @inheritDoc
*/
public function offsetUnset ($offset): void {
$this->remove($offset);
}
}