Add prepend methods

2.x 2.3.0
Julien Rosset 3 years ago
parent 89f89d8c19
commit 9993701f58

@ -23,6 +23,7 @@ class Collection extends ImmutableCollection implements ICollection {
public function merge (IImmutableCollection ...$collections): ICollection { public function merge (IImmutableCollection ...$collections): ICollection {
return $this->_merge(...$collections); return $this->_merge(...$collections);
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
@ -36,6 +37,25 @@ class Collection extends ImmutableCollection implements ICollection {
return $this->_addCollection(...$collections); return $this->_addCollection(...$collections);
} }
/**
* @inheritDoc
*/
public function prepend (...$values): ICollection {
array_unshift($this->elements, ...$values);
return $this;
}
/**
* @inheritDoc
*/
public function prependCollection (IImmutableCollection ...$collections): ICollection {
foreach ($collections as $collection) {
foreach ($collection as $value) {
$this->prepend($value);
}
}
return $this;
}
/** /**
* @inheritDoc * @inheritDoc
*/ */

@ -30,6 +30,7 @@ interface ICollection extends IImmutableCollection {
* @return $this * @return $this
*/ */
public function merge (IImmutableCollection ...$collections): self; public function merge (IImmutableCollection ...$collections): self;
/** /**
* Add one or multiple values * Add one or multiple values
* *
@ -49,6 +50,25 @@ interface ICollection extends IImmutableCollection {
*/ */
public function addCollection (IImmutableCollection ...$collections): self; public function addCollection (IImmutableCollection ...$collections): self;
/**
* Add one or multiple values at the beginning of the current collection
*
* @param mixed ...$values The values
*
* @psalm-param TValue ...$values
*
* @return $this
*/
public function prepend (...$values): self;
/**
* Add the <b>values</b> of one or multiple collections at the beginning of the current collection
*
* @param IImmutableCollection ...$collections The collections to add
*
* @return mixed
*/
public function prependCollection (IImmutableCollection ...$collections): self;
/** /**
* Empties the collection * Empties the collection
* *

@ -24,8 +24,8 @@ class Measure {
*/ */
function getMeasures (): Collection { function getMeasures (): Collection {
$measures = new Collection(); $measures = new Collection();
$measures->add(new Measure(38));
$measures->add(new Measure(27), new Measure(34)); $measures->add(new Measure(27), new Measure(34));
$measures->prepend(new Measure(38));
return $measures; return $measures;
} }

Loading…
Cancel
Save