Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize block classes #14

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/alley/wp/blocks/class-block-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

namespace Alley\WP\Blocks;

use Alley\WP\Types\Block_Sequence;
use Alley\WP\Types\Serialized_Blocks;

/**
* A sequence of blocks in the given content.
* Blocks in the given content.
*/
final class Block_Content implements Block_Sequence {
final class Block_Content implements Serialized_Blocks {
/**
* Set up.
*
Expand Down
68 changes: 68 additions & 0 deletions src/alley/wp/blocks/class-blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Blocks class file
*
* @package wp-type-extensions
*/

namespace Alley\WP\Blocks;

use Alley\WP\Types\Serialized_Blocks;

/**
* Bundle many blocks.
*/
final class Blocks implements Serialized_Blocks {
/**
* Collected blocks.
*
* @var Serialized_Blocks[]
*/
private readonly array $blocks;

/**
* Set up.
*
* @param Serialized_Blocks ...$blocks Blocks.
*/
public function __construct( Serialized_Blocks ...$blocks ) {
$this->blocks = $blocks;
}

/**
* Serialized block content.
*
* @return string
*/
public function serialized_blocks(): string {
return array_reduce(
$this->blocks,
fn ( string $carry, Serialized_Blocks $block ) => $carry .= $block->serialized_blocks(),
'',
);
}

/**
* Constructor for creating blocks from a set of values.
*
* @phpstan-param iterable<mixed> $values
* @phpstan-param callable(Serialized_Blocks[] $carry, mixed $item, mixed $index, iterable<mixed> $values): Serialized_Blocks[] $reduce
*
* @param iterable $values Values.
* @param callable $reduce Reducer callback that produces block instances.
* @return Serialized_Blocks
*/
public static function from_iterable( iterable $values, callable $reduce ): Serialized_Blocks {
return new Lazy_Blocks(
function () use ( $values, $reduce ) {
$carry = [];

foreach ( $values as $index => $item ) {
$carry = ( $reduce )( $carry, $item, $index, $values );
}

return new Blocks( ...$carry );
}
);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?php
/**
* Default_Classname_Wrapped_Block class file
* Default_Classname_Block class file
*
* @package wp-type-extensions
*/

namespace Alley\WP;
namespace Alley\WP\Blocks;

use Alley\WP\Types\Single_Block;

/**
* Wrap block in block default classname.
*/
final class Default_Classname_Wrapped_Block implements Single_Block {
final class Default_Classname_Block implements Single_Block {
/**
* Set up.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
* @package wp-type-extensions
*/

namespace Alley\WP;
namespace Alley\WP\Blocks;

use Alley\WP\Types\Block_Sequence;
use Alley\WP\Types\Serialized_Blocks;

/**
* Replace each matched blocks with other block content.
* Replace each matched block with other block content.
*/
final class Each_Replaced_Blocks implements Block_Sequence {
final class Each_Replaced_Blocks implements Serialized_Blocks {
/**
* Set up.
*
Expand All @@ -35,13 +34,13 @@ public function __construct(
public function serialized_blocks(): string {
$out = $this->origin->serialized_blocks();

$parsed_blocks = parse_blocks( $this->find->serialized_blocks() );
$needles = parse_blocks( $this->find->serialized_blocks() );

if ( \is_array( $parsed_blocks ) ) {
foreach ( $parsed_blocks as $find ) {
if ( \is_array( $find ) && \count( $find ) > 0 ) {
$parsed = new Parsed_Block( $find );
$out = str_replace( $parsed->serialized_blocks(), $this->replace->serialized_blocks(), $out );
if ( \is_array( $needles ) && count( $needles ) > 0 ) {
foreach ( $needles as $needle ) {
if ( \is_array( $needle ) ) {
$pbn = new Parsed_Block( $needle );
$out = str_replace( $pbn->serialized_blocks(), $this->replace->serialized_blocks(), $out );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package wp-type-extensions
*/

namespace Alley\WP;
namespace Alley\WP\Blocks;

use Alley\WP\Types\Serialized_Blocks;
use Alley\WP\Types\Single_Block;
Expand Down Expand Up @@ -50,7 +50,10 @@ public function parsed_block(): array {
&& \is_array( $out['innerContent'] )
) {
$out['innerBlocks'] = array_merge( $add, $out['innerBlocks'] );
$out['innerContent'] = array_merge( array_fill( 0, \count( $add ), null ), $out['innerContent'] );
$out['innerContent'] = array_merge(
array_fill( 0, \count( $add ), null ),
$out['innerContent'],
);
}

return $out;
Expand All @@ -62,6 +65,7 @@ public function parsed_block(): array {
* @return string
*/
public function serialized_blocks(): string {
return serialize_block( $this->parsed_block() );
$pb = new Parsed_Block( $this->parsed_block() );
return $pb->serialized_blocks();
}
}
33 changes: 33 additions & 0 deletions src/alley/wp/blocks/class-lazy-blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Lazy_Blocks class file
*
* @package wp-type-extensions
*/

namespace Alley\WP\Blocks;

use Alley\WP\Types\Serialized_Blocks;

/**
* Instantiate blocks only when called upon.
*/
final class Lazy_Blocks implements Serialized_Blocks {
/**
* Set up.
*
* @param callable(): Serialized_Blocks $final Callback to create the blocks.
*/
public function __construct(
private $final,
) {}

/**
* Serialized block content.
*
* @return string
*/
public function serialized_blocks(): string {
return ( $this->final )()->serialized_blocks();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
* @package wp-type-extensions
*/

namespace Alley\WP;
namespace Alley\WP\Blocks;

use Alley\WP\Types\Block_Sequence;
use Alley\WP\Types\Serialized_Blocks;

use function Alley\WP\match_blocks;

/**
* Matched blocks with {@see match_blocks()}.
*/
final class Matched_Blocks implements Block_Sequence {
final class Matched_Blocks implements Serialized_Blocks {
/**
* Set up.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package wp-type-extensions
*/

namespace Alley\WP;
namespace Alley\WP\Blocks;

use Alley\WP\Types\Single_Block;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package wp-type-extensions
*/

namespace Alley\WP;
namespace Alley\WP\Blocks;

use Alley\WP\Types\Single_Block;
use WP_Block_Parser_Block;
Expand Down
57 changes: 0 additions & 57 deletions src/alley/wp/class-blocks-of.php

This file was deleted.

45 changes: 0 additions & 45 deletions src/alley/wp/class-blocks.php

This file was deleted.

13 changes: 0 additions & 13 deletions src/alley/wp/types/interface-block-sequence.php

This file was deleted.

4 changes: 3 additions & 1 deletion tests/alley/wp/BlocksOfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Alley\WP;

use Alley\WP\Blocks\Blocks;
use Alley\WP\Blocks\Named_Block;
use Mantle\Testkit\Test_Case;

/**
Expand All @@ -17,7 +19,7 @@ final class BlocksOfTest extends Test_Case {
* Test blocks of iterable.
*/
public function test_iterable() {
$actual = Blocks_Of::iterable(
$actual = Blocks::from_iterable(
[ 'foo/bar', 'foo/baz', 'foo/bat' ],
function ( array $carry, string $item ) {
$carry[] = new Named_Block( block_name: $item );
Expand Down
Loading