1: <?php namespace Laravel;
2:
3: class Pluralizer {
4:
5: /**
6: * The "strings" configuration array.
7: *
8: * @var array
9: */
10: protected $config;
11:
12: /**
13: * The cached copies of the plural inflections.
14: */
15: protected $plural = array();
16:
17: /**
18: * The cached copies of the singular inflections.
19: *
20: * @var array
21: */
22: protected $singular = array();
23:
24: /**
25: * Create a new pluralizer instance.
26: *
27: * @param array $config
28: * @return void
29: */
30: public function __construct($config)
31: {
32: $this->config = $config;
33: }
34:
35: /**
36: * Get the singular form of the given word.
37: *
38: * @param string $value
39: * @return string
40: */
41: public function singular($value)
42: {
43: // First we'll check the cache of inflected values. We cache each word that
44: // is inflected so we don't have to spin through the regular expressions
45: // each time we need to inflect a given value for the developer.
46: if (isset($this->singular[$value]))
47: {
48: return $this->singular[$value];
49: }
50:
51: // English words may be automatically inflected using regular expressions.
52: // If the word is English, we'll just pass off the word to the automatic
53: // inflection method and return the result, which is cached.
54: $irregular = $this->config['irregular'];
55:
56: $result = $this->auto($value, $this->config['singular'], $irregular);
57:
58: return $this->singular[$value] = $result ?: $value;
59: }
60:
61: /**
62: * Get the plural form of the given word.
63: *
64: * @param string $value
65: * @param int $count
66: * @return string
67: */
68: public function plural($value, $count = 2)
69: {
70: if ($count == 1) return $value;
71:
72: // First we'll check the cache of inflected values. We cache each word that
73: // is inflected so we don't have to spin through the regular expressions
74: // each time we need to inflect a given value for the developer.
75: if (isset($this->plural[$value]))
76: {
77: return $this->plural[$value];
78: }
79:
80: // English words may be automatically inflected using regular expressions.
81: // If the word is English, we'll just pass off the word to the automatic
82: // inflection method and return the result, which is cached.
83: $irregular = array_flip($this->config['irregular']);
84:
85: $result = $this->auto($value, $this->config['plural'], $irregular);
86:
87: return $this->plural[$value] = $result;
88: }
89:
90: /**
91: * Perform auto inflection on an English word.
92: *
93: * @param string $value
94: * @param array $source
95: * @param array $irregular
96: * @return string
97: */
98: protected function auto($value, $source, $irregular)
99: {
100: // If the word hasn't been cached, we'll check the list of words that
101: // that are "uncountable". This should be a quick look up since we
102: // can just hit the array directly for the value.
103: if (in_array(Str::lower($value), $this->config['uncountable']))
104: {
105: return $value;
106: }
107:
108: // Next, we will check the "irregular" patterns, which contain words
109: // like "children" and "teeth" which can not be inflected using the
110: // typically used regular expression matching approach.
111: foreach ($irregular as $irregular => $pattern)
112: {
113: if (preg_match($pattern = '/'.$pattern.'$/i', $value))
114: {
115: return preg_replace($pattern, $irregular, $value);
116: }
117: }
118:
119: // Finally we'll spin through the array of regular expressions and
120: // and look for matches for the word. If we find a match we will
121: // cache and return the inflected value for quick look up.
122: foreach ($source as $pattern => $inflected)
123: {
124: if (preg_match($pattern, $value))
125: {
126: return preg_replace($pattern, $inflected, $value);
127: }
128: }
129: }
130:
131: }