-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock_Factory.php
More file actions
134 lines (115 loc) · 3.71 KB
/
Copy pathBlock_Factory.php
File metadata and controls
134 lines (115 loc) · 3.71 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
<?php
/**
* Block_Factory class file
*
* @package Mantle
*/
namespace Mantle\Testing;
use Faker\Generator;
use InvalidArgumentException;
use function Mantle\Support\Helpers\collect;
/**
* Block Factory
*
* Used to generate blocks and create presets of blocks for testing.
*
* @method string button(string $text, string $url, array $attributes = [])
* @method string block(string $name = 'paragraph', ?string $content = null, array $attributes = [])
* @method string heading(?string $text = null, int $level = 2)
* @method string image(?string $url = null, ?string $alt = null, array $attributes = [])
* @method string list(array $items = [], bool $ordered = false, array $attributes = [])
* @method string ordered_list(array $items = [], bool $ordered = false, array $attributes = [])
* @method string paragraph(?string $text = null, int|array<int>|\Closure $sentences = 3)
* @method string paragraphs(int $count = 3, bool $as_text = true, int|array<int>|\Closure $sentences = 3)
* @method string reusable(int $id)
*/
class Block_Factory {
/**
* Presets of blocks.
*
* @var array<string, array|string|callable>
*/
public static array $presets = [];
/**
* Register a preset of blocks.
*
* @param string $name Name of the preset.
* @param array|string|callable $preset Preset to register.
*/
public static function register_preset( string $name, array|string|callable $preset ): void {
static::$presets[ $name ] = $preset;
}
/**
* Clear all presets.
*/
public static function clear_presets(): void {
static::$presets = [];
}
/**
* Constructor.
*
* @param Generator $faker Faker generator.
*/
public function __construct( protected Generator $faker ) {}
/**
* Apply a preset.
*
* @throws InvalidArgumentException If the preset is not found.
*
* @param string $name Name of the preset.
* @param array $arguments Arguments to pass to the preset.
*/
public function preset( string $name, array $arguments = [] ): string {
if ( ! isset( static::$presets[ $name ] ) ) {
throw new InvalidArgumentException( "Unknown block factory preset: {$name}" );
}
$result = static::$presets[ $name ];
if ( is_callable( $result ) ) {
$result = $result( $this, ...$arguments );
}
return is_array( $result ) ? serialize_blocks( $result ) : $result;
}
/**
* Magic method to generate blocks from a preset.
*
* @throws InvalidArgumentException If the block method is not found.
*
* @param string $name Name of the preset.
* @param array $arguments Arguments to pass to the preset.
*/
public function __call( string $name, array $arguments ): string {
if ( isset( static::$presets[ $name ] ) ) {
return static::preset( $name, $arguments );
}
if ( 'ordered_list' === $name ) {
$name = 'list';
$arguments['ordered'] = true;
}
$method = match ( $name ) {
'paragraphs' => 'paragraph_blocks',
'block' => 'block',
default => str_ends_with( $name, '_block' ) ? $name : "{$name}_block",
};
try {
return $this->faker->$method( ...$arguments );
} catch ( InvalidArgumentException ) {
throw new InvalidArgumentException( "Unknown block factory method: {$name}" );
}
}
/**
* Generate a collection of blocks.
*
* Blocks can be passed as an array or as individual arguments.
*
* @param array|string ...$blocks Blocks to generate.
*/
public function blocks( array|string ...$blocks ): string {
$blocks = isset( $blocks[0] ) && is_array( $blocks[0] ) ? $blocks[0] : $blocks;
if ( ! is_array( $blocks ) ) { // @phpstan-ignore-line function.alreadyNarrowedType
$blocks = [ $blocks ];
}
return collect( $blocks )
->map( fn ( $block ) => is_array( $block ) ? serialize_blocks( $block ) : $block )
->implode( "\n\n" );
}
}