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

Commit

Permalink
✨ Add __invoke() method
Browse files Browse the repository at this point in the history
  • Loading branch information
siguici committed Sep 27, 2024
1 parent 85f56ed commit 4bd8ad4
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Concerns/AsBool.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ public static function of(mixed $value): self
));
}

public function __invoke(mixed $value = null): bool
{
if ($value !== null) {
return static::of($value)->get();
}

return $this->get();
}

public function isTrue(): bool
{
return $this->get();
Expand Down
9 changes: 9 additions & 0 deletions src/Concerns/AsFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ public static function of(mixed $value): self
get_debug_type($value),
));
}

public function __invoke(mixed $value = null): float
{
if ($value !== null) {
return static::of($value)->get();
}

return $this->get();
}
}
9 changes: 9 additions & 0 deletions src/Concerns/AsInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ public static function of(mixed $value): self
get_debug_type($value),
));
}

public function __invoke(mixed $value = null): int
{
if ($value !== null) {
return static::of($value)->get();
}

return $this->get();
}
}
9 changes: 9 additions & 0 deletions src/Concerns/AsMixed.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,13 @@ public function to(string $type): MixedType
{
return $type::of($this->get());
}

public function __invoke(mixed $value = null): mixed
{
if ($value !== null) {
static::of($value)->get();
}

return $this->get();
}
}
9 changes: 9 additions & 0 deletions src/Concerns/AsNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ public static function of(mixed $value): self
get_debug_type($value),
));
}

public function __invoke(mixed $value = null): int|float
{
if ($value !== null) {
return static::of($value)->get();
}

return $this->get();
}
}
9 changes: 9 additions & 0 deletions src/Concerns/AsNumeric.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ public static function of(mixed $value): self
get_debug_type($value),
));
}

public function __invoke(mixed $value = null): int|float|string
{
if ($value !== null) {
return static::of($value)->get();
}

return $this->get();
}
}
9 changes: 9 additions & 0 deletions src/Concerns/AsScalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,13 @@ public static function of(mixed $value): self
get_debug_type($value),
));
}

public function __invoke(mixed $value = null): bool|int|float|string
{
if ($value !== null) {
return static::of($value)->get();
}

return $this->get();
}
}
9 changes: 9 additions & 0 deletions src/Concerns/AsString.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@ public static function of(mixed $value): self
get_debug_type($value),
));
}

public function __invoke(mixed $value = null): string
{
if ($value !== null) {
return static::of($value)->get();
}

return $this->get();
}
}
2 changes: 2 additions & 0 deletions src/Types/BoolType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ public static function isFalsy(bool $value): bool;
public static function truthify(mixed $value): self;

public static function falsify(mixed $value): self;

public function __invoke(mixed $value = null): bool;
}
2 changes: 2 additions & 0 deletions src/Types/FloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
interface FloatType extends NumberType
{
public function get(): float;

public function __invoke(mixed $value = null): float;
}
2 changes: 2 additions & 0 deletions src/Types/IntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
interface IntType extends NumberType
{
public function get(): int;

public function __invoke(mixed $value = null): int;
}
2 changes: 2 additions & 0 deletions src/Types/MixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ interface MixedType
public function get(): mixed;

public static function of(mixed $value): self;

public function __invoke(mixed $value = null): mixed;
}
2 changes: 2 additions & 0 deletions src/Types/NumberType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
interface NumberType extends NumericType
{
public function get(): int|float;

public function __invoke(mixed $value = null): int|float;
}
2 changes: 2 additions & 0 deletions src/Types/NumericType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
interface NumericType extends ScalarType
{
public function get(): int|float|string;

public function __invoke(mixed $value = null): int|float|string;
}
2 changes: 2 additions & 0 deletions src/Types/ScalarType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
interface ScalarType extends \Stringable, MixedType
{
public function get(): bool|int|float|string;

public function __invoke(mixed $value = null): bool|int|float|string;
}
2 changes: 2 additions & 0 deletions src/Types/StringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
interface StringType extends ScalarType
{
public function get(): string;

public function __invoke(mixed $value = null): string;
}

0 comments on commit 4bd8ad4

Please sign in to comment.