Skip to content

Instantly share code, notes, and snippets.

@Purexo
Created March 8, 2017 15:30
Show Gist options
  • Save Purexo/82d4008042aef8b921419926a36cbc98 to your computer and use it in GitHub Desktop.
Save Purexo/82d4008042aef8b921419926a36cbc98 to your computer and use it in GitHub Desktop.
Une liste avec une taille limite et des items qui sortent au fur et à mesure.
<?php
/**
* Created by PhpStorm.
* User: Purexo
* Date: 08/03/17
* Time: 10:45
*/
namespace VillageBundle\Utils;
/**
* Class RolledList
*
* Une RolledList est une liste avec une taille maximum,
* Les ajouts ce font en début de liste,
* lorque la taille maximum est dépassé le dernier item est enlevé.
*
* Voyez ça comme un tube ou l'on pousse des objets à l'interieur, et quand la longueur est atteintes, les objets
* sortent au fur et à mesure.
*
* .====
* -> .====
* -> .====
* -> .====
* -> .====
* -> .====.
* -> .====..
*
* le ==== représente le tube
* les . représentent les items
* -> corresponds à des push
* les seuls items accessibles sont dans le tube
*
* Ça ce base sur un array, mais ça ne supporte pas les associative array.
* Donc lors du construct, si vous passez un array associatif, les clé seront ignoré, ça ne prendra en compte que les
* valeurs.
*
* Implémente tout le necessaire pour être utilisé comme un array, et itéré comme un array
*
* @package VillageBundle\Utils
*/
class RolledList implements \ArrayAccess, \SeekableIterator, \Countable, \Serializable
{
/**
* @var array
*/
private $array;
/**
* @var int
*/
private $maxlength;
/**
* RolledList constructor.
*
* @param int $maxlength
* @param array $array
*/
function __construct ($maxlength = 5, $array = [])
{
$this->init($maxlength, $array);
}
function init($maxlength, $array) {
$this->maxlength = $maxlength;
$this->array = [];
foreach ($array as $value) {
$this[] = $value;
}
}
/**
* Return the current element
* @link http://php.net/manual/en/iterator.current.php
* @return mixed Can return any type.
* @since 5.0.0
*/
public function current ()
{
return current($this->array);
}
/**
* Move forward to next element
* @link http://php.net/manual/en/iterator.next.php
* @return void Any returned value is ignored.
* @since 5.0.0
*/
public function next ()
{
next($this->array);
}
/**
* Return the key of the current element
* @link http://php.net/manual/en/iterator.key.php
* @return mixed scalar on success, or null on failure.
* @since 5.0.0
*/
public function key ()
{
return key($this->array);
}
/**
* Checks if current position is valid
* @link http://php.net/manual/en/iterator.valid.php
* @return boolean The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure.
* @since 5.0.0
*/
public function valid ()
{
return $this->offsetExists(key($this->array));
}
/**
* Rewind the Iterator to the first element
* @link http://php.net/manual/en/iterator.rewind.php
* @return void Any returned value is ignored.
* @since 5.0.0
*/
public function rewind ()
{
reset($this->array);
}
/**
* Whether a offset exists
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
*
* @param mixed $offset <p>
* An offset to check for.
* </p>
*
* @return boolean true on success or false on failure.
* </p>
* <p>
* The return value will be casted to boolean if non-boolean was returned.
* @since 5.0.0
*/
public function offsetExists ($offset)
{
return key_exists($offset, $this->array);
}
/**
* Offset to retrieve
* @link http://php.net/manual/en/arrayaccess.offsetget.php
*
* @param mixed $offset <p>
* The offset to retrieve.
* </p>
*
* @return mixed Can return all value types.
* @since 5.0.0
*/
public function offsetGet ($offset)
{
return $this->array[$offset];
}
/**
* Offset to set
*
* /!\ Ici ce trouve toute la magie de la RolledList, on ne prends pas en compte l'offset, juste la value
*
* @link http://php.net/manual/en/arrayaccess.offsetset.php
*
* @param mixed $offset <p>
* The offset to assign the value to.
* </p>
* @param mixed $value <p>
* The value to set.
* </p>
*
* @return void
* @since 5.0.0
*/
public function offsetSet ($offset, $value)
{
if ($this->count() >= $this->maxlength) {
array_pop($this->array);
}
array_unshift($this->array, $value);
}
/**
* Offset to unset
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
*
* @param mixed $offset <p>
* The offset to unset.
* </p>
*
* @return void
* @since 5.0.0
*/
public function offsetUnset ($offset)
{
unset($this->array[$offset]);
}
/**
* String representation of object
* @link http://php.net/manual/en/serializable.serialize.php
* @return string the string representation of the object or null
* @since 5.1.0
*/
public function serialize ()
{
return serialize([
'maxlength' => $this->maxlength,
'array' => $this->array
]);
}
/**
* Constructs the object
* @link http://php.net/manual/en/serializable.unserialize.php
*
* @param string $serialized <p>
* The string representation of the object.
* </p>
*
* @return void
* @since 5.1.0
*/
public function unserialize ($serialized)
{
$unserialized = unserialize($serialized);
$this->init($unserialized['maxlength'], $unserialized['array']);
}
/**
* Count elements of an object
* @link http://php.net/manual/en/countable.count.php
* @return int The custom count as an integer.
* </p>
* <p>
* The return value is cast to an integer.
* @since 5.1.0
*/
public function count ()
{
return count($this->array);
}
/**
* Seeks to a position
* @link http://php.net/manual/en/seekableiterator.seek.php
*
* @param int $position <p>
* The position to seek to.
* </p>
*
* @return void
* @since 5.1.0
*/
public function seek ($position)
{
$this->rewind();
for ($i = 0; $i < $position; $i++) {
$this->next();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment