Skip to content

Instantly share code, notes, and snippets.

@asheliahut
Last active February 7, 2020 16:30
Show Gist options
  • Select an option

  • Save asheliahut/8bc5f09bb9b42ebf1984e2465bc3b241 to your computer and use it in GitHub Desktop.

Select an option

Save asheliahut/8bc5f09bb9b42ebf1984e2465bc3b241 to your computer and use it in GitHub Desktop.
ObjectCheckFirst
<?php
// This is an object that can come from a few sources into a reusable resolve for urls for a graphql resolver
// can also be an array from a different source.
$value = (object) ['url' => 'https://example.com'];
// As array
$arrayValue = ['url' => 'https://example.com'];
/**
* The normal definition for the function is in the doc block.
* @param $value
* @param array $args
* @param ResolveInfo $info
*
* @return string|null
*/
function resolve($value): ?string
{
//This is the only actual thing this function does
return $value['url'] ?? $value->url ?? null;
// This option will always work in both cases
// return $value->url ?? $value['url'] ?? null
}
// This one fails
var_dump(resolve($value));
// This one passes
var_dump(resolve($arrayValue));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment