Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
✨ Add mutable mixed value
Browse files Browse the repository at this point in the history
  • Loading branch information
siguici committed Nov 13, 2023
1 parent d5f4993 commit b971bce
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 40 deletions.
10 changes: 0 additions & 10 deletions src/Contracts/IsMixedValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
18 changes: 18 additions & 0 deletions src/Contracts/IsMutableMixedValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Sikessem\Values\Contracts;

use Closure;

interface IsMutableMixedValue extends IsMixedValue
{
public const DEFAULT_VALUE = null;

public function set(mixed $value): static;

public function update(callable|Closure $callback): static;

public static function from(mixed $value): self;
}
29 changes: 1 addition & 28 deletions src/MixedValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,14 @@

namespace Sikessem\Values;

use Closure;
use Sikessem\Values\Contracts\IsMixedValue;

class MixedValue implements IsMixedValue
{
protected mixed $value = self::DEFAULT_VALUE;

public function __construct(mixed $value = self::DEFAULT_VALUE)
{
$this->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);
}
}
39 changes: 39 additions & 0 deletions src/MutableMixedValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Sikessem\Values;

use Closure;
use Sikessem\Values\Contracts\IsMutableMixedValue;

class MutableMixedValue extends MixedValue implements IsMutableMixedValue
{
protected mixed $value = self::DEFAULT_VALUE;

public function __construct(mixed $value = self::DEFAULT_VALUE)
{
$this->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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
});

0 comments on commit b971bce

Please sign in to comment.