Faker /src/Faker/Provider/fr_FR/Company.php

Language PHP Lines 222
MD5 Hash 57e7c14e942032e225f508f1537b8014
Repository https://github.com/gobb/Faker.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php

namespace Faker\Provider\fr_FR;

class Company extends \Faker\Provider\Company
{
    /**
     * @var array French company name formats.
     */
    protected static $formats = array(
        '{{lastName}} {{companySuffix}}',
        '{{lastName}} {{lastName}} {{companySuffix}}',
        '{{lastName}}',
        '{{lastName}}',
    );

    /**
     * @var array French catch phrase formats.
     */
    protected static $catchPhraseFormats = array(
        '{{catchPhraseNoun}} {{catchPhraseVerb}} {{catchPhraseAttribute}}',
    );

    /**
     * @var array French nouns (used by the catch phrase format).
     */
    protected static $noun = array(
        'la sécurité', 'le plaisir', 'le confort', 'la simplicité', "l'assurance", "l'art", 'le pouvoir', 'le droit',
        'la possibilité', "l'avantage", 'la liberté'
    );

    /**
     * @var array French verbs (used by the catch phrase format).
     */
    protected static $verb = array(
        'de rouler', "d'avancer", "d'évoluer", 'de changer', "d'innover", 'de louer', "d'atteindre vos buts",
        'de concrétiser vos projets'
    );

    /**
     * @var array End of sentences (used by the catch phrase format).
     */
    protected static $attribute = array(
        'de manière efficace', 'plus rapidement', 'plus facilement', 'plus simplement', 'en toute tranquilité',
        'avant-tout', 'autrement', 'naturellement', 'à la pointe', 'sans soucis', "à l'état pur",
        'à sa source', 'de manière sûre', 'en toute sécurité'
    );

    /**
     * @var array Company suffixes.
     */
    protected static $companySuffix = array('SA', 'S.A.', 'SARL', 'S.A.R.L.', 'S.A.S.', 'et Fils');

    /**
     * Returns a random catch phrase noun.
     *
     * @return string
     */
    public function catchPhraseNoun()
    {
        return static::randomElement(static::$noun);
    }

    /**
     * Returns a random catch phrase attribute.
     *
     * @return string
     */
    public function catchPhraseAttribute()
    {
        return static::randomElement(static::$attribute);
    }

    /**
     * Returns a random catch phrase verb.
     *
     * @return string
     */
    public function catchPhraseVerb()
    {
        return static::randomElement(static::$verb);
    }

    /**
     * Generates a french catch phrase.
     *
     * @return string
     */
    public function catchPhrase()
    {
        do {
            $format = static::randomElement(static::$catchPhraseFormats);
            $catchPhrase = ucfirst($this->generator->parse($format));

            if ($this->isCatchPhraseValid($catchPhrase)) {
                break;
            }
        } while (true);

        return $catchPhrase;
    }

    /**
     * Generates a siret number (14 digits) that passes the Luhn check. 
     * Use $maxSequentialDigits to make sure the digits at position 2 to 5 are not zeros. 
     * @see http://en.wikipedia.org/wiki/Luhn_algorithm
     * @param int $maxSequentialDigits The maximum number of digits for the sequential number (> 0 && <= 4).
     * @return string
     */
    public static function siret($maxSequentialDigits = 2)
    {
        
        if ($maxSequentialDigits > 4 || $maxSequentialDigits <= 0) {
            $maxSequentialDigits = 2;
        }
        
        $controlDigit = mt_rand(0, 9);
        $siret = $sum = $controlDigit;
        
        $position = 2;
        for ($i = 0; $i < $maxSequentialDigits; $i++) {
            
            $sequentialDigit = mt_rand(0, 9);
            $isEven = $position++ % 2 === 0;
            
            $tmp = $isEven ? $sequentialDigit * 2 : $sequentialDigit;
            if ($tmp >= 10) $tmp -= 9;
            $sum += $tmp;
            
            $siret = $sequentialDigit . $siret;
            
        }
        
        $siret = str_pad($siret, 5, '0', STR_PAD_LEFT);
        
        $position = 6;
        for ($i = 0; $i < 7; $i++) {
            
            $digit = mt_rand(0, 9);
            $isEven = $position++ % 2 === 0;
            
            $tmp = $isEven ? $digit * 2 : $digit;
            if ($tmp >= 10) $tmp -= 9;
            $sum += $tmp;
            
            $siret = $digit . $siret;
            
        }
        
        $mod = $sum % 10;
        if ($mod === 0) {
            $siret = '00' . $siret;
        } else {
            // Use the odd position to avoid multiplying by two
            $siret = '0' . (10 - $mod) . $siret;
        }
        
        return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{5})/", "$1 $2 $3 $4", $siret);
        
    }

    /**
     * Generates a siren number (9 digits) that passes the Luhn check. 
     * @see http://en.wikipedia.org/wiki/Luhn_algorithm
     * @return string
     */
    public static function siren()
    {
        $siren = '';
        $sum = 0;
        for ($i = 9; $i > 1; $i--) {
            
            $digit = mt_rand(0, 9);
            $isEven = $i % 2 === 0;
            
            $tmp = $isEven ? $digit * 2 : $digit;
            if ($tmp >= 10) $tmp -= 9;
            $sum += $tmp;
            
            $siren = $digit . $siren;
            
        }
        
        $mod = $sum % 10;
        if ($mod === 0) {
            $siren = '0' . $siren;
        } else {
            $siren = (10 - $mod) . $siren;
        }
        
        return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{3})/", "$1 $2 $3", $siren);
        
    }

    /**
     * @var array An array containing string which should not appear twice in a catch phrase.
     */
    protected static $wordsWhichShouldNotAppearTwice = array('sécurité', 'simpl');

    /**
     * Validates a french catch phrase.
     *
     * @param string $catchPhrase The catch phrase to validate.
     *
     * @return boolean (true if valid, false otherwise)
     */
    protected static function isCatchPhraseValid($catchPhrase)
    {
        foreach (static::$wordsWhichShouldNotAppearTwice as $word) {
            // Fastest way to check if a piece of word does not appear twice.
            $beginPos = strpos($catchPhrase, $word);
            $endPos = strrpos($catchPhrase, $word);

            if ($beginPos !== false && $beginPos != $endPos) {
                return false;
            }
        }

        return true;
    }
}
Back to Top