diff --git a/src/Bag/Bag.php b/src/Bag/Bag.php index d92180e..50defaa 100644 --- a/src/Bag/Bag.php +++ b/src/Bag/Bag.php @@ -71,4 +71,12 @@ public static function rules(): array { return []; } + + /** + * @param array $array + */ + public static function __set_state(array $array): static + { + return static::from($array); + } } diff --git a/tests/Feature/BagTest.php b/tests/Feature/BagTest.php index 3325a6f..2c008ee 100644 --- a/tests/Feature/BagTest.php +++ b/tests/Feature/BagTest.php @@ -216,3 +216,27 @@ ->and($value->age)->toBe('40') ->and($value->email)->toBe(false); }); + +test('it can be var_exported', function () { + $value = TestBag::from('Davey Shafik', 40, 'davey@php.net'); + + $exported = var_export($value, true); + + expect($exported)->toBe('\Tests\Fixtures\Values\TestBag::__set_state(array(' . PHP_EOL . ' \'name\' => \'Davey Shafik\',' . PHP_EOL . ' \'age\' => 40,' . PHP_EOL . ' \'email\' => \'davey@php.net\',' . PHP_EOL . '))'); + + $imported = eval('return ' . $exported . ';'); + + expect($imported)->toBeInstanceOf(TestBag::class) + ->and($imported->toArray())->toBe($value->toArray()); +}); + +test('it can be serialized and unserialized', function () { + $value = TestBag::from('Davey Shafik', 40, 'davey@php.net'); + + $serialized = serialize($value); + $unserialized = unserialize($serialized); + + /** @var TestBag $unserialized */ + expect($unserialized)->toBeInstanceOf(TestBag::class) + ->and($unserialized->toArray())->toBe($value->toArray()); +});