Skip to content

Implement BooleanValue and related classes with comprehensive tests #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
"phpunit/php-code-coverage": "^11.0",
"phpunit/phpunit": "^11.3",
"wiz-develop/php-monad": "^2.2",
"phpstan/phpstan": "^2.1"
"phpstan/phpstan": "^2.1",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan-strict-rules": "^2.0"
},
"config": {
"allow-plugins": {
"phpstan/extension-installer": true
}
}
}
100 changes: 98 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions examples/Boolean/TestBooleanValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace WizDevelop\PhpValueObject\Examples\Boolean;

use WizDevelop\PhpValueObject\Boolean\BooleanValue;
use WizDevelop\PhpValueObject\ValueObjectMeta;

/**
* BooleanValue抽象クラスのテスト用実装
* 単にBooleanValueを継承するだけのシンプルなクラス
*/
#[ValueObjectMeta(displayName: '真偽値')]
final readonly class TestBooleanValue extends BooleanValue
{
// 追加実装なし - 基本クラスから継承
}
2 changes: 1 addition & 1 deletion examples/Number/Decimal/TestDecimalValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace WizDevelop\PhpValueObject\Examples\Number\Decimal;

use BCMath\Number;
use BcMath\Number;
use Override;
use WizDevelop\PhpValueObject\Number\DecimalValue;
use WizDevelop\PhpValueObject\ValueObjectMeta;
Expand Down
2 changes: 1 addition & 1 deletion examples/Number/Decimal/TestNegativeDecimalValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace WizDevelop\PhpValueObject\Examples\Number\Decimal;

use BCMath\Number;
use BcMath\Number;
use Override;
use WizDevelop\PhpValueObject\Number\NegativeDecimalValue;
use WizDevelop\PhpValueObject\ValueObjectMeta;
Expand Down
2 changes: 1 addition & 1 deletion examples/Number/Decimal/TestPositiveDecimalValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace WizDevelop\PhpValueObject\Examples\Number\Decimal;

use BCMath\Number;
use BcMath\Number;
use Override;
use WizDevelop\PhpValueObject\Number\PositiveDecimalValue;
use WizDevelop\PhpValueObject\ValueObjectMeta;
Expand Down
7 changes: 6 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@ parameters:
level: max
paths:
- src
- tests
typeAliases:
NumberValueBase: '\WizDevelop\PhpValueObject\Number\Decimal\DecimalValueBase|\WizDevelop\PhpValueObject\Number\Integer\IntegerValueBase'
NumberValueBase: '\WizDevelop\PhpValueObject\Number\Decimal\DecimalValueBase|\WizDevelop\PhpValueObject\Number\Integer\IntegerValueBase'
ignoreErrors:
-
# NOTE: PHP-CS-Fixer のルールと競合するため
identifier: staticMethod.dynamicCall
97 changes: 97 additions & 0 deletions src/Boolean/Base/BooleanValueBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace WizDevelop\PhpValueObject\Boolean\Base;

use Override;
use WizDevelop\PhpMonad\Result;
use WizDevelop\PhpValueObject\Boolean\BooleanValueError;
use WizDevelop\PhpValueObject\IValueObject;

/**
* 真偽値の値オブジェクトの基底クラス
*/
abstract readonly class BooleanValueBase implements IValueObject, IBooleanValueFactory
{
protected function __construct(public bool $value)
{
assert(static::isValid($value)->isOk());
}

#[Override]
final public function equals(IValueObject $other): bool
{
return (string)$this === (string)$other;
}

#[Override]
final public function __toString(): string
{
return $this->value ? 'true' : 'false';
}

#[Override]
final public function jsonSerialize(): bool
{
return $this->value;
}

/**
* 有効な値かどうか
* NOTE: 実装クラスでのオーバーライド用メソッド
* @return Result<bool,BooleanValueError>
*/
protected static function isValid(bool $value): Result
{
return Result\ok(true);
}

/**
* 真の値かどうか
*/
final public function yes(): bool
{
return $this->value === true;
}

/**
* 偽の値かどうか
*/
final public function no(): bool
{
return $this->value === false;
}

/**
* 否定値を取得
*/
final public function not(): static
{
return static::from(!$this->value);
}

/**
* 論理積
*/
final public function and(self $other): static
{
return static::from($this->value && $other->value);
}

/**
* 論理和
*/
final public function or(self $other): static
{
return static::from($this->value || $other->value);
}

/**
* 排他的論理和
*/
final public function xor(self $other): static
{
return static::from($this->value xor $other->value);
}
}
57 changes: 57 additions & 0 deletions src/Boolean/Base/BooleanValueFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace WizDevelop\PhpValueObject\Boolean\Base;

use Override;
use WizDevelop\PhpMonad\Option;
use WizDevelop\PhpMonad\Result;

/**
* Default implementation of IBooleanValueFactory
* @see WizDevelop\PhpValueObject\Boolean\BooleanValue
* @see WizDevelop\PhpValueObject\Boolean\Base\IBooleanValueFactory
*/
trait BooleanValueFactory
{
#[Override]
final public static function from(bool $value): static
{
return new static($value);
}

#[Override]
final public static function fromNullable(?bool $value): Option
{
if ($value === null) {
return Option\none();
}

return Option\some(static::from($value));
}

#[Override]
final public static function tryFromNullable(?bool $value): Result
{
if ($value === null) {
// @phpstan-ignore-next-line
return Result\ok(Option\none());
}

// @phpstan-ignore-next-line
return static::tryFrom($value)->map(static fn ($result) => Option\some($result));
}

#[Override]
final public static function true(): static
{
return static::from(true);
}

#[Override]
final public static function false(): static
{
return static::from(false);
}
}
Loading
Loading