Skip to content

Instantly share code, notes, and snippets.

@ker0x
Last active February 20, 2018 17:22
Show Gist options
  • Save ker0x/48fce6c98e714a9ed95ba95c37d89039 to your computer and use it in GitHub Desktop.
Save ker0x/48fce6c98e714a9ed95ba95c37d89039 to your computer and use it in GitHub Desktop.
Example of using Iterator, Countable, ArrayAccess, JsonSerializable and Serializable PHP interfaces
<?php
namespace Aperitips;
/**
* Class Collection.
*
* Example of using Iterator, Countable, ArrayAccess, JsonSerializable and Serializable interfaces
*
* @author Romain Monteil <monteil.romain@gmail.com>
* @package Aperitips
*/
class Collection implements \Iterator, \Countable, \ArrayAccess, \JsonSerializable, \Serializable
{
/**
* @var integer
*/
protected $index = 0;
/**
* @var array
*/
protected $entries = [];
/**
* Collection constructor.
*
* @param array $array
*/
public function __construct(array $array = [])
{
foreach ($array as $value) {
$this->entries[] = $value;
}
}
/**
* @return mixed
*/
public function current()
{
return $this->entries[$this->index];
}
public function next(): void
{
++$this->index;
}
/**
* @return int|mixed
*/
public function key()
{
return $this->index;
}
/**
* @return bool
*/
public function valid(): bool
{
return isset($this->entries[$this->index]);
}
public function rewind(): void
{
$this->index = 0;
}
/**
* @param mixed $offset
*
* @return bool
*/
public function offsetExists($offset): bool
{
return isset($this->entries[$offset]);
}
/**
* @param mixed $offset
*
* @return mixed|null
*/
public function offsetGet($offset)
{
return $this->entries[$offset] ?? null;
}
public function offsetSet($offset, $value): void
{
if ($offset === null) {
$this->entries[] = $value;
} else {
$this->entries[$offset] = $value;
}
}
public function offsetUnset($offset): void
{
unset($this->entries[$offset]);
}
public function count(): int
{
return \count($this->entries);
}
public function toArray(): array
{
return $this->entries;
}
/**
* @return array
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
/**
* @return string
*/
public function serialize(): string
{
return serialize($this->entries);
}
/**
* @param string $serialized
*/
public function unserialize($serialized)
{
$this->entries = unserialize($serialized, [Collection::class]);
}
}
$collection = new Collection([
[
'fistName' => 'Romain',
'lastName' => 'Monteil',
'age' => 30,
'frontEnd' => false,
'backEnd' => true,
'designer' => false,
'funny' => 'HELL YEAH',
],
[
'fistName' => 'Matthieu',
'lastName' => 'Pétel',
'age' => 26,
'frontEnd' => true,
'backEnd' => false,
'designer' => false,
'funny' => 'FUCK NO',
],
[
'fistName' => 'Simon',
'lastName' => 'Kabab',
'age' => 23,
'frontEnd' => false,
'backEnd' => false,
'designer' => true,
'funny' => 'YEAH',
],
]);
echo "--------------------\n";
echo "\Countable\n";
echo "--------------------\n";
var_dump(\count($collection));
echo "\n";
echo "\n";
echo "--------------------\n";
echo "\Iterator\n";
echo "--------------------\n";
foreach ($collection as $key => $value) {
var_dump($key, $value);
echo "\n";
}
echo "\n";
echo "\n";
echo "--------------------\n";
echo "\ArrayAccess\n";
echo "--------------------\n";
var_dump($collection[0]);
var_dump(isset($collection[1]));
unset($collection[2]);
var_dump(isset($collection[2]));
$collection[] = [
'fistName' => 'Thibault',
'lastName' => 'Desmoulin',
'age' => 29,
'frontEnd' => false,
'backEnd' => false,
'designer' => false,
'funny' => 'HELL YEAH',
];
var_dump($collection);
echo "\n";
echo "\n";
echo "--------------------\n";
echo "\Serializable\n";
echo "--------------------\n";
$serialize = serialize($collection);
var_dump($serialize);
echo "\n";
$newCollection = unserialize($serialize, [Collection::class]);
var_dump($newCollection->toArray());
echo "\n";
echo "\n";
echo "--------------------\n";
echo "\JsonSerializable\n";
echo "--------------------\n";
var_dump(json_encode($collection));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment