Skip to content

Instantly share code, notes, and snippets.

@twbell
Last active December 25, 2015 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twbell/7030129 to your computer and use it in GitHub Desktop.
Save twbell/7030129 to your computer and use it in GitHub Desktop.
Gets a Factual entity by ID, and resolves by attributes of redirect or deprecation fails.
$entity = array(
'factual_id' => "930edc75-4547-4e4f-acfd-f9ebfc8ac122", //will redirect
'name' => "Denver County Traffic Div",
'address' => "3280 Downing St",
'region' => "CO",
'locality' => "Denver"
);
$tableName = "places-us";
$res = getOrResolve($tableName, $entity);
$data = $res->getData();
$refresh = $data[0];
print_r($refresh);
/**
* Gets entity data. If redirect fails, resolves by attribute
* Use this method for cache refreshes and similar updates
* @param string tableName Table Name
* @param array Factual entity (with factual attribute names as keys)
* @return FactualResult
*/
function getOrResolve($tableName, $entity){
global $factual;
try {
$res = $factual->fetchRow($tableName, $entity['factual_id']);
} catch (Exception $e) {
//try to resolve if entity is missing
if ($e->getCode() == 404){ //special handling for entity not found logic
//add logging if required here
//resolve entity on its attributes
$resolveQuery = new ResolveQuery();
$resolveQuery->addEntity($entity);
$res = $factual->fetch("places", $resolveQuery);
$res->getResolved();
} else { //throw exception in event of non-404 error
throw $e;
}
}
return $res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment