<?php
/*
* This file is part of the Geotools library.
*
* (c) Antoine Corcy <contact@sbin.dk>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Geotools;
/**
* @author Gabriel Bull <me@gabrielbull.com>
*/
class ArrayCollection implements \Countable, \IteratorAggregate, \ArrayAccess, \JsonSerializable
{
/**
* @var array
*/
protected $elements;
/**
* @param array $elements
*/
public function __construct(array $elements = array())
{
$this->elements = $elements;
}
/**
* @return array
*/
public function toArray()
{
return $this->elements;
}
/**
* {@inheritDoc}
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->elements;
}
/**
* {@inheritDoc}
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return isset($this->elements[$offset]) || array_key_exists($offset, $this->elements);
}
/**
* {@inheritDoc}
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* {@inheritDoc}
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
$this->set($offset, $value);
}
/**
* {@inheritDoc}
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
return $this->remove($offset);
}
/**
* {@inheritDoc}
*/
#[\ReturnTypeWillChange]
public function count()
{
return count($this->elements);
}
/**
* {@inheritDoc}
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
return new \ArrayIterator($this->elements);
}
/**
* @param string $key
* @return null|mixed
*/
public function get($key)
{
if (isset($this->elements[$key])) {
return $this->elements[$key];
}
return null;
}
/**
* @param string $key
* @param mixed $value
*/
public function set($key, $value)
{
$this->elements[$key] = $value;
}
/**
* @param mixed $value
* @return bool
*/
public function add($value)
{
$this->elements[] = $value;
return true;
}
/**
* @param string $key
* @return null|mixed
*/
public function remove($key)
{
if (isset($this->elements[$key]) || array_key_exists($key, $this->elements)) {
$removed = $this->elements[$key];
unset($this->elements[$key]);
return $removed;
}
return null;
}
/**
* @param ArrayCollection $collection
* @return ArrayCollection
*/
public function merge(ArrayCollection $collection)
{
$merged = clone $this;
foreach ($collection as $key => $element) {
if (is_int($key)) {
$merged->add($element);
} else {
$merged->set($key, $element);
}
}
return $merged;
}
}