diff --git a/src/Contracts/IsMixedValue.php b/src/Contracts/IsMixedValue.php index bfc0617..cd4697b 100644 --- a/src/Contracts/IsMixedValue.php +++ b/src/Contracts/IsMixedValue.php @@ -4,17 +4,7 @@ namespace Sikessem\Values\Contracts; -use Closure; - interface IsMixedValue { - public const DEFAULT_VALUE = null; - public function get(): mixed; - - public function set(mixed $value): static; - - public function update(callable|Closure $callback): static; - - public static function from(mixed $value): self; } diff --git a/src/Contracts/IsMutableMixedValue.php b/src/Contracts/IsMutableMixedValue.php new file mode 100644 index 0000000..bebb826 --- /dev/null +++ b/src/Contracts/IsMutableMixedValue.php @@ -0,0 +1,18 @@ +set($value); - } + protected mixed $value; public function get(): mixed { return $this->value; } - - public function set(mixed $value): static - { - $this->value = $value; - - return $this; - } - - public function update(callable|Closure $callback): static - { - return $this->set($callback($this->get())); - } - - public static function from(mixed $value): self - { - if ($value instanceof self) { - return $value; - } - - return new self($value); - } } diff --git a/src/MutableMixedValue.php b/src/MutableMixedValue.php new file mode 100644 index 0000000..a8fa423 --- /dev/null +++ b/src/MutableMixedValue.php @@ -0,0 +1,39 @@ +set($value); + } + + public function set(mixed $value): static + { + $this->value = $value; + + return $this; + } + + public function update(callable|Closure $callback): static + { + return $this->set($callback($this->get())); + } + + public static function from(mixed $value): self + { + if ($value instanceof self) { + return $value; + } + + return new self($value); + } +} diff --git a/tests/Feat/MixedValueTest.php b/tests/Feat/MutableMixedValueTest.php similarity index 65% rename from tests/Feat/MixedValueTest.php rename to tests/Feat/MutableMixedValueTest.php index 1e76f87..bb61c4b 100644 --- a/tests/Feat/MixedValueTest.php +++ b/tests/Feat/MutableMixedValueTest.php @@ -2,10 +2,10 @@ declare(strict_types=1); -use Sikessem\Values\MixedValue; +use Sikessem\Values\MutableMixedValue; it('should work', function () { - $val = MixedValue::from('Hello World'); + $val = MutableMixedValue::from('Hello World'); $val->update(fn (string $data) => "{$data}!"); expect($val->get())->toBe('Hello World!'); });