]> git.andy128k.dev Git - missing-tools.git/commitdiff
WIP improvements
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 17 Dec 2016 22:16:18 +0000 (23:16 +0100)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 17 Dec 2016 22:16:18 +0000 (23:16 +0100)
src/array.php
src/key.php [new file with mode: 0644]
src/str.php
src/symbol.php
t/StrTest.php

index 7978a3e08b9547df08fd3c4d093d4ec895dc900f..11f188fd85375f7c34ec6d7a133de07fb78bafb6 100644 (file)
@@ -85,3 +85,35 @@ final class Arr
         return $result;
     }
 }
+
+final class Map
+{
+    public $data = array();
+
+    static function create()
+    {
+        return new self;
+    }
+
+    function __set($name, $value)
+    {
+        $this->data[$name] = $value;
+    }
+
+    function __get($name)
+    {
+        return $this->data[$name];
+    }
+
+    function __call($method, $args)
+    {
+        if (preg_match('/^set([A-Z]\w+)$/', $method, $m)) {
+            $name = \PFF\Str::underscore($m[1]);
+            $this->data[$name] = $args[0];
+            return $this;
+        } else {
+            $name = \PFF\Str::underscore($method);
+            return $this->data[$name];
+        }
+    }
+}
diff --git a/src/key.php b/src/key.php
new file mode 100644 (file)
index 0000000..07b02c9
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+namespace PFF;
+
+class Key
+{
+    public static function generate($scope, $exclude=array())
+    {
+        return self::gen($scope, 'in_array', $exclude);
+    }
+
+    public static function generateKey($scope, $dictionary)
+    {
+        return self::gen($scope, 'array_key_exists', $dictionary);
+    }
+
+    protected static function gen($scope, $exists, $haystack)
+    {
+        $index = 0;
+        do {
+            ++$index;
+            $key = "__key_{$scope}_{$index}";
+        } while (call_user_func($exists, $key, $haystack));
+        return $key;
+    }
+
+    public static function isGenerated($key, $scope)
+    {
+        return 1 === preg_match('/^__key_'.preg_quote($scope, '/').'_[0-9]+$/', $key);
+    }
+}
+
index 31785846ba855767bf442703ea36a6d3c196d961..0ea01e95dcdad4c88cab0031db6de75cd7c4f283 100644 (file)
@@ -14,5 +14,10 @@ final class Str
         $length = strlen($needle);
         return !$length || substr($haystack, -$length) === $needle;
     }
+
+    public static function underscore($str)
+    {
+        return strtolower(preg_replace('/([^_])([A-Z])/', '$1_$2', $str));
+    }
 }
 
index 8c140b0bf29e8d87e149dcb1cef573ce6d284b34..fe6406393c8708e9d002ed83afd5183e9230f787 100644 (file)
@@ -7,8 +7,9 @@ final class Symbol
     private static $symbols = array();
     private $name;
 
-    private function __construct()
+    private function __construct($name)
     {
+        $this->name = $name;
     }
 
     function __clone()
@@ -28,13 +29,13 @@ final class Symbol
 
     public static function intern($name)
     {
-        if (array_key_exists($name, self::$symbols))
+        if (array_key_exists($name, self::$symbols)) {
             return self::$symbols[$name];
-
-        $symbol = new self;
-        $symbol->name = $name;
-        self::$symbols[$name] = $symbol;
-        return $symbol;
+        } else {
+            $symbol = new self($name);
+            self::$symbols[$name] = $symbol;
+            return $symbol;
+        }
     }
 
     public static function __callStatic($name, $args)
index adf76f23cb9e9b5570a36eb3b481f346013b4929..6cb19ae7ce731036303ae6719d8cb9d3f3a852ce 100644 (file)
@@ -21,5 +21,20 @@ class StrTest extends PHPUnit_Framework_TestCase
         $this->assertTrue(\PFF\Str::endsWith('abc', ''));
         $this->assertFalse(\PFF\Str::endsWith('abc', ' '));
     }
+
+    public function testUnderscore()
+    {
+        $this->assertEquals('', \PFF\Str::underscore(null));
+        $this->assertEquals('7.5', \PFF\Str::underscore(7.5));
+        $this->assertEquals('', \PFF\Str::underscore(''));
+        $this->assertEquals('abc', \PFF\Str::underscore('abc'));
+        $this->assertEquals('abc', \PFF\Str::underscore('Abc'));
+        $this->assertEquals('a_bc', \PFF\Str::underscore('ABc'));
+        $this->assertEquals('a_bc', \PFF\Str::underscore('aBc'));
+        $this->assertEquals('camel_case', \PFF\Str::underscore('CamelCase'));
+        $this->assertEquals('camel_case', \PFF\Str::underscore('camelCase'));
+        $this->assertEquals('snake_case', \PFF\Str::underscore('snake_case'));
+        $this->assertEquals('snake_case', \PFF\Str::underscore('snake_Case'));
+    }
 }