Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Typed Collection Examples in PHP
<?php
abstract class GenericCollection implements \ArrayAccess, \Countable, \IteratorAggregate {
protected $collection_items;
/**
*
* ArrayAccess: does the requested key exist?
*
* @param string $key The requested key.
*
* @return bool
*
*/
public function offsetExists($key)
{
return array_key_exists($key, $this->collection_items);
}
/**
*
* ArrayAccess: get a key value.
*
* @param string $key The requested key.
*
* @return mixed
*
*/
public function offsetGet($key)
{
if (array_key_exists($key, $this->collection_items)) {
return $this->collection_items[$key];
} else {
throw new \Exception("offsetGet({$key})");
}
}
/**
*
* ArrayAccess: set a key value.
*
* @param string $key The requested key.
*
* @param string $val The value to set it to.
*
* @return void
*
*/
public function offsetSet($key, $val)
{
if ($key === null) {
//support for $this[] = $val; syntax
$key = $this->count();
if (! $key) {
$key = 0;
}
}
$this->collection_items[$key] = $val;
}
/**
*
* ArrayAccess: unset a key.
*
* @param string $key The requested key.
*
* @return void
*
*/
public function offsetUnset($key)
{
$this->collection_items[$key] = null;
unset($this->collection_items[$key]);
}
public function toArray() {
return $this->collection_items;
}
// IteratorAggregate
public function getIterator() {
return new \ArrayIterator($this->collection_items);
}
// Countable: how many keys are there?
public function count()
{
return count($this->collection_items);
}
// Because we are not type hinting here
// strict typing will be enforced by assuming
// all items in $arr_objs should be of the same
// type as the first item in $arr_objs
public function __construct(...$arr_objs) {
// if count $arr_objs > 0
// gettype of first item
// if it's an object get the class name
// for subsequent items check that they
// are of the same type and (optionally) class
// as first item. Use native array construct to
// do this e.g array_walk . If an item with different
// type is encoutered throw Exception
// Actually move the strict type check into a new sub-class
// StrictlyTypedGenericCollection
$this->collection_items = $arr_objs;
}
}
class AclGroups extends GenericCollection
{
public function __construct(ArrayObject ...$arr_objs) {
$this->collection_items = $arr_objs;
//var_dump($arr_objs);
}
}
$test = new AclGroups(); // legit in this case $this->collection_items will be === []
// inside class AclGroups
$test = new AclGroups( (new ArrayObject([])) ); // legit
$test = new AclGroups( (new ArrayObject([])), (new ArrayObject([])), (new ArrayObject([])) ); // legit
$test = new AclGroups( ...[(new ArrayObject([]))] ); // legit
echo 'All is well!<br>'.PHP_EOL.PHP_EOL;
//$test = new AclGroups((new StdClass())); // blow up here
//$test = new AclGroups( [(new ArrayObject([]))] ); // will also blow up here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.