1: <?php
2:
3: namespace Guzzle\Inflection;
4:
5: /**
6: * Decorator used to add pre-computed inflection mappings to an inflector
7: */
8: class PreComputedInflector implements InflectorInterface
9: {
10: /**
11: * @var array Array of pre-computed inflections
12: */
13: protected $mapping = array(
14: 'snake' => array(),
15: 'camel' => array()
16: );
17:
18: /**
19: * @var InflectorInterface Decorated inflector
20: */
21: protected $decoratedInflector;
22:
23: /**
24: * Decorate using a pre-computed map.
25: *
26: * @param InflectorInterface $inflector Inflector being decorated
27: * @param array $snake Hash of pre-computed camel to snake
28: * @param array $camel Hash of pre-computed snake to camel
29: * @param bool $mirror Mirror snake and camel reflections
30: */
31: public function __construct(InflectorInterface $inflector, array $snake = array(), array $camel = array(), $mirror = false)
32: {
33: if ($mirror) {
34: $camel = array_merge(array_flip($snake), $camel);
35: $snake = array_merge(array_flip($camel), $snake);
36: }
37:
38: $this->decoratedInflector = $inflector;
39: $this->mapping = array(
40: 'snake' => $snake,
41: 'camel' => $camel
42: );
43: }
44:
45: /**
46: * {@inheritdoc}
47: */
48: public function snake($word)
49: {
50: return isset($this->mapping['snake'][$word])
51: ? $this->mapping['snake'][$word]
52: : $this->decoratedInflector->snake($word);
53: }
54:
55: /**
56: * Converts strings from snake_case to upper CamelCase
57: *
58: * @param string $word Value to convert into upper CamelCase
59: *
60: * @return string
61: */
62: public function camel($word)
63: {
64: return isset($this->mapping['camel'][$word])
65: ? $this->mapping['camel'][$word]
66: : $this->decoratedInflector->camel($word);
67: }
68: }
69: