123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php namespace Laravel;
- class Pluralizer {
-
- protected $config;
-
- protected $plural = array();
-
- protected $singular = array();
-
- public function __construct($config)
- {
- $this->config = $config;
- }
-
- public function singular($value)
- {
-
-
-
- if (isset($this->singular[$value]))
- {
- return $this->singular[$value];
- }
-
-
-
- $irregular = $this->config['irregular'];
- $result = $this->auto($value, $this->config['singular'], $irregular);
- return $this->singular[$value] = $result ?: $value;
- }
-
- public function plural($value, $count = 2)
- {
- if ((int) $count == 1) return $value;
-
-
-
- if (isset($this->plural[$value]))
- {
- return $this->plural[$value];
- }
-
-
-
- $irregular = array_flip($this->config['irregular']);
- $result = $this->auto($value, $this->config['plural'], $irregular);
- return $this->plural[$value] = $result;
- }
-
- protected function auto($value, $source, $irregular)
- {
-
-
-
- if (in_array(Str::lower($value), $this->config['uncountable']))
- {
- return $value;
- }
-
-
-
- foreach ($irregular as $irregular => $pattern)
- {
- if (preg_match($pattern = '/'.$pattern.'$/i', $value))
- {
- return preg_replace($pattern, $irregular, $value);
- }
- }
-
-
-
- foreach ($source as $pattern => $inflected)
- {
- if (preg_match($pattern, $value))
- {
- return preg_replace($pattern, $inflected, $value);
- }
- }
- }
- }
|