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

Commit

Permalink
✨ Add bool value
Browse files Browse the repository at this point in the history
  • Loading branch information
siguici committed Nov 13, 2023
1 parent 317dc5f commit 2dc3e32
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/BoolValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Sikessem\Values;

use Sikessem\Values\Concerns\AsBool;
use Sikessem\Values\Contracts\BoolType;

class BoolValue implements BoolType
{
use AsBool;

public function __construct(protected bool $value)
{
$this->value = $value;
}

public function get(): bool
{
return $this->value;
}
}
37 changes: 37 additions & 0 deletions src/Concerns/AsBool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Sikessem\Values\Concerns;

use Sikessem\Values\Contracts\ScalarType;

trait AsBool
{
use AsScalar;

abstract public function get(): bool;

/**
* @throws \InvalidArgumentException If the value is not a boolean.
*/
public static function from(mixed $value): self
{
if ($value instanceof static) {
return $value;
}

if ($value instanceof ScalarType) {
$value = (bool) $value->get();
}

if (is_bool($value)) {
return new self($value);
}

throw new \InvalidArgumentException(sprintf(
'Value "%s" is not a boolean.',
get_debug_type($value),
));
}
}
10 changes: 10 additions & 0 deletions src/Contracts/BoolType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Sikessem\Values\Contracts;

interface BoolType extends ScalarType
{
public function get(): bool;
}
27 changes: 27 additions & 0 deletions tests/Unit/BoolTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Sikessem\Values\BoolValue;
use Sikessem\Values\Contracts\BoolType;
use Sikessem\Values\Contracts\ScalarType;

beforeEach(function () {
$this->bool = BoolValue::from(true);
});

it('should be instantiable', function () {
expect($this->bool)->toBeInstanceOf(BoolValue::class);
});

it('should be an instance of BoolType', function () {
expect($this->bool)->toBeInstanceOf(BoolType::class);
});

it('should be an instance of ScalarType', function () {
expect($this->bool)->toBeInstanceOf(ScalarType::class);
});

it('should return the value', function () {
expect($this->bool->get())->toBe(true);
});

0 comments on commit 2dc3e32

Please sign in to comment.