Last active
February 24, 2016 23:13
-
-
Save arleighdickerson/e26ae8e1f4c608812eaa to your computer and use it in GitHub Desktop.
No attempt at being canonical
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace common\helpers; | |
use yii\base\UnknownPropertyException; | |
use yii\helpers\ArrayHelper; | |
/** | |
* @author Arleigh Dickerson | |
* | |
* an array helper class to make functional programming more accessible | |
* | |
* Class Underscore | |
* @package fu\core\common\helpers | |
*/ | |
class Underscore extends \ArrayObject { | |
public function filter($predicate) { | |
return self::wrap(array_filter((array)$this, $predicate)); | |
} | |
public function map($callback) { | |
return self::wrap(array_map($callback, (array)$this)); | |
} | |
public function fold($callback, $initial = null) { | |
return self::wrap(array_reduce((array)$this, $callback, $initial)); | |
} | |
public function diff($other) { | |
return self::wrap(array_diff((array)$this, $other)); | |
} | |
public function withoutKeys($keys) { | |
return self::wrap(array_diff_key((array)$this, array_flip($keys))); | |
} | |
public function intersect($other) { | |
return self::wrap(array_intersect((array)$this, $other)); | |
} | |
public function intersectKey($other) { | |
return self::wrap(array_intersect_key((array)$this, $other)); | |
} | |
public function join($glue) { | |
return self::wrap(join($glue, (array)$this)); | |
} | |
public function contains($value) { | |
return in_array($value, (array)$this); | |
} | |
public function containsKey($value) { | |
return array_key_exists($value, (array)$this); | |
} | |
public function merge($other) { | |
return self::wrap(array_merge((array)$this, (array)$other)); | |
} | |
public function toLower() { | |
return $this->map(function ($str) { | |
return strtolower((string)$str); | |
}); | |
} | |
public function one($default = false) { | |
$array = array_values($this->__); | |
if ($default === false) { | |
$count = count($array); | |
assert($count === 1); | |
} | |
return __($array)->getValue(0, $default); | |
} | |
public function asSet() { | |
return self::wrap(array_combine((array)$this, (array)$this)); | |
} | |
public function __get($name) { | |
if ($name === '__') { | |
return (array)$this; | |
} | |
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name); | |
} | |
public function __call($name, $args = []) { | |
array_unshift($args, (array)$this); | |
return self::wrap(call_user_func_array([ArrayHelper::class, $name], $args)); | |
} | |
private static function wrap($obj) { | |
if ($obj instanceof self) { | |
return $obj; | |
} | |
if (is_array($obj)) { | |
return new self($obj); | |
} | |
if ($obj instanceof \ArrayObject) { | |
return new self((array)$obj); | |
} | |
return $obj; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment