for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* Trait Enumerable
*
* @filesource Enumerable.php
* @created 30.05.2017
* @package chillerlan\Database\Traits
* @author Smiley <[email protected]>
* @copyright 2017 Smiley
* @license MIT
*/
namespace chillerlan\Database\Traits;
use chillerlan\Database\DBException;
* @link http://api.prototypejs.org/language/Enumerable/
trait Enumerable{
* @var array
protected $array = [];
* @var int
protected $offset = 0;
/*************
* prototype *
*************/
* @link http://api.prototypejs.org/language/Enumerable/prototype/toArray/
* @return array
public function __toArray():array {
return $this->array;
}
* @link http://api.prototypejs.org/language/Enumerable/prototype/each/
* @param callable $callback
* @return $this
public function __each($callback){
$this->__map($callback);
return $this;
* @link http://api.prototypejs.org/language/Enumerable/prototype/collect/
* @link http://api.prototypejs.org/language/Enumerable/prototype/map/
* @throws \Exception
public function __map($callback):array {
if(!is_callable($callback)){
throw new DBException('invalid callback');
$return = [];
foreach($this->array as $index => $element){
$return[$index] = call_user_func_array($callback, [$element, $index]);
return $return;
* @link http://api.prototypejs.org/language/Array/prototype/reverse/
public function __reverse(){
$this->array = array_reverse($this->array);
$this->offset = 0;