This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
generated from Sikessem/skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |