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];
+ }
+ }
+}
--- /dev/null
+<?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);
+ }
+}
+
$length = strlen($needle);
return !$length || substr($haystack, -$length) === $needle;
}
+
+ public static function underscore($str)
+ {
+ return strtolower(preg_replace('/([^_])([A-Z])/', '$1_$2', $str));
+ }
}
private static $symbols = array();
private $name;
- private function __construct()
+ private function __construct($name)
{
+ $this->name = $name;
}
function __clone()
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)
$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'));
+ }
}