-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFaker_Provider.php
More file actions
192 lines (171 loc) · 5.81 KB
/
Copy pathFaker_Provider.php
File metadata and controls
192 lines (171 loc) · 5.81 KB
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
<?php
/**
* Faker_Provider class file.
*
* @package Mantle
*/
namespace Mantle\Faker;
use Closure;
use Faker\Provider\Base;
use Faker\Provider\Lorem;
use InvalidArgumentException;
use function Mantle\Support\Helpers\collect;
use function Mantle\Support\Helpers\value;
/**
* Faker Block Provider
*/
class Faker_Provider extends Base {
/**
* Compile a set of blocks.
*
* @param string[] $blocks Blocks to compile.
*/
public static function blocks( array $blocks ): string {
return implode( "\n\n", $blocks );
}
/**
* Build a heading block.
*
* @param string|int|null $text Heading text.
* @param int $level Heading level.
*/
public static function heading_block( string|null|int $text = null, int $level = 2 ): string {
if ( is_int( $text ) ) {
$level = $text;
$text = null;
}
return static::block(
'heading',
sprintf( '<h%d>%s</h%d>', $level, $text ?: Lorem::sentence(), $level ),
[
'level' => $level,
],
);
}
/**
* Build a paragraph block.
*
* @throws InvalidArgumentException Thrown on invalid sentences parameter.
*
* @param string|int|null $text Text for the block.
* @param int|(\Closure(): int)|array<int> $sentences Number of sentences in the block.
* Supports a integer length, array of min/max, or a closure that will return an array.
*/
public static function paragraph_block( string|null|int $text = null, int|array|Closure $sentences = 3 ): string {
if ( is_int( $text ) ) {
$sentences = $text;
$text = null;
}
if ( is_array( $sentences ) ) {
if (
2 !== count( $sentences )
|| ! isset( $sentences[0], $sentences[1] )
|| ! is_int( $sentences[0] ) // @phpstan-ignore-line booleanNot.alwaysFalse
|| ! is_int( $sentences[1] ) // @phpstan-ignore-line booleanNot.alwaysFalse
) {
throw new InvalidArgumentException(
'When passing an array to the sentences parameter, it must be an array of two integers.',
);
}
$sentences = wp_rand( $sentences[0], $sentences[1] );
}
return static::block(
'paragraph',
sprintf( '<p>%s</p>', $text ?: Lorem::sentences( (int) value( $sentences ), true ) ) // @phpstan-ignore-line argument.type
);
}
/**
* Generate a set of paragraph blocks.
*
* @param int $count Number of paragraph blocks to generate.
* @param bool $as_text Return as text or an array of blocks.
* @param int|(\Closure(): int)|array<int> $sentences Number of sentences in each block.
* Supports a integer length, array of min/max, or a closure that will return an array.
* @return string|string[]
* @phpstan-return ($as_text is true ? string : array<string>)
*/
public static function paragraph_blocks( int $count = 3, bool $as_text = true, int|array|Closure $sentences = 3 ): string|array {
$paragraphs = [];
for ( $i = 0; $i < $count; $i++ ) {
$paragraphs[] = static::paragraph_block( sentences: $sentences );
}
return $as_text ? implode( "\n\n", $paragraphs ) : $paragraphs;
}
/**
* Build an image block.
*
* @param string|null $url Image URL.
* @param string|null $alt Image alt text.
* @param array<mixed> $attributes Additional attributes for the block.
*/
public static function image_block( ?string $url = null, ?string $alt = null, array $attributes = [] ): string {
$image = sprintf(
'<figure class="wp-block-image"><img src="%s"%s/></figure>',
$url ?? 'https://picsum.photos/' . wp_rand( 100, 1000 ) . '/' . wp_rand( 100, 1000 ),
$alt ? ' alt="' . esc_attr( $alt ) . '"' : '',
);
return static::block( 'image', $image, $attributes );
}
/**
* Generate a list block.
*
* @param string[] $items List items.
* @param bool $ordered Whether the list is ordered or unordered.
* @param array<string, mixed> $attributes Block attributes.
*/
public static function list_block( array $items = [], bool $ordered = false, array $attributes = [] ): string {
$list_type = $ordered ? 'ol' : 'ul';
$list_items = collect( $items )->map(
static fn ( string $item ) => static::block(
name: 'list-item',
content: "<li>{$item}</li>",
),
)->implode( "\n" );
if ( $ordered ) {
$attributes['ordered'] = true;
}
return static::block(
name: 'list',
content: sprintf( "<%s class=\"wp-block-list\">\n%s\n</%s>", $list_type, $list_items, $list_type ),
attributes: $attributes,
);
}
/**
* Build a reusable block.
*
* @param int $id Block ID.
*/
public static function reusable_block( int $id ): string {
return static::block( 'block', null, [ 'ref' => $id ] );
}
/**
* Build a button block (or rather a buttons block with a button inside).
*
* @param string $text Button text.
* @param string $url Button URL.
* @param array<mixed> $attributes Additional attributes for the block.
*/
public static function button_block( string $text, string $url, array $attributes = [] ): string {
return static::block(
name: 'buttons',
content: '<div class="wp-block-buttons">' . static::block(
name: 'button',
content: '<div class="wp-block-button"><a class="wp-block-button__link wp-element-button" href="' . esc_url( $url ) . '">' . esc_html( $text ) . '</a></div>',
) . '</div>',
);
}
/**
* Build a block for Gutenberg.
*
* @param string $name Block name.
* @param string|null $content Content for the block.
* @param array<mixed> $attributes Attributes for the block.
*/
public static function block( string $name, ?string $content = null, array $attributes = [] ): string {
// Add a newline before and after the content.
if ( ! is_null( $content ) ) {
$content = "\n{$content}\n";
}
return get_comment_delimited_block_content( $name, $attributes, $content ?? '' );
}
}