Skip to content

Commit a39a04a

Browse files
Allow for guarding or unguarding multiple attributes at once.
1 parent 77a53f5 commit a39a04a

File tree

2 files changed

+131
-20
lines changed

2 files changed

+131
-20
lines changed

src/Model.php

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -268,29 +268,42 @@ public function fill( array $data = array() ) {
268268
*
269269
* @since 2.1.0
270270
*
271-
* @param string $attribute
272-
* @param callable $callback
271+
* @param string|array $attribute,...
272+
* @param callable $callback
273273
*/
274274
public function with_guarded( $attribute, $callback ) {
275+
276+
if ( is_array( $attribute ) ) {
277+
$attributes = $attribute;
278+
} else {
279+
$attributes = func_get_args();
280+
$callback = array_pop( $attributes );
281+
}
282+
275283
$unguarded = static::$_unguarded;
276284

277285
if ( $unguarded ) {
278286
static::$_unguarded = false;
279287
}
280288

281-
if ( ( $i = array_search( $attribute, $this->_fillable, true ) ) !== false ) {
282-
unset( $this->_fillable[ $i ] );
289+
$_fillable = $this->_fillable;
290+
$_guarded = $this->_guarded;
291+
292+
if ( $this->_fillable ) {
293+
$this->_fillable = array_diff( $this->_fillable, $attributes );
294+
295+
// If the diff removes all fillable attributes we need to use _guarded.
296+
if ( ! $this->_fillable ) {
297+
$this->_guarded = $attributes;
298+
}
283299
} else {
284-
$this->_guarded[] = $attribute;
300+
$this->_guarded = array_unique( array_merge( $this->_guarded, $attributes ) );
285301
}
286302

287303
$callback( $this );
288304

289-
if ( $i !== false ) {
290-
$this->_fillable[ $i ] = $attribute;
291-
} else {
292-
unset( $this->_guarded[ array_search( $attribute, $this->_guarded, true ) ] );
293-
}
305+
$this->_fillable = $_fillable;
306+
$this->_guarded = $_guarded;
294307

295308
static::$_unguarded = $unguarded;
296309
}
@@ -300,7 +313,7 @@ public function with_guarded( $attribute, $callback ) {
300313
*
301314
* @since 2.1.0
302315
*
303-
* @param string $attribute
316+
* @param string $attribute,...
304317
* @param callable $callback
305318
*/
306319
public function with_unguarded( $attribute, $callback ) {
@@ -311,21 +324,27 @@ public function with_unguarded( $attribute, $callback ) {
311324
return;
312325
}
313326

314-
$i = false;
327+
if ( is_array( $attribute ) ) {
328+
$attributes = $attribute;
329+
} else {
330+
$attributes = func_get_args();
331+
$callback = array_pop( $attributes );
332+
}
333+
334+
$_fillable = $this->_fillable;
335+
$_guarded = $this->_guarded;
315336

316337
if ( $this->_fillable ) {
317-
$this->_fillable[] = $attribute;
318-
} elseif ( ( $i = array_search( $attribute, $this->_guarded, true ) ) !== false ) {
319-
unset( $this->_guarded[ $i ] );
338+
$this->_fillable = array_merge( $this->_fillable, $attributes );
339+
} else {
340+
$this->_guarded = array_diff( $this->_guarded, $attributes );
320341
}
321342

322343
$callback( $this );
323344

324-
if ( $this->_fillable ) {
325-
unset( $this->_fillable[ array_search( $attribute, $this->_fillable, true ) ] );
326-
} elseif ( $i !== false ) {
327-
$this->_guarded[ $i ] = $attribute;
328-
}
345+
$this->_fillable = $_fillable;
346+
$this->_guarded = $_guarded;
347+
329348
}
330349

331350
/**

tests/unit/test-model-php7.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,51 @@ public static function get_table() {
201201
$this->assertEquals( 'o', $model->get_attribute( 'colC' ) );
202202
}
203203

204+
public function test_with_guarded_multiple() {
205+
206+
$table = $this->get_table();
207+
208+
/** @var Model $model */
209+
$model = new class( $table, [] ) extends Model {
210+
211+
public static $table;
212+
213+
public function __construct( $table = null, $data = array() ) {
214+
215+
if ( $table ) {
216+
static::$table = $table;
217+
}
218+
219+
parent::__construct( [] );
220+
}
221+
222+
public function get_pk() {
223+
return $this->get_attribute( 'colA' );
224+
}
225+
226+
public static function get_table() {
227+
return static::$table;
228+
}
229+
230+
};
231+
232+
$model::$table = $table;
233+
234+
$model->with_guarded( 'colB', 'colC', function ( Model $model ) {
235+
$model->fill( array( 'colA' => 'He', 'colB' => 'll', 'colC' => 'o' ) );
236+
} );
237+
238+
$this->assertEquals( 'He', $model->get_attribute( 'colA' ) );
239+
$this->assertEquals( null, $model->get_attribute( 'colB' ) );
240+
$this->assertEquals( null, $model->get_attribute( 'colC' ) );
241+
242+
$model->fill( array( 'colA' => 'He', 'colB' => 'll', 'colC' => 'o' ) );
243+
244+
$this->assertEquals( 'He', $model->get_attribute( 'colA' ) );
245+
$this->assertEquals( 'll', $model->get_attribute( 'colB' ) );
246+
$this->assertEquals( 'o', $model->get_attribute( 'colC' ) );
247+
}
248+
204249
public function test_with_guarded_and_existing_guarded_properties() {
205250

206251
$table = $this->get_table();
@@ -387,6 +432,53 @@ public static function get_table() {
387432
$this->assertEquals( 'o', $model->get_attribute( 'colC' ) );
388433
}
389434

435+
public function test_with_unguarded_multiple() {
436+
437+
$table = $this->get_table();
438+
439+
/** @var Model $model */
440+
$model = new class( $table, [] ) extends Model {
441+
442+
public static $table;
443+
444+
protected $_guarded = array( 'colA', 'colB' );
445+
446+
public function __construct( $table = null, $data = array() ) {
447+
448+
if ( $table ) {
449+
static::$table = $table;
450+
}
451+
452+
parent::__construct( [] );
453+
}
454+
455+
public function get_pk() {
456+
return $this->get_attribute( 'colA' );
457+
}
458+
459+
public static function get_table() {
460+
return static::$table;
461+
}
462+
463+
};
464+
465+
$model::$table = $table;
466+
467+
$model->with_unguarded( 'colA', 'colB', function ( Model $model ) {
468+
$model->fill( array( 'colA' => 'He', 'colB' => 'll', 'colC' => 'o' ) );
469+
} );
470+
471+
$this->assertEquals( 'He', $model->get_attribute( 'colA' ) );
472+
$this->assertEquals( 'll', $model->get_attribute( 'colB' ) );
473+
$this->assertEquals( 'o', $model->get_attribute( 'colC' ) );
474+
475+
$model->fill( array( 'colA' => 'Me', 'colB' => 'bo', 'colC' => 'o' ) );
476+
477+
$this->assertEquals( 'He', $model->get_attribute( 'colA' ) );
478+
$this->assertEquals( 'll', $model->get_attribute( 'colB' ) );
479+
$this->assertEquals( 'o', $model->get_attribute( 'colC' ) );
480+
}
481+
390482
public function test_with_unguarded_and_existing_guarded_properties() {
391483

392484
$table = $this->get_table();

0 commit comments

Comments
 (0)