Skip to content

Commit 6155f54

Browse files
committed
Switched back return type definition
1 parent 5e68514 commit 6155f54

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

src/Models/Model.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ protected function purgeRelationship( string $key ): void {
484484
* @since 2.0.0
485485
*
486486
* @param string $key Relationship name.
487-
* @param mixed $value The relationship value to cache.
487+
* @param Model|list<Model>|null $value The relationship value to cache.
488488
*
489489
* @return void
490490
*/

src/Models/ModelRelationship.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,22 +159,17 @@ public function purge(): void {
159159
*/
160160
public function setValue( $value ): self {
161161
if ( $value !== null ) {
162-
if ( $this->definition->isSingle() && ! $this->definition->getValidateRelationshipWith()( $value ) ) {
163-
throw new InvalidArgumentException( 'Relationship value must be a valid value.' );
162+
if ( $this->definition->isSingle() ) {
163+
$value = $this->definition->getValidateSanitizeRelationshipWith()( $value );
164164
}
165165

166166
if ( $this->definition->isMultiple() ) {
167167
if ( ! is_array( $value ) ) {
168-
throw new InvalidArgumentException( 'Multiple relationship value must be an array or null.' );
168+
Config::throwInvalidArgumentException( 'Multiple relationship value must be an array or null.' );
169169
}
170170

171-
foreach ( $value as $item ) {
172-
if ( $this->definition->getValidateRelationshipWith()( $item ) ) {
173-
continue;
174-
}
175-
176-
throw new InvalidArgumentException( 'Multiple relationship value must be an array of valid values.' );
177-
}
171+
$sanitizer = $this->definition->getValidateSanitizeRelationshipWith();
172+
$value = array_map( $sanitizer, $value );
178173
}
179174
}
180175

src/Models/ModelRelationshipDefinition.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ class ModelRelationshipDefinition {
3333
private $hydrateWith = null;
3434

3535
/**
36-
* The callable to validate the relationship with.
36+
* The callable to validate and sanitize the relationship with.
3737
*
3838
* @since 2.0.0
3939
*
4040
* @var ?callable
4141
*/
42-
private $validateRelationshipWith = null;
42+
private $validateSanitizeRelationshipWith = null;
4343

4444
/**
4545
* Whether the definition is locked. Once locked, the definition cannot be changed.
@@ -129,12 +129,12 @@ public function getHydrateWith(): callable {
129129
*
130130
* @since 2.0.0
131131
*
132-
* @param callable $validateRelationshipWith The callable to validate the relationship with.
132+
* @param callable $validateSanitizeRelationshipWith The callable to validate the relationship with.
133133
*/
134-
public function setValidateRelationshipWith( callable $validateRelationshipWith ): self {
134+
public function setValidateSanitizeRelationshipWith( callable $validateSanitizeRelationshipWith ): self {
135135
$this->checkLock();
136136

137-
$this->validateRelationshipWith = $validateRelationshipWith;
137+
$this->validateSanitizeRelationshipWith = $validateSanitizeRelationshipWith;
138138

139139
return $this;
140140
}
@@ -143,10 +143,18 @@ public function setValidateRelationshipWith( callable $validateRelationshipWith
143143
* Get the callable to validate the relationship with.
144144
*
145145
* @since 2.0.0
146+
*
147+
* @return callable( mixed $thing ): ( Model | null )
146148
*/
147149

148-
public function getValidateRelationshipWith(): callable {
149-
return $this->validateRelationshipWith ?? fn( $thing ): bool => null === $thing || $thing instanceof Model;
150+
public function getValidateSanitizeRelationshipWith(): callable {
151+
return $this->validateSanitizeRelationshipWith ?? function( $thing ): ?Model {
152+
if ( null !== $thing && ! $thing instanceof Model ) {
153+
throw new InvalidArgumentException( 'Relationship value must be a valid value.' );
154+
}
155+
156+
return $thing;
157+
};
150158
}
151159

152160
/**

tests/wpunit/ModelRelationshipTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,15 @@ protected static function properties(): array {
234234
protected static function relationships(): array {
235235
$definition = new ModelRelationshipDefinition('post', Relationship::HAS_ONE());
236236
$definition->setHydrateWith( fn( int $post_id ) => get_post( $post_id ) );
237-
$definition->setValidateRelationshipWith( fn( $post_or_post_id ) => get_post( $post_or_post_id ) instanceof WP_Post );
237+
$definition->setValidateSanitizeRelationshipWith( function ( $post_or_post_id ) {
238+
$p = get_post( $post_or_post_id );
239+
if ( ! $p instanceof WP_Post ) {
240+
throw new \InvalidArgumentException( 'Post must be a valid WP_Post object.' );
241+
}
242+
243+
return $p->ID;
244+
});
245+
238246
return [
239247
'post' => $definition,
240248
];
@@ -254,5 +262,12 @@ protected static function relationships(): array {
254262

255263
$this->assertInstanceOf(WP_Post::class, $myModel->post);
256264
$this->assertEquals($post_id, $myModel->post->ID);
265+
266+
$myModel->setCachedRelationship('post', null);
267+
$this->assertNull($myModel->post);
268+
269+
$myModel->setCachedRelationship('post', get_post($post_id));
270+
$this->assertInstanceOf(WP_Post::class, $myModel->post);
271+
$this->assertEquals($post_id, $myModel->post->ID);
257272
}
258273
}

0 commit comments

Comments
 (0)