-
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.
API Objects (e.g. IsoDate, IsoDateTime, IsoTime) for feature parity
- Loading branch information
1 parent
9495623
commit 081beed
Showing
32 changed files
with
1,010 additions
and
185 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,34 @@ | ||
<?php | ||
|
||
use PhpTypeScriptApi\PhpStan\IsoDate; | ||
use PhpTypeScriptApi\PhpStan\IsoTime; | ||
use PhpTypeScriptApi\PhpStan\IsoDateTime; | ||
use PhpTypeScriptApi\TypedEndpoint; | ||
|
||
/** | ||
* @extends TypedEndpoint< | ||
* array{date: IsoDate, time: \PhpTypeScriptApi\PhpStan\IsoTime}, | ||
* array{dateTime: IsoDateTime}, | ||
* > | ||
*/ | ||
class CombineDateTimeTypedEndpoint extends TypedEndpoint { | ||
public static function getApiObjectClasses(): array { | ||
return [IsoDate::class, IsoTime::class, IsoDateTime::class]; | ||
} | ||
|
||
public function runtimeSetup(): void { | ||
// no runtime setup required | ||
} | ||
|
||
public static function getIdent(): string { | ||
return 'CombineDateTimeTypedEndpoint'; | ||
} | ||
|
||
protected function handle(mixed $input): mixed { | ||
$date = $input['date']->format('Y-m-d'); | ||
$time = $input['time']->format('H:i:s'); | ||
return [ | ||
'dateTime' => new IsoDateTime("{$date} {$time}"), | ||
]; | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
example/tests/BackendTests/Endpoints/CombineDateTimeTypedEndpointTest.php
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpTypeScriptApi\BackendTests\Endpoints; | ||
|
||
use PhpTypeScriptApi\BackendTests\Common\ExampleBackendTestCase; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @coversNothing | ||
*/ | ||
final class CombineDateTimeTypedEndpointTest extends ExampleBackendTestCase { | ||
public function testCombineDateTimeTyped(): void { | ||
$result = $this->callBackend('combineDateTimeTyped', [ | ||
'date' => '2025-01-01', | ||
'time' => '13:27:35', | ||
]); | ||
$this->assertSame('', $result['error']); | ||
$this->assertSame(200, $result['http_code']); | ||
$this->assertSame(['dateTime' => '2025-01-01 13:27:35'], $result['result']); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace PhpTypeScriptApi\PhpStan; | ||
|
||
/** | ||
* @template Data | ||
*/ | ||
interface ApiObjectInterface { | ||
/** @return Data */ | ||
public function data(): mixed; | ||
|
||
/** | ||
* @return ApiObjectInterface<Data> | ||
*/ | ||
public static function fromData(mixed $data): ApiObjectInterface; | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace PhpTypeScriptApi\PhpStan; | ||
|
||
/** | ||
* @implements ApiObjectInterface<non-empty-string> | ||
*/ | ||
class IsoDate extends \DateTime implements ApiObjectInterface { | ||
public function data(): mixed { | ||
return $this->format('Y-m-d'); | ||
} | ||
|
||
public static function fromData(mixed $data): IsoDate { | ||
if (!is_string($data)) { | ||
throw new \InvalidArgumentException("IsoDate must be string"); | ||
} | ||
if (!preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $data)) { | ||
throw new \InvalidArgumentException("IsoDate must be Y-m-d"); | ||
} | ||
return new IsoDate($data); | ||
} | ||
|
||
public function __toString(): string { | ||
return "{$this->data()}"; | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace PhpTypeScriptApi\PhpStan; | ||
|
||
/** | ||
* @implements ApiObjectInterface<non-empty-string> | ||
*/ | ||
class IsoDateTime extends \DateTime implements ApiObjectInterface { | ||
public function data(): mixed { | ||
return $this->format('Y-m-d H:i:s'); | ||
} | ||
|
||
public static function fromData(mixed $data): IsoDateTime { | ||
if (!is_string($data)) { | ||
throw new \InvalidArgumentException("IsoDateTime must be string"); | ||
} | ||
if (!preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/', $data)) { | ||
throw new \InvalidArgumentException("IsoDateTime must be Y-m-d H:i:s"); | ||
} | ||
return new IsoDateTime($data); | ||
} | ||
|
||
public function __toString(): string { | ||
return "{$this->data()}"; | ||
} | ||
} |
Oops, something went wrong.