PluginProbe
FakerPress / 0.8.0
FakerPress v0.8.0
0.9.2 trunk 0.1.0 0.1.1 0.1.2 0.1.3 0.5.2 0.5.3 0.6.1 0.6.2 0.6.3 0.6.4 0.6.5 0.6.6 0.7.0 0.7.1 0.7.2 0.8.0 0.9.0 0.9.1
fakerpress / src / FakerPress / Provider / Text / Base.php

Base.php in FakerPress 0.8.0, at src/FakerPress/Provider/Text/Base.php

207 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace FakerPress\Provider\Text;
3
4 use FakerPress\ThirdParty\Faker\Provider\Base as Faker_Base;
5
6 class Base extends Faker_Base {
7 public static $wordList = [
8 'alias', 'consequatur', 'aut', 'perferendis', 'sit', 'voluptatem',
9 'accusantium', 'doloremque', 'aperiam', 'eaque','ipsa', 'quae', 'ab',
10 'illo', 'inventore', 'veritatis', 'et', 'quasi', 'architecto',
11 'beatae', 'vitae', 'dicta', 'sunt', 'explicabo', 'aspernatur', 'aut',
12 'odit', 'aut', 'fugit', 'sed', 'quia', 'consequuntur', 'magni',
13 'dolores', 'eos', 'qui', 'ratione', 'voluptatem', 'sequi', 'nesciunt',
14 'neque', 'dolorem', 'ipsum', 'quia', 'dolor', 'sit', 'amet',
15 'consectetur', 'adipisci', 'velit', 'sed', 'quia', 'non', 'numquam',
16 'eius', 'modi', 'tempora', 'incidunt', 'ut', 'labore', 'et', 'dolore',
17 'magnam', 'aliquam', 'quaerat', 'voluptatem', 'ut', 'enim', 'ad',
18 'minima', 'veniam', 'quis', 'nostrum', 'exercitationem', 'ullam',
19 'corporis', 'nemo', 'enim', 'ipsam', 'voluptatem', 'quia', 'voluptas',
20 'sit', 'suscipit', 'laboriosam', 'nisi', 'ut', 'aliquid', 'ex', 'ea',
21 'commodi', 'consequatur', 'quis', 'autem', 'vel', 'eum', 'iure',
22 'reprehenderit', 'qui', 'in', 'ea', 'voluptate', 'velit', 'esse',
23 'quam', 'nihil', 'molestiae', 'et', 'iusto', 'odio', 'dignissimos',
24 'ducimus', 'qui', 'blanditiis', 'praesentium', 'laudantium', 'totam',
25 'rem', 'voluptatum', 'deleniti', 'atque', 'corrupti', 'quos',
26 'dolores', 'et', 'quas', 'molestias', 'excepturi', 'sint',
27 'occaecati', 'cupiditate', 'non', 'provident', 'sed', 'ut',
28 'perspiciatis', 'unde', 'omnis', 'iste', 'natus', 'error',
29 'similique', 'sunt', 'in', 'culpa', 'qui', 'officia', 'deserunt',
30 'mollitia', 'animi', 'id', 'est', 'laborum', 'et', 'dolorum', 'fuga',
31 'et', 'harum', 'quidem', 'rerum', 'facilis', 'est', 'et', 'expedita',
32 'distinctio', 'nam', 'libero', 'tempore', 'cum', 'soluta', 'nobis',
33 'est', 'eligendi', 'optio', 'cumque', 'nihil', 'impedit', 'quo',
34 'porro', 'quisquam', 'est', 'qui', 'minus', 'id', 'quod', 'maxime',
35 'placeat', 'facere', 'possimus', 'omnis', 'voluptas', 'assumenda',
36 'est', 'omnis', 'dolor', 'repellendus', 'temporibus', 'autem',
37 'quibusdam', 'et', 'aut', 'consequatur', 'vel', 'illum', 'qui',
38 'dolorem', 'eum', 'fugiat', 'quo', 'voluptas', 'nulla', 'pariatur',
39 'at', 'vero', 'eos', 'et', 'accusamus', 'officiis', 'debitis', 'aut',
40 'rerum', 'necessitatibus', 'saepe', 'eveniet', 'ut', 'et',
41 'voluptates', 'repudiandae', 'sint', 'et', 'molestiae', 'non',
42 'recusandae', 'itaque', 'earum', 'rerum', 'hic', 'tenetur', 'a',
43 'sapiente', 'delectus', 'ut', 'aut', 'reiciendis', 'voluptatibus',
44 'maiores', 'doloribus', 'asperiores', 'repellat',
45 ];
46
47 /**
48 * @example 'Lorem'
49 */
50 public static function word() {
51 return static::randomElement( static::$wordList );
52 }
53
54 /**
55 * Generate an array of random words
56 *
57 * @example [ 'Lorem', 'ipsum', 'dolor' ]
58 * @param integer $nb how many words to return
59 * @param bool $asText if true the sentences are returned as one string
60 * @return array|string
61 */
62 public static function words( $nb = 3, $asText = false ) {
63 $words = [];
64 for ( $i = 0; $i < $nb; $i++ ) {
65 $words[] = static::word();
66 }
67
68 return $asText ? join( ' ', $words ) : $words;
69 }
70
71 /**
72 * Generate a random sentence
73 *
74 * @example 'Lorem ipsum dolor sit amet.'
75 * @param integer $nbWords around how many words the sentence should contain
76 * @param boolean $variableNbWords set to false if you want exactly $nbWords returned,
77 * otherwise $nbWords may vary by +/-40% with a minimum of 1
78 * @return string
79 */
80 public static function sentence( $nbWords = 6, $variableNbWords = true ) {
81 if ( $nbWords <= 0 ) {
82 return '';
83 }
84 if ( $variableNbWords ) {
85 $nbWords = self::randomize_nb_elements( $nbWords );
86 }
87
88 $words = static::words( $nbWords );
89 $words[0] = ucwords( $words[0] );
90
91 return join( $words, ' ' ) . '.';
92 }
93
94 /**
95 * Generate an array of sentences
96 *
97 * @example [ 'Lorem ipsum dolor sit amet.', 'Consectetur adipisicing eli.' ]
98 * @param integer $nb how many sentences to return
99 * @param bool $asText if true the sentences are returned as one string
100 * @return array|string
101 */
102 public static function sentences( $nb = 3, $asText = false ) {
103 $sentences = [];
104 for ( $i = 0; $i < $nb; $i++ ) {
105 $sentences[] = static::sentence();
106 }
107
108 return $asText ? join( ' ', $sentences ) : $sentences;
109 }
110
111 /**
112 * Generate a single paragraph
113 *
114 * @example 'Sapiente sunt omnis. Ut pariatur ad autem ducimus et. Voluptas rem voluptas sint modi dolorem amet.'
115 * @param integer $nbSentences around how many sentences the paragraph should contain
116 * @param boolean $variableNbSentences set to false if you want exactly $nbSentences returned,
117 * otherwise $nbSentences may vary by +/-40% with a minimum of 1
118 * @return string
119 */
120 public static function paragraph( $nbSentences = 3, $variableNbSentences = true ) {
121 if ( $nbSentences <= 0 ) {
122 return '';
123 }
124 if ( $variableNbSentences ) {
125 $nbSentences = self::randomize_nb_elements( $nbSentences );
126 }
127
128 return join( static::sentences( $nbSentences ), ' ' );
129 }
130
131 /**
132 * Generate an array of paragraphs
133 *
134 * @example [ $paragraph1, $paragraph2, $paragraph3 ]
135 * @param integer $nb how many paragraphs to return
136 * @param bool $asText if true the paragraphs are returned as one string, separated by two newlines
137 * @return array|string
138 */
139 public static function paragraphs( $nb = 3, $asText = false ) {
140 $paragraphs = [];
141 for ( $i = 0; $i < $nb; $i++ ) {
142 $paragraphs [] = static::paragraph();
143 }
144
145 return $asText ? join( "\n\n", $paragraphs ) : $paragraphs;
146 }
147
148 /**
149 * Generate a text string.
150 * Depending on the $maxNbChars, returns a string made of words, sentences, or paragraphs.
151 *
152 * @example 'Sapiente sunt omnis. Ut pariatur ad autem ducimus et. Voluptas rem voluptas sint modi dolorem amet.'
153 * @param integer $maxNbChars Maximum number of characters the text should contain (minimum 5)
154 * @return string
155 */
156 public static function text( $maxNbChars = 200 ) {
157 $text = [];
158 if ( $maxNbChars < 5 ) {
159 throw new \InvalidArgumentException( 'text() can only generate text of at least 5 characters' );
160 } elseif ( $maxNbChars < 25 ) {
161 // join words
162 while ( empty( $text ) ) {
163 $size = 0;
164 // determine how many words are needed to reach the $maxNbChars once;
165 while ( $size < $maxNbChars ) {
166 $word = ( $size ? ' ' : '' ) . static::word();
167 $text[] = $word;
168 $size += strlen( $word );
169 }
170 array_pop( $text );
171 }
172 $text[0][0] = static::toUpper( $text[0][0] );
173 $text[ count( $text ) - 1 ] .= '.';
174 } elseif ( $maxNbChars < 100 ) {
175 // join sentences
176 while ( empty( $text ) ) {
177 $size = 0;
178 // determine how many sentences are needed to reach the $maxNbChars once;
179 while ( $size < $maxNbChars ) {
180 $sentence = ($size ? ' ' : '') . static::sentence();
181 $text[] = $sentence;
182 $size += strlen( $sentence );
183 }
184 array_pop( $text );
185 }
186 } else {
187 // join paragraphs
188 while ( empty( $text ) ) {
189 $size = 0;
190 // determine how many paragraphs are needed to reach the $maxNbChars once;
191 while ( $size < $maxNbChars ) {
192 $paragraph = ( $size ? "\n" : '' ) . static::paragraph();
193 $text[] = $paragraph;
194 $size += strlen( $paragraph );
195 }
196 array_pop( $text );
197 }
198 }
199
200 return join( $text, '' );
201 }
202
203 protected static function randomize_nb_elements( $nbElements ) {
204 return (int) ( $nbElements * mt_rand( 60, 140 ) / 100 ) + 1;
205 }
206 }
207