Skip to content

Commit 1fa48dc

Browse files
authored
Merge pull request #13 from alleyinteractive/fix/inner-validators
Reduce use of inner validators
2 parents 94db2d4 + cc4570d commit 1fa48dc

File tree

4 files changed

+21
-85
lines changed

4 files changed

+21
-85
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ This library adheres to [Semantic Versioning](https://semver.org/) and [Keep a C
66

77
Nothing yet.
88

9+
## 3.1.0
10+
11+
### Changed
12+
13+
* Reduce uses of validators within validators.
14+
915
## 3.0.0
1016

1117
### Changed

src/alley/wp/validator/class-block-name.php

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Laminas\Validator\InArray;
1717
use Laminas\Validator\ValidatorInterface;
1818
use Traversable;
19+
use WP_Block;
1920
use WP_Block_Parser_Block;
2021

2122
/**
@@ -74,20 +75,6 @@ final class Block_Name extends Block_Validator {
7475
*/
7576
protected ?string $current_block_name = '';
7677

77-
/**
78-
* Validates the input block name based on how many names are valid.
79-
*
80-
* @var ValidatorInterface
81-
*/
82-
private ValidatorInterface $current_validator;
83-
84-
/**
85-
* Message template to use based on how many names are valid.
86-
*
87-
* @var string
88-
*/
89-
private string $current_message;
90-
9178
/**
9279
* Set up.
9380
*
@@ -107,14 +94,6 @@ public function __construct( $options = null ) {
10794
'%block_name%',
10895
);
10996

110-
$this->current_validator = new Comparison(
111-
[
112-
'operator' => '===',
113-
'compared' => $this->options['name'],
114-
]
115-
);
116-
$this->current_message = self::NOT_NAMED;
117-
11897
parent::__construct( $options );
11998
}
12099

@@ -124,8 +103,16 @@ public function __construct( $options = null ) {
124103
* @param WP_Block_Parser_Block $block The block to test.
125104
*/
126105
protected function test_block( WP_Block_Parser_Block $block ): void {
127-
if ( ! $this->current_validator->isValid( $block->blockName ) ) {
128-
$this->error( $this->current_message );
106+
$allowed = $this->options['name'];
107+
$message = self::NAME_NOT_IN;
108+
109+
if ( ! \is_array( $allowed ) ) {
110+
$allowed = [ $allowed ];
111+
$message = self::NOT_NAMED;
112+
}
113+
114+
if ( ! \in_array( $block->blockName, $allowed, true ) ) {
115+
$this->error( $message );
129116
}
130117
}
131118

@@ -147,13 +134,5 @@ protected function setValue( $value ) {
147134
*/
148135
protected function setName( $name ) {
149136
$this->options['name'] = $name;
150-
151-
$this->current_validator = new InArray(
152-
[
153-
'haystack' => \is_array( $this->options['name'] ) ? $this->options['name'] : [ $this->options['name'] ],
154-
'strict' => InArray::COMPARE_STRICT,
155-
]
156-
);
157-
$this->current_message = \is_array( $this->options['name'] ) && \count( $this->options['name'] ) > 1 ? self::NAME_NOT_IN : self::NOT_NAMED;
158137
}
159138
}

src/alley/wp/validator/class-block-offset.php

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,6 @@ final class Block_Offset extends Block_Validator {
6969
*/
7070
private array $final_blocks = [];
7171

72-
/**
73-
* Validates whether blocks are empty.
74-
*
75-
* @var ValidatorInterface
76-
*/
77-
private ValidatorInterface $nonempty_validator;
78-
79-
/**
80-
* Validates whether blocks are iterable.
81-
*
82-
* @var ValidatorInterface
83-
*/
84-
private ValidatorInterface $iterable_validator;
85-
8672
/**
8773
* Set up.
8874
*
@@ -95,13 +81,6 @@ public function __construct( $options = null ) {
9581
'%offset%'
9682
);
9783

98-
$this->nonempty_validator = new Nonempty_Block();
99-
$this->iterable_validator = new Type(
100-
[
101-
'type' => 'iterable',
102-
],
103-
);
104-
10584
parent::__construct( $options );
10685
}
10786

@@ -117,7 +96,7 @@ public function setOptions( $options = [] ) {
11796
$this->final_blocks = $this->options['blocks'];
11897

11998
if ( $this->options['skip_empty_blocks'] ) {
120-
$this->final_blocks = array_filter( $this->final_blocks, [ $this->nonempty_validator, 'isValid' ] );
99+
$this->final_blocks = array_filter( $this->final_blocks, [ new Nonempty_Block(), 'isValid' ] );
121100
}
122101

123102
return $this;
@@ -157,10 +136,8 @@ protected function test_block( WP_Block_Parser_Block $block ): void {
157136
* @param array[]|WP_Block[]|WP_Block_Parser_Block[] $blocks Blocks.
158137
*/
159138
protected function setBlocks( $blocks ) {
160-
$valid = $this->iterable_validator->isValid( $blocks );
161-
162-
if ( ! $valid ) {
163-
throw new InvalidArgumentException( $this->iterable_validator->getMessages()[0] );
139+
if ( ! is_iterable( $blocks ) ) {
140+
throw new InvalidArgumentException( 'Blocks must be iterable.' );
164141
}
165142

166143
if ( ! $blocks instanceof \Traversable ) {

src/alley/wp/validator/class-nonempty-block.php

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ final class Nonempty_Block extends Block_Validator {
3838
self::EMPTY_BLOCK => '',
3939
];
4040

41-
/**
42-
* Validates the input block.
43-
*
44-
* @var ValidatorInterface
45-
*/
46-
private ValidatorInterface $nonempty_tests;
47-
4841
/**
4942
* Set up.
5043
*
@@ -53,25 +46,6 @@ final class Nonempty_Block extends Block_Validator {
5346
public function __construct( $options = null ) {
5447
$this->messageTemplates[ self::EMPTY_BLOCK ] = __( 'Block is empty.', 'alley' );
5548

56-
$this->nonempty_tests = new AnyValidator(
57-
[
58-
new Not(
59-
new Block_Name(
60-
[
61-
'name' => null,
62-
],
63-
),
64-
__( 'Block name is null.', 'alley' ),
65-
),
66-
new Block_InnerHTML(
67-
[
68-
'operator' => 'REGEX',
69-
'content' => '#\S#',
70-
],
71-
),
72-
],
73-
);
74-
7549
parent::__construct( $options );
7650
}
7751

@@ -81,7 +55,7 @@ public function __construct( $options = null ) {
8155
* @param WP_Block_Parser_Block $block The block to test.
8256
*/
8357
protected function test_block( WP_Block_Parser_Block $block ): void {
84-
if ( ! $this->nonempty_tests->isValid( $block ) ) {
58+
if ( null === $block->blockName && preg_match( '#\S#', $block->innerHTML ) === 0 ) {
8559
$this->error( self::EMPTY_BLOCK );
8660
}
8761
}

0 commit comments

Comments
 (0)