Skip to content

Commit

Permalink
Allow the block factory to override text (#575)
Browse files Browse the repository at this point in the history
* Allow the block factory to override text

* phpcs fix

* Ensure backwards compat
  • Loading branch information
srtfisher authored Aug 26, 2024
1 parent 2a4229b commit e9a5805
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
24 changes: 18 additions & 6 deletions src/mantle/faker/class-faker-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ public static function blocks( array $blocks ): string {
/**
* Build a heading block.
*
* @param int $level Heading level.
* @param string|int|null $text Heading text.
* @param int $level Heading level.
*/
public static function heading_block( int $level = 2 ): string {
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, Lorem::sentence(), $level ),
sprintf( '<h%d>%s</h%d>', $level, $text ?: Lorem::sentence(), $level ),
[
'level' => $level,
],
Expand All @@ -41,12 +47,18 @@ public static function heading_block( int $level = 2 ): string {
/**
* Build a paragraph block.
*
* @param int $sentences Number of sentences in the block.
* @param string|int|null $text Text for the block.
* @param int $sentences Number of sentences in the block.
*/
public static function paragraph_block( int $sentences = 3 ): string {
public static function paragraph_block( string|null|int $text = null, int $sentences = 3 ): string {
if ( is_int( $text ) ) {
$sentences = $text;
$text = null;
}

return static::block(
'paragraph',
sprintf( '<p>%s</p>', Lorem::sentences( $sentences, true ) )
sprintf( '<p>%s</p>', $text ?: Lorem::sentences( $sentences, true ) )
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/mantle/testing/class-block-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
*
* @method string block(string $name = 'paragraph', string $content = '', array $attributes = [])
* @method string image(?string $url = null, ?string $alt = null, array $attributes = [])
* @method string heading(int $level = 2)
* @method string paragraph(int $sentences = 3)
* @method string heading(?string $text = null, int $level = 2)
* @method string paragraph(?string $text = null, int $sentences = 3)
* @method string paragraphs(int $count = 3, bool $as_text = true)
*/
class Block_Factory {
Expand Down

0 comments on commit e9a5805

Please sign in to comment.