diff options
| author | Murilo Ijanc <[email protected]> | 2026-07-26 16:41:48 -0300 |
|---|---|---|
| committer | Murilo Ijanc <[email protected]> | 2026-07-26 16:47:14 -0300 |
| commit | 535dbda6b48171f8115b455e54790de94d97d42a (patch) | |
| tree | 5c0410011e61a883bdf063ee612875a637fb1d22 /lib/cepis/Health.php | |
| download | woocommerce-0.1.0.tar.gz | |
Initial importv0.1.0
Diffstat (limited to 'lib/cepis/Health.php')
| -rw-r--r-- | lib/cepis/Health.php | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/lib/cepis/Health.php b/lib/cepis/Health.php new file mode 100644 index 0000000..08f7547 --- /dev/null +++ b/lib/cepis/Health.php @@ -0,0 +1,76 @@ +<?php + +declare(strict_types=1); + +namespace Cepis; + +/** + * The response of `GET /health`. + * + * Good for two things: checking that the API answers, and knowing **which + * version of the database** is live. The DNE database changes every two weeks; + * {@see Health::$dne} identifies it. + */ +final class Health +{ + /** + * Property names are the API's own JSON keys, `base_gerada_em` included. + * See {@see Address} for why they are not translated or camel-cased. + */ + public function __construct( + public readonly string $status, + public readonly string $dne, + public readonly int $ceps, + public readonly int $geo, + public readonly string $base_gerada_em, + ) { + } + + /** + * Build from the decoded response. + * + * @param array<string, mixed> $data + */ + public static function fromArray(array $data): self + { + return new self( + status: self::asString($data['status'] ?? ''), + dne: self::asString($data['dne'] ?? ''), + ceps: self::asInt($data['ceps'] ?? 0), + geo: self::asInt($data['geo'] ?? 0), + base_gerada_em: self::asString($data['base_gerada_em'] ?? ''), + ); + } + + /** + * `true` when the API reports itself healthy. + */ + public function isOk(): bool + { + return $this->status === 'ok'; + } + + /** + * @return array<string, mixed> + */ + public function toArray(): array + { + return [ + 'status' => $this->status, + 'dne' => $this->dne, + 'ceps' => $this->ceps, + 'geo' => $this->geo, + 'base_gerada_em' => $this->base_gerada_em, + ]; + } + + private static function asString(mixed $value): string + { + return is_scalar($value) ? (string) $value : ''; + } + + private static function asInt(mixed $value): int + { + return is_numeric($value) ? (int) (float) $value : 0; + } +} |