]> git.andy128k.dev Git - missing-tools.git/commitdiff
fluent api for arrays
authorAndrey Kutejko <andy128k@gmail.com>
Sun, 24 Aug 2014 18:59:36 +0000 (21:59 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sun, 24 Aug 2014 18:59:36 +0000 (21:59 +0300)
src/array.php
src/symbol.php [new file with mode: 0644]

index e11671c21abb1210787e62531679f0439556b8a8..f2f38f7cf786d4cb3eb04e8573ebbcffab245001 100644 (file)
@@ -2,8 +2,86 @@
 
 namespace PFF;
 
-final class Arr
+use \PFF\Symbol as S;
+
+final class Arr implements \ArrayAccess
 {
+    private $arr;
+
+    public function __construct($arr)
+    {
+        $this->arr = $arr;
+    }
+
+    public static function create($arr)
+    {
+        return new self($arr);
+    }
+
+    public function arr()
+    {
+        return $this->arr;
+    }
+
+    public function map(/* $func, ... $args */)
+    {
+        $args = func_get_args();
+        $func = array_unshift($args);
+        $pos = array_search(S::_(), $args, true);
+        if (!$pos)
+            $pos = 0;
+
+        $result = array();
+        foreach ($this->arr as $item) {
+            $args[$pos] = $item;
+            $result[] = call_user_func_array($func, $args);
+        }
+
+        return new self($result);
+    }
+
+    public function pluck($key)
+    {
+        $result = array();
+        foreach ($this->arr as $item)
+            $result[] = is_object($item) ? $item->$key : $item[$key];
+        return new self($result);
+    }
+
+    /* strings */
+
+    public function join($separator)
+    {
+        return implode($separator, $this->arr);
+    }
+
+    /* ArrayAccess */
+
+    public function offsetSet($offset, $value)
+    {
+        if (is_null($offset))
+            $this->arr[] = $value;
+        else
+            $this->arr[$offset] = $value;
+    }
+
+    public function offsetExists($offset)
+    {
+        return isset($this->arr[$offset]);
+    }
+
+    public function offsetUnset($offset)
+    {
+        unset($this->arr[$offset]);
+    }
+
+    public function offsetGet($offset)
+    {
+        return isset($this->arr[$offset]) ? $this->arr[$offset] : null;
+    }
+
+    /* */
+
     public static function get($array, $key, $default=null)
     {
         if (array_key_exists($key, $array))
diff --git a/src/symbol.php b/src/symbol.php
new file mode 100644 (file)
index 0000000..8c140b0
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+
+namespace PFF;
+
+final class Symbol
+{
+    private static $symbols = array();
+    private $name;
+
+    private function __construct()
+    {
+    }
+
+    function __clone()
+    {
+        throw new \Exception('Cloning of symbols is not allowed.');
+    }
+
+    function __wakeup()
+    {
+        throw new \Exception('Deserialization of symbols is not allowed.');
+    }
+
+    public function name()
+    {
+        return $this->name;
+    }
+
+    public static function intern($name)
+    {
+        if (array_key_exists($name, self::$symbols))
+            return self::$symbols[$name];
+
+        $symbol = new self;
+        $symbol->name = $name;
+        self::$symbols[$name] = $symbol;
+        return $symbol;
+    }
+
+    public static function __callStatic($name, $args)
+    {
+        return self::intern($name);
+    }
+}
+