Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Support;
4
 
5
use Doctrine\Inflector\InflectorFactory;
6
 
7
class Pluralizer
8
{
9
    /**
10
     * The cached inflector instance.
11
     *
12
     * @var static
13
     */
14
    protected static $inflector;
15
 
16
    /**
17
     * The language that should be used by the inflector.
18
     *
19
     * @var string
20
     */
21
    protected static $language = 'english';
22
 
23
    /**
24
     * Uncountable non-nouns word forms.
25
     *
26
     * Contains words supported by Doctrine/Inflector/Rules/English/Uninflected.php
27
     *
28
     * @var string[]
29
     */
30
    public static $uncountable = [
31
        'cattle',
32
        'kin',
33
        'recommended',
34
        'related',
35
    ];
36
 
37
    /**
38
     * Get the plural form of an English word.
39
     *
40
     * @param  string  $value
41
     * @param  int|array|\Countable  $count
42
     * @return string
43
     */
44
    public static function plural($value, $count = 2)
45
    {
46
        if (is_countable($count)) {
47
            $count = count($count);
48
        }
49
 
50
        if ((int) abs($count) === 1 || static::uncountable($value) || preg_match('/^(.*)[A-Za-z0-9\x{0080}-\x{FFFF}]$/u', $value) == 0) {
51
            return $value;
52
        }
53
 
54
        $plural = static::inflector()->pluralize($value);
55
 
56
        return static::matchCase($plural, $value);
57
    }
58
 
59
    /**
60
     * Get the singular form of an English word.
61
     *
62
     * @param  string  $value
63
     * @return string
64
     */
65
    public static function singular($value)
66
    {
67
        $singular = static::inflector()->singularize($value);
68
 
69
        return static::matchCase($singular, $value);
70
    }
71
 
72
    /**
73
     * Determine if the given value is uncountable.
74
     *
75
     * @param  string  $value
76
     * @return bool
77
     */
78
    protected static function uncountable($value)
79
    {
80
        return in_array(strtolower($value), static::$uncountable);
81
    }
82
 
83
    /**
84
     * Attempt to match the case on two strings.
85
     *
86
     * @param  string  $value
87
     * @param  string  $comparison
88
     * @return string
89
     */
90
    protected static function matchCase($value, $comparison)
91
    {
92
        $functions = ['mb_strtolower', 'mb_strtoupper', 'ucfirst', 'ucwords'];
93
 
94
        foreach ($functions as $function) {
95
            if ($function($comparison) === $comparison) {
96
                return $function($value);
97
            }
98
        }
99
 
100
        return $value;
101
    }
102
 
103
    /**
104
     * Get the inflector instance.
105
     *
106
     * @return \Doctrine\Inflector\Inflector
107
     */
108
    public static function inflector()
109
    {
110
        if (is_null(static::$inflector)) {
111
            static::$inflector = InflectorFactory::createForLanguage(static::$language)->build();
112
        }
113
 
114
        return static::$inflector;
115
    }
116
 
117
    /**
118
     * Specify the language that should be used by the inflector.
119
     *
120
     * @param  string  $language
121
     * @return void
122
     */
123
    public static function useLanguage(string $language)
124
    {
125
        static::$language = $language;
126
 
127
        static::$inflector = null;
128
    }
129
}