for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/*
* Copyright (c) Nate Brunette.
* Distributed under the MIT License (http://opensource.org/licenses/MIT)
*/
namespace Tebru\Collection;
use ArrayIterator;
/**
* Class ArraySet
*
* @author Nate Brunette <[email protected]>
class ArraySet extends AbstractSet
{
* The set elements
* @var array
private $elements = [];
* Constructor
* @param array $elements
public function __construct(array $elements = [])
$this->addAllArray($elements);
}
* Ensure the element exists in the collection
* Returns true if the collection can contain duplicates,
* and false if it cannot.
* @param mixed $element
* @return bool
public function add($element): bool
if ($this->contains($element)) {
return false;
$this->elements[] = $element;
return true;
* Removes all elements from a collection
* @return void
public function clear(): void
$this->elements = [];
* Removes object if it exists
* Returns true if the element was removed
public function remove($element): bool
if (!$this->contains($element)) {
unset($this->elements[array_search($element, $this->elements, true)]);
* Returns an array of all elements in the collection
* @return array
public function toArray(): array
return array_values($this->elements);
* Retrieve an external iterator
* @return ArrayIterator
public function getIterator(): ArrayIterator
return new ArrayIterator($this->toArray());