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 / WP_User.php

WP_User.php in FakerPress 0.8.0, at src/FakerPress/Provider/WP_User.php

212 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace FakerPress\Provider;
3
4 use FakerPress\ThirdParty\Faker\Provider\Base;
5 use FakerPress\ThirdParty\Cake\Chronos\Chronos;
6 use FakerPress\Utils;
7 use function FakerPress\make;
8
9 class WP_User extends Base {
10
11 /**
12 * Returns a first name, if nothing was passed, it will generate a random one.
13 *
14 * @since 0.1.0
15 * @since 0.6.2 Introduced type safety.
16 *
17 * @param string|null $first_name
18 * @param string[] $gender
19 *
20 * @return string|null
21 */
22 public function first_name( ?string $first_name = null, array $gender = [ 'male', 'female' ] ): ?string {
23 return $first_name ?? $this->generator->firstName( $this->generator->randomElements( $gender, 1 ) );
24 }
25
26 /**
27 * Returns a last name, if nothing was passed, it will generate a random one.
28 *
29 * @since 0.1.0
30 * @since 0.6.2 Introduced type safety.
31 *
32 * @param string|null $last_name
33 *
34 * @return string|null
35 */
36 public function last_name( ?string $last_name = null ): ?string {
37 return $last_name ?? $this->generator->lastName();
38 }
39
40 /**
41 * Returns a random username, if nothing was passed, it will generate a random one.
42 *
43 * @since 0.1.0
44 * @since 0.6.2 Introduced type safety.
45 *
46 * @param string|null $login
47 *
48 * @return string|null
49 */
50 public function user_login( ?string $login = null ): ?string {
51 return $login ?? $this->generator->userName();
52 }
53
54 /**
55 * Returns a random nicename, if nothing was passed, it will generate a random one.
56 *
57 * @since 0.1.0
58 * @since 0.6.2 Introduced type safety.
59 *
60 * @param string|null $nicename
61 *
62 * @return string|null
63 */
64 public function user_nicename( ?string $nicename = null ): ?string {
65 return $nicename ?? $this->generator->userName();
66 }
67
68 /**
69 * Returns a random URL, if nothing was passed, it will generate a random one.
70 *
71 * @since 0.1.0
72 * @since 0.6.2 Introduced type safety.
73 *
74 * @param string|null $url
75 *
76 * @return string|null
77 */
78 public function user_url( ?string $url = null ): ?string {
79 return $url ?? $this->generator->url();
80 }
81
82 /**
83 * Returns a random email, if nothing was passed, it will generate a random one.
84 *
85 * @since 0.1.0
86 * @since 0.6.2 Introduced type safety.
87 *
88 * @param string|null $email
89 *
90 * @return string|null
91 */
92 public function user_email( ?string $email = null ): ?string {
93 return $email ?? $this->generator->safeEmail();
94 }
95
96 /**
97 * Returns a random display name, if nothing was passed, it will generate a random one.
98 *
99 * @since 0.1.0
100 * @since 0.6.2 Introduced type safety.
101 *
102 * @param string|null $display_name
103 * @param string[] $gender
104 *
105 * @return string|null
106 */
107 public function display_name( ?string $display_name = null, array $gender = [ 'male', 'female' ] ): ?string {
108 return $display_name ?? $this->generator->firstName( $this->generator->randomElements( $gender, 1 ) );
109 }
110
111 /**
112 * Returns a random nickname, if nothing was passed, it will generate a random one.
113 *
114 * @since 0.1.0
115 * @since 0.6.2 Introduced type safety.
116 *
117 * @param string|null $nickname
118 *
119 * @return string|null
120 */
121 public function nickname( ?string $nickname = null ): ?string {
122 return $nickname ?? $this->generator->userName();
123 }
124
125 /**
126 * Returns a random password, if nothing was passed, it will generate a random one.
127 *
128 * @since 0.1.0
129 * @since 0.6.2 Introduced type safety.
130 *
131 * @param string|null $pass
132 * @param int $qty
133 *
134 * @return string|null
135 */
136 public function user_pass( ?string $pass = null, int $qty = 16 ): ?string {
137 if ( is_null( $pass ) ) {
138 if ( function_exists( 'wp_generate_password' ) ) {
139 $pass = wp_generate_password( $qty, true );
140 } else {
141 $pass = $this->generator->password( $qty );
142 }
143 }
144 return $pass;
145 }
146
147 /**
148 * Returns a random description.
149 *
150 * @since 0.1.0
151 * @since 0.6.2 Introduced type safety.
152 *
153 * @param bool $html Whether to return HTML or plain text.
154 * @param array $args
155 *
156 * @return string|null
157 */
158 public function description( $html = true, $args = [] ): ?string {
159 $defaults = [
160 'qty' => [ 5, 15 ],
161 ];
162 $args = wp_parse_args( $args, $defaults );
163
164 if ( true === $html ) {
165 $content = implode( "\n", $this->generator->html_elements( $args ) );
166 } else {
167 $content = implode( "\r\n\r\n", $this->generator->paragraphs( make( Utils::class )->get_qty_from_range( $args['qty'] ) ) );
168 }
169
170 return $content;
171 }
172
173 /**
174 * Returns a random role, if nothing was passed, it will pick a random one from the available roles.
175 *
176 * @since 0.1.0
177 * @since 0.6.2 Introduced type safety.
178 *
179 * @param string[] $role
180 *
181 * @return string
182 */
183 public function role( ?array $role = [] ): ?string {
184 return $this->generator->randomElement( $role ?? array_keys( get_editable_roles() ) );
185 }
186
187 public function user_registered( $min = 'now', $max = null ) {
188 try {
189 $min = new Chronos( $min );
190 } catch ( \Exception $e ) {
191 return null;
192 }
193
194 if ( ! is_null( $max ) ) {
195 // Unfortunatelly there is not such solution to this problem, we need to try and catch with DateTime
196 try {
197 $max = new Chronos( $max );
198 } catch ( \Exception $e ) {
199 return null;
200 }
201 }
202
203 if ( ! is_null( $max ) ) {
204 $selected = $this->generator->dateTimeBetween( (string) $min, (string) $max )->format( 'Y-m-d H:i:s' );
205 } else {
206 $selected = (string) $min;
207 }
208
209 return $selected;
210 }
211 }
212