@ 2015-12-18T02:49:08Z <?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);
var_dump($this->invalidated, $this->invalidatedBy);
}
}
Assert::that(true)->string();
Assert::that(1)->string();
Assert::that([])->string();
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.0.0 - 7.0.20 , 7.1.0 - 7.1.20 , 7.2.0 - 7.2.33 , 7.3.16 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.4 , 8.3.6 bool(true)
array(1) {
[0]=>
string(6) "string"
}
bool(true)
array(1) {
[0]=>
string(6) "string"
}
bool(true)
array(1) {
[0]=>
string(6) "string"
}
Output for 8.3.5 Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
bool(true)
array(1) {
[0]=>
string(6) "string"
}
bool(true)
array(1) {
[0]=>
string(6) "string"
}
bool(true)
array(1) {
[0]=>
string(6) "string"
}
Output for 5.6.0 - 5.6.25 Parse error: syntax error, unexpected 'array' (T_ARRAY), expecting identifier (T_STRING) in /in/XNh0T on line 78
Process exited with code 255 . Output for 5.5.0 - 5.5.38 Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/XNh0T on line 21
Process exited with code 255 . preferences:dark mode live preview
180.94 ms | 401 KiB | 224 Q