Skip to content

feat: Implement Stringable interface in value object classes #14

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 1 commit into from
May 8, 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
5 changes: 3 additions & 2 deletions src/Boolean/Base/BooleanValueBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
namespace WizDevelop\PhpValueObject\Boolean\Base;

use Override;
use Stringable;
use WizDevelop\PhpMonad\Result;
use WizDevelop\PhpValueObject\Error\ValueObjectError;
use WizDevelop\PhpValueObject\IValueObject;

/**
* 真偽値の値オブジェクトの基底クラス
*/
abstract readonly class BooleanValueBase implements IValueObject, IBooleanValueFactory
abstract readonly class BooleanValueBase implements IValueObject, Stringable, IBooleanValueFactory
{
protected function __construct(public bool $value)
{
Expand All @@ -22,7 +23,7 @@ protected function __construct(public bool $value)
#[Override]
final public function equals(IValueObject $other): bool
{
return (string)$this === (string)$other;
return $this->value === $other->value;
}

#[Override]
Expand Down
12 changes: 8 additions & 4 deletions src/Collection/Base/CollectionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace WizDevelop\PhpValueObject\Collection\Base;

use Override;
use Stringable;
use WizDevelop\PhpMonad\Result;
use WizDevelop\PhpValueObject\Error\ValueObjectError;
use WizDevelop\PhpValueObject\IValueObject;
Expand All @@ -16,7 +17,7 @@
* @template TKey
* @template TValue
*/
abstract readonly class CollectionBase implements IValueObject
abstract readonly class CollectionBase implements IValueObject, Stringable
{
final protected const int MIN_COUNT = 0;
final protected const int MAX_COUNT = 99999999;
Expand All @@ -38,13 +39,16 @@ final public function equals(IValueObject $other): bool
#[Override]
final public function __toString(): string
{
return $this->jsonSerialize();
return json_encode($this->jsonSerialize(), JSON_THROW_ON_ERROR);
}

/**
* @return array<TKey,TValue>
*/
#[Override]
final public function jsonSerialize(): string
final public function jsonSerialize(): array
{
return json_encode($this->elements, JSON_THROW_ON_ERROR);
return $this->elements;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Collection/Pair.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace WizDevelop\PhpValueObject\Collection;

use Stringable;
use WizDevelop\PhpValueObject\IValueObject;
use WizDevelop\PhpValueObject\ValueObjectDefault;

Expand All @@ -13,7 +14,7 @@
* @template TKey
* @template TValue
*/
readonly class Pair implements IValueObject
readonly class Pair implements IValueObject, Stringable
{
use ValueObjectDefault;

Expand Down
3 changes: 2 additions & 1 deletion src/DateTime/LocalDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DateTimeInterface;
use DateTimeZone;
use Override;
use Stringable;
use WizDevelop\PhpMonad\Option;
use WizDevelop\PhpMonad\Result;
use WizDevelop\PhpValueObject\Error\ValueObjectError;
Expand All @@ -18,7 +19,7 @@
* ローカル日付を表す値オブジェクト
*/
#[ValueObjectMeta(displayName: 'ローカル日付')]
readonly class LocalDate implements IValueObject
readonly class LocalDate implements IValueObject, Stringable
{
/**
* The minimum supported year for instances of `LocalDate`.
Expand Down
3 changes: 2 additions & 1 deletion src/DateTime/LocalDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DateTimeInterface;
use DateTimeZone;
use Override;
use Stringable;
use WizDevelop\PhpMonad\Option;
use WizDevelop\PhpMonad\Result;
use WizDevelop\PhpValueObject\Error\ValueObjectError;
Expand All @@ -18,7 +19,7 @@
* ローカル日時を表す値オブジェクト
*/
#[ValueObjectMeta(displayName: 'ローカル日時')]
readonly class LocalDateTime implements IValueObject
readonly class LocalDateTime implements IValueObject, Stringable
{
/**
* Avoid new() operator.
Expand Down
3 changes: 2 additions & 1 deletion src/DateTime/LocalTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DateTimeInterface;
use DateTimeZone;
use Override;
use Stringable;
use WizDevelop\PhpMonad\Option;
use WizDevelop\PhpMonad\Result;
use WizDevelop\PhpValueObject\Error\ValueObjectError;
Expand All @@ -18,7 +19,7 @@
* ローカル時刻を表す値オブジェクト
*/
#[ValueObjectMeta(displayName: 'ローカル時刻')]
readonly class LocalTime implements IValueObject
readonly class LocalTime implements IValueObject, Stringable
{
final public const int MONTHS_PER_YEAR = 12;
final public const int DAYS_PER_WEEK = 7;
Expand Down
3 changes: 2 additions & 1 deletion src/Error/IErrorValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

namespace WizDevelop\PhpValueObject\Error;

use Stringable;
use WizDevelop\PhpValueObject\IValueObject;

/**
* エラー値オブジェクト インターフェース
*/
interface IErrorValue extends IValueObject
interface IErrorValue extends IValueObject, Stringable
{
/**
* シリアライズする際の区切り文字列
Expand Down
3 changes: 1 addition & 2 deletions src/IValueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
namespace WizDevelop\PhpValueObject;

use JsonSerializable;
use Stringable;

/**
* すべての値オブジェクトの基底インターフェース
* @see WizDevelop\PhpValueObject\ValueObjectDefault
*/
interface IValueObject extends Stringable, JsonSerializable
interface IValueObject extends JsonSerializable
{
/**
* 値オブジェクトの等価性を比較する
Expand Down
7 changes: 4 additions & 3 deletions src/Number/Decimal/DecimalValueBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

use BcMath\Number;
use Override;
use Stringable;
use WizDevelop\PhpMonad\Result;
use WizDevelop\PhpValueObject\Error\ValueObjectError;
use WizDevelop\PhpValueObject\IValueObject;

/**
* 少数の値オブジェクトの基底クラス
*/
abstract readonly class DecimalValueBase implements IValueObject, IArithmetic, IComparison, IDecimalValueFactory
abstract readonly class DecimalValueBase implements IValueObject, Stringable, IArithmetic, IComparison, IDecimalValueFactory
{
use Arithmetic;
use Comparison;
Expand All @@ -35,7 +36,7 @@ protected function __construct(public Number $value)
#[Override]
final public function equals(IValueObject $other): bool
{
return (string)$this === (string)$other;
return $this->value->compare($other->value) === 0;
}

#[Override]
Expand All @@ -47,7 +48,7 @@ final public function __toString(): string
#[Override]
final public function jsonSerialize(): string
{
return (string)$this;
return (string)$this->value;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Number/Integer/IntegerValueBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
namespace WizDevelop\PhpValueObject\Number\Integer;

use Override;
use Stringable;
use WizDevelop\PhpMonad\Result;
use WizDevelop\PhpValueObject\Error\ValueObjectError;
use WizDevelop\PhpValueObject\IValueObject;

/**
* 整数の値オブジェクトの基底クラス
*/
abstract readonly class IntegerValueBase implements IValueObject, IArithmetic, IComparison, IIntegerValueFactory
abstract readonly class IntegerValueBase implements IValueObject, Stringable, IArithmetic, IComparison, IIntegerValueFactory
{
use Arithmetic;
use Comparison;
Expand All @@ -31,7 +32,7 @@ protected function __construct(public int $value)
#[Override]
final public function equals(IValueObject $other): bool
{
return (string)$this === (string)$other;
return $this->value === $other->value;
}

#[Override]
Expand Down
7 changes: 4 additions & 3 deletions src/String/Base/StringValueBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace WizDevelop\PhpValueObject\String\Base;

use Override;
use Stringable;
use WizDevelop\PhpMonad\Result;
use WizDevelop\PhpValueObject\Error\ValueObjectError;
use WizDevelop\PhpValueObject\IValueObject;
Expand All @@ -14,7 +15,7 @@
/**
* 文字列の値オブジェクトの基底クラス
*/
abstract readonly class StringValueBase implements IValueObject, IStringValueFactory
abstract readonly class StringValueBase implements IValueObject, Stringable, IStringValueFactory
{
final protected const int MIN_LENGTH = 1;
final protected const int MAX_LENGTH = 4194303;
Expand All @@ -32,7 +33,7 @@ protected function __construct(public string $value)
#[Override]
final public function equals(IValueObject $other): bool
{
return (string)$this === (string)$other;
return $this->value === $other->value;
}

#[Override]
Expand All @@ -44,7 +45,7 @@ final public function __toString(): string
#[Override]
final public function jsonSerialize(): string
{
return (string)$this;
return $this->value;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/ValueObjectDefault.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
use Override;

/**
* Default implementation of IValueObject
* @see IValueObject
* Default implementation of IValueObject and Stringable.
* @see WizDevelop\PhpValueObject\IValueObject
* @see Stringable
*/
trait ValueObjectDefault
{
Expand Down
Loading