diff options
Diffstat (limited to 'vendor/guzzle/guzzle/src/Guzzle/Inflection')
5 files changed, 220 insertions, 0 deletions
diff --git a/vendor/guzzle/guzzle/src/Guzzle/Inflection/Inflector.php b/vendor/guzzle/guzzle/src/Guzzle/Inflection/Inflector.php new file mode 100644 index 0000000..c699773 --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Inflection/Inflector.php @@ -0,0 +1,38 @@ +<?php + +namespace Guzzle\Inflection; + +/** + * Default inflection implementation + */ +class Inflector implements InflectorInterface +{ + /** @var InflectorInterface */ + protected static $default; + + /** + * Get the default inflector object that has support for caching + * + * @return MemoizingInflector + */ + public static function getDefault() + { + // @codeCoverageIgnoreStart + if (!self::$default) { + self::$default = new MemoizingInflector(new self()); + } + // @codeCoverageIgnoreEnd + + return self::$default; + } + + public function snake($word) + { + return ctype_lower($word) ? $word : strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $word)); + } + + public function camel($word) + { + return str_replace(' ', '', ucwords(strtr($word, '_-', ' '))); + } +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Inflection/InflectorInterface.php b/vendor/guzzle/guzzle/src/Guzzle/Inflection/InflectorInterface.php new file mode 100644 index 0000000..321d718 --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Inflection/InflectorInterface.php @@ -0,0 +1,27 @@ +<?php + +namespace Guzzle\Inflection; + +/** + * Inflector interface used to convert the casing of words + */ +interface InflectorInterface +{ + /** + * Converts strings from camel case to snake case (e.g. CamelCase camel_case). + * + * @param string $word Word to convert to snake case + * + * @return string + */ + public function snake($word); + + /** + * Converts strings from snake_case to upper CamelCase + * + * @param string $word Value to convert into upper CamelCase + * + * @return string + */ + public function camel($word); +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Inflection/MemoizingInflector.php b/vendor/guzzle/guzzle/src/Guzzle/Inflection/MemoizingInflector.php new file mode 100644 index 0000000..32968d6 --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Inflection/MemoizingInflector.php @@ -0,0 +1,70 @@ +<?php + +namespace Guzzle\Inflection; + +/** + * Decorator used to add memoization to previously inflected words + */ +class MemoizingInflector implements InflectorInterface +{ + /** @var array Array of cached inflections */ + protected $cache = array( + 'snake' => array(), + 'camel' => array() + ); + + /** @var int Max entries per cache */ + protected $maxCacheSize; + + /** @var InflectorInterface Decorated inflector */ + protected $decoratedInflector; + + /** + * @param InflectorInterface $inflector Inflector being decorated + * @param int $maxCacheSize Maximum number of cached items to hold per cache + */ + public function __construct(InflectorInterface $inflector, $maxCacheSize = 500) + { + $this->decoratedInflector = $inflector; + $this->maxCacheSize = $maxCacheSize; + } + + public function snake($word) + { + if (!isset($this->cache['snake'][$word])) { + $this->pruneCache('snake'); + $this->cache['snake'][$word] = $this->decoratedInflector->snake($word); + } + + return $this->cache['snake'][$word]; + } + + /** + * Converts strings from snake_case to upper CamelCase + * + * @param string $word Value to convert into upper CamelCase + * + * @return string + */ + public function camel($word) + { + if (!isset($this->cache['camel'][$word])) { + $this->pruneCache('camel'); + $this->cache['camel'][$word] = $this->decoratedInflector->camel($word); + } + + return $this->cache['camel'][$word]; + } + + /** + * Prune one of the named caches by removing 20% of the cache if it is full + * + * @param string $cache Type of cache to prune + */ + protected function pruneCache($cache) + { + if (count($this->cache[$cache]) == $this->maxCacheSize) { + $this->cache[$cache] = array_slice($this->cache[$cache], $this->maxCacheSize * 0.2); + } + } +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Inflection/PreComputedInflector.php b/vendor/guzzle/guzzle/src/Guzzle/Inflection/PreComputedInflector.php new file mode 100644 index 0000000..db37e4f --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Inflection/PreComputedInflector.php @@ -0,0 +1,59 @@ +<?php + +namespace Guzzle\Inflection; + +/** + * Decorator used to add pre-computed inflection mappings to an inflector + */ +class PreComputedInflector implements InflectorInterface +{ + /** @var array Array of pre-computed inflections */ + protected $mapping = array( + 'snake' => array(), + 'camel' => array() + ); + + /** @var InflectorInterface Decorated inflector */ + protected $decoratedInflector; + + /** + * @param InflectorInterface $inflector Inflector being decorated + * @param array $snake Hash of pre-computed camel to snake + * @param array $camel Hash of pre-computed snake to camel + * @param bool $mirror Mirror snake and camel reflections + */ + public function __construct(InflectorInterface $inflector, array $snake = array(), array $camel = array(), $mirror = false) + { + if ($mirror) { + $camel = array_merge(array_flip($snake), $camel); + $snake = array_merge(array_flip($camel), $snake); + } + + $this->decoratedInflector = $inflector; + $this->mapping = array( + 'snake' => $snake, + 'camel' => $camel + ); + } + + public function snake($word) + { + return isset($this->mapping['snake'][$word]) + ? $this->mapping['snake'][$word] + : $this->decoratedInflector->snake($word); + } + + /** + * Converts strings from snake_case to upper CamelCase + * + * @param string $word Value to convert into upper CamelCase + * + * @return string + */ + public function camel($word) + { + return isset($this->mapping['camel'][$word]) + ? $this->mapping['camel'][$word] + : $this->decoratedInflector->camel($word); + } +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Inflection/composer.json b/vendor/guzzle/guzzle/src/Guzzle/Inflection/composer.json new file mode 100644 index 0000000..93f9e7b --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Inflection/composer.json @@ -0,0 +1,26 @@ +{ + "name": "guzzle/inflection", + "description": "Guzzle inflection component", + "homepage": "http://guzzlephp.org/", + "keywords": ["inflection", "guzzle"], + "license": "MIT", + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "require": { + "php": ">=5.3.2" + }, + "autoload": { + "psr-0": { "Guzzle\\Inflection": "" } + }, + "target-dir": "Guzzle/Inflection", + "extra": { + "branch-alias": { + "dev-master": "3.7-dev" + } + } +} |