-
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.
allow to convert a type to a floating point
- Loading branch information
Showing
23 changed files
with
167 additions
and
51 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
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,22 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Fixtures\Formal\ORM; | ||
|
||
/** | ||
* @psalm-immutable | ||
*/ | ||
final class CreatedAt | ||
{ | ||
private float $value; | ||
|
||
public function __construct(float $value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
public function toFloat(): float | ||
{ | ||
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,65 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Fixtures\Formal\ORM; | ||
|
||
use Fixtures\Formal\ORM\CreatedAt; | ||
use Formal\ORM\Adapter\Elasticsearch\ElasticsearchType; | ||
use Formal\ORM\Adapter\SQL\SQLType; | ||
use Formal\ORM\Definition\{ | ||
Type, | ||
Types, | ||
}; | ||
use Formal\AccessLayer\Table\Column\Type as Definition; | ||
use Innmind\Type\{ | ||
Type as Concrete, | ||
ClassName, | ||
}; | ||
use Innmind\Immutable\Maybe; | ||
|
||
/** | ||
* @psalm-immutable | ||
* @implements Type<CreatedAt> | ||
*/ | ||
final class CreatedAtType implements Type, SQLType, ElasticsearchType | ||
{ | ||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* @psalm-pure | ||
* | ||
* @return Maybe<self> | ||
*/ | ||
public static function of(Types $types, Concrete $type): Maybe | ||
{ | ||
return Maybe::just($type) | ||
->filter(static fn($type) => $type->accepts(ClassName::of(CreatedAt::class))) | ||
->map(static fn() => new self); | ||
} | ||
|
||
public function elasticsearchType(): array | ||
{ | ||
return ['type' => 'double']; | ||
} | ||
|
||
public function sqlType(): Definition | ||
{ | ||
return Definition::decimal(65, 2); | ||
} | ||
|
||
public function normalize(mixed $value): null|string|int|float|bool | ||
{ | ||
return $value->toFloat(); | ||
} | ||
|
||
public function denormalize(null|string|int|float|bool $value): mixed | ||
{ | ||
if (!\is_float($value)) { | ||
throw new \LogicException("'$value' is not a float"); | ||
} | ||
|
||
return new CreatedAt($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
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
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
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
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
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
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
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
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
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
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
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
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
Oops, something went wrong.