Skip to content

Instantly share code, notes, and snippets.

@mannion007
Last active November 12, 2019 17:20
Show Gist options
  • Save mannion007/edf733d5fe2a9310613df515648606a6 to your computer and use it in GitHub Desktop.
Save mannion007/edf733d5fe2a9310613df515648606a6 to your computer and use it in GitHub Desktop.
Validating against a schema at runtime
<?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