Last active
November 12, 2019 17:20
-
-
Save mannion007/edf733d5fe2a9310613df515648606a6 to your computer and use it in GitHub Desktop.
Validating against a schema at runtime
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
<?php | |
/** ...Make the request, decode the response */ | |
$validator->validate($decodedResponse, json_decode('details.json')); | |
if (!empty($validator->getErrors())) { | |
throw new InvalidResponseException( | |
sprintf('Invalid Net Verify response for [%s]: [%s]', $type, implode(',', $errorMessages)) | |
); | |
} | |
/** ...safely use any of properties of the valdiated response without checks */ | |
/** @var string */ | |
$scanReference = $decodedResponse->scanReference; | |
/** @var string */ | |
$status = $decodedResponse->status; | |
/** @var \DateTimeImmutable|null */ | |
$dob = null; | |
if (isset($decodedResponse->document->dob)) { | |
$dob = \DateTimeImmutable::createFromFormat('Y-m-d', $decodedResponse->document->dob); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment