3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Arr { public static function collapse($array, $prefix = '') { $output = []; foreach($array as $key => $value) { if (Assert::that($value)->array()->result()) { static::merge($output, static::collapse($value, $prefix . $key . '.')); } else { } } return $output; } public static function merge(...$arrays) { Assert::that($arrays)->items('array')->error(); $output = []; foreach($arrays as $array) { $output = array_merge($output, $array); } return $output; } public static function join($glue = '', $array = []) { return implode($glue, $array); } } class Funct { public static function getCaller($offset = 0, $full = false) { return static::nameFromBacktrace(debug_backtrace()[2 + $offset], $full); } public static function nameFromBacktrace($backtrace, $full = false) { if($full) { if(!array_key_exists('type', $backtrace)) return $backtrace['function']; if($backtrace['type'] === '::') { return $backtrace['class'] . '::' . $backtrace['function']; } elseif($backtrace['type'] === '->') { return [$backtrace['class'], $backtrace['function']]; } } return $backtrace['function']; } } class Assert { protected $variable; protected $invalidated = false; protected $invalidatedBy = []; protected function __construct($variable) { $this->variable = $variable; } public static function that($variable) { return new static($variable); } public function string() { return $this->check('is_string'); } public function array() { return $this->check('is_array'); } public function object() { return $this->check('is_object'); } public function boolean() { return $this->check('is_bool'); } public function callable() { return $this->check('is_callable'); } public function integer() { return $this->check('is_int'); } public function null() { return $this->check('is_null'); } public function float() { return $this->check('is_float'); } public function double() { return $this->check('is_double'); } public function resource() { return $this->check('is_resource'); } public function type($type) { return $this->check('is_a', $type); } public function items($type) { return $this->checkAll('is_a', $type); } protected function check(callable $function, ...$parameters) { array_unshift($parameters, $this->variable); if(!call_user_func_array($function, $parameters)) $this->invalidate(); return $this; } protected function checkAll(callable $function, ...$parameters) { foreach($this->variable as $value) { array_unshift($parameters, $value); if(!call_user_func_array($function, $parameters)) $this->invalidate(); break; } return $this; } public function result() { return !$this->invalidated; } public function error() { if(!$this->result()) throw new \AssertionError("The assertion failed at: " . Arr::join(', ', $this->invalidatedBy)); } protected function invalidate($by = null) { $this->invalidated = true; $this->invalidatedBy[] = Funct::getCaller(1); } }

preferences:
57.66 ms | 402 KiB | 5 Q