Skip to content

Commit 64b0a4c

Browse files
committed
Add __toString implementation and tests to date-time, timestamp, and boolean classes, refine namespace and format-related methods.
1 parent 5d7640b commit 64b0a4c

27 files changed

+327
-61
lines changed

src/Abstract/Bool/BoolType.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,4 @@
2525
*/
2626
abstract readonly class BoolType extends AbstractType implements BoolTypeInterface
2727
{
28-
abstract protected function __construct(bool $value);
29-
30-
public function toString(): string
31-
{
32-
return $this->value() ? 'true' : 'false';
33-
}
34-
35-
public static function fromBool(bool $value): static
36-
{
37-
return new static($value);
38-
}
39-
40-
public function __toString(): string
41-
{
42-
return $this->toString();
43-
}
4428
}

src/Abstract/Bool/BoolTypeInterface.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpTypedValues\Abstract\Bool;
66

7+
use PhpTypedValues\Exception\BoolTypeException;
78
use PhpTypedValues\Undefined\Alias\Undefined;
89

910
/**
@@ -21,11 +22,16 @@
2122
*/
2223
interface BoolTypeInterface
2324
{
24-
public function value(): bool;
25-
2625
public static function tryFromString(string $value): static|Undefined;
2726

2827
public static function tryFromInt(int $value): static|Undefined;
2928

29+
/**
30+
* @throws BoolTypeException
31+
*/
32+
public static function fromInt(int $value): static;
33+
34+
public function value(): bool;
35+
3036
public static function fromBool(bool $value): static;
3137
}

src/Abstract/DateTime/DateTimeType.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
protected const MIN_TIMESTAMP_SECONDS = -62135596800; // 0001-01-01
4040
protected const MAX_TIMESTAMP_SECONDS = 253402300799; // 9999-12-31 23:59:59
4141

42-
protected DateTimeImmutable $value;
43-
4442
/**
4543
* @throws DateTimeTypeException
4644
* @throws ReasonableRangeDateTimeTypeException
@@ -103,32 +101,4 @@ protected static function createFromFormat(
103101
*/
104102
return $dt;
105103
}
106-
107-
public function __construct(DateTimeImmutable $value)
108-
{
109-
$this->value = $value;
110-
}
111-
112-
public static function fromDateTime(DateTimeImmutable $value): static
113-
{
114-
// normalized timezone
115-
return new static(
116-
$value->setTimezone(new DateTimeZone(static::ZONE))
117-
);
118-
}
119-
120-
public function value(): DateTimeImmutable
121-
{
122-
return $this->value;
123-
}
124-
125-
public static function getFormat(): string
126-
{
127-
return static::FORMAT;
128-
}
129-
130-
public function __toString(): string
131-
{
132-
return $this->toString();
133-
}
134104
}

src/Abstract/Integer/IntTypeInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ public function value(): int;
2525
public static function fromInt(int $value): static;
2626

2727
public static function tryFromInt(int $value): static|Undefined;
28+
29+
public static function tryFromString(string $value): static|Undefined;
2830
}

src/Abstract/Undefined/UndefinedTypeInterface.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace PhpTypedValues\Abstract\Undefined;
66

7+
use PhpTypedValues\Exception\UndefinedTypeException;
8+
79
/**
810
* Contract for the special Undefined typed value.
911
*
@@ -21,9 +23,18 @@ interface UndefinedTypeInterface
2123
{
2224
public static function create(): static;
2325

24-
public function value(): never;
25-
26+
/**
27+
* @throws UndefinedTypeException
28+
*/
2629
public function toInt(): never;
2730

31+
/**
32+
* @throws UndefinedTypeException
33+
*/
2834
public function toFloat(): never;
35+
36+
/**
37+
* @throws UndefinedTypeException
38+
*/
39+
public function value(): never;
2940
}

src/Bool/BoolStandard.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,19 @@ public function jsonSerialize(): bool
9595
{
9696
return $this->value();
9797
}
98+
99+
public function toString(): string
100+
{
101+
return $this->value() ? 'true' : 'false';
102+
}
103+
104+
public static function fromBool(bool $value): static
105+
{
106+
return new static($value);
107+
}
108+
109+
public function __toString(): string
110+
{
111+
return $this->toString();
112+
}
98113
}

src/Bool/FalseStandard.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,22 @@ public function jsonSerialize(): bool
9696
{
9797
return $this->value();
9898
}
99+
100+
public function toString(): string
101+
{
102+
return $this->value() ? 'true' : 'false';
103+
}
104+
105+
/**
106+
* @throws BoolTypeException
107+
*/
108+
public static function fromBool(bool $value): static
109+
{
110+
return new static($value);
111+
}
112+
113+
public function __toString(): string
114+
{
115+
return $this->toString();
116+
}
99117
}

src/Bool/TrueStandard.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,22 @@ public function jsonSerialize(): bool
9696
{
9797
return $this->value();
9898
}
99+
100+
public function toString(): string
101+
{
102+
return $this->value() ? 'true' : 'false';
103+
}
104+
105+
/**
106+
* @throws BoolTypeException
107+
*/
108+
public static function fromBool(bool $value): static
109+
{
110+
return new static($value);
111+
}
112+
113+
public function __toString(): string
114+
{
115+
return $this->toString();
116+
}
99117
}

src/DateTime/DateTimeAtom.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
{
3333
protected const FORMAT = DATE_ATOM;
3434

35+
protected DateTimeImmutable $value;
36+
37+
public function __construct(DateTimeImmutable $value)
38+
{
39+
$this->value = $value;
40+
}
41+
3542
/**
3643
* @throws DateTimeTypeException
3744
*/
@@ -62,11 +69,29 @@ public function toString(): string
6269

6370
public static function fromDateTime(DateTimeImmutable $value): static
6471
{
65-
return new static($value);
72+
// normalized timezone
73+
return new static(
74+
$value->setTimezone(new DateTimeZone(static::ZONE))
75+
);
6676
}
6777

6878
public function jsonSerialize(): string
6979
{
7080
return $this->toString();
7181
}
82+
83+
public function __toString(): string
84+
{
85+
return $this->toString();
86+
}
87+
88+
public function value(): DateTimeImmutable
89+
{
90+
return $this->value;
91+
}
92+
93+
public static function getFormat(): string
94+
{
95+
return static::FORMAT;
96+
}
7297
}

src/DateTime/DateTimeRFC3339.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
{
3333
protected const FORMAT = DATE_RFC3339;
3434

35+
protected DateTimeImmutable $value;
36+
37+
public function __construct(DateTimeImmutable $value)
38+
{
39+
$this->value = $value;
40+
}
41+
3542
/**
3643
* @throws DateTimeTypeException
3744
*/
@@ -62,11 +69,29 @@ public function toString(): string
6269

6370
public static function fromDateTime(DateTimeImmutable $value): static
6471
{
65-
return new static($value);
72+
// normalized timezone
73+
return new static(
74+
$value->setTimezone(new DateTimeZone(static::ZONE))
75+
);
6676
}
6777

6878
public function jsonSerialize(): string
6979
{
7080
return $this->toString();
7181
}
82+
83+
public function __toString(): string
84+
{
85+
return $this->toString();
86+
}
87+
88+
public function value(): DateTimeImmutable
89+
{
90+
return $this->value;
91+
}
92+
93+
public static function getFormat(): string
94+
{
95+
return static::FORMAT;
96+
}
7297
}

0 commit comments

Comments
 (0)