Last active
August 18, 2016 10:40
-
-
Save linxlad/a39d4493feaab7797ed4bea384c9655c to your computer and use it in GitHub Desktop.
Document Typelist Meta Matcher
This file contains hidden or 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 | |
namespace OpenObjects\Bundle\CoreBundle\Util; | |
/** | |
* Class TypeListMetaUtil | |
* @package OpenObjects\Bundle\CoreBundle\Util | |
*/ | |
class TypeListMetaUtil | |
{ | |
/** | |
* @param $response | |
*/ | |
public static function matchTypeListsToDocument(&$response) | |
{ | |
$typeLists = self::createTypeListLookupArray($response); | |
foreach ($typeLists as $fieldName => $record) { | |
foreach ($response['documents'] as $documentIndex => $document) { | |
if (isset($document[$fieldName])) { | |
$documentFieldName = $document[$fieldName]; | |
if (!is_array($documentFieldName)) { | |
self::matchTypeListValue( | |
$response, | |
$typeLists, | |
$fieldName, | |
$documentFieldName, | |
$documentIndex | |
); | |
} else { | |
self::matchArrayTypeList( | |
$response, | |
$documentFieldName, | |
$typeLists, | |
$fieldName, | |
$documentIndex | |
); | |
} | |
} | |
} | |
} | |
} | |
/** | |
* @param $response | |
* @param $documentFieldName | |
* @param $typeLists | |
* @param $fieldName | |
* @param $documentIndex | |
*/ | |
private static function matchArrayTypeList( | |
&$response, | |
$documentFieldName, | |
$typeLists, | |
$fieldName, | |
$documentIndex | |
) { | |
foreach ($documentFieldName as $fieldKey => $fieldValue) { | |
if (!is_array($fieldValue)) { | |
self::matchTypeListValue( | |
$response, | |
$typeLists, | |
$fieldName, | |
$fieldValue, | |
$documentIndex, | |
$fieldKey | |
); | |
} else { | |
foreach ($fieldValue as $fieldVKey => $fieldVValue) { | |
self::matchTypeListValue( | |
$response, | |
$typeLists, | |
$fieldName, | |
$fieldValue, | |
$documentIndex, | |
$fieldKey | |
); | |
} | |
} | |
} | |
} | |
/** | |
* @param array $response | |
* | |
* @return array | |
*/ | |
protected function createTypeListLookupArray($response) | |
{ | |
$typeLists = []; | |
// Build type list array. | |
foreach ($response['recordMetadata']['indexMetadata'] as $directory => $metaRecord) { | |
foreach ($metaRecord['typeListMetadata'] as $typeListItem) { | |
self::addTypeListLookup($typeLists, $typeListItem); | |
} | |
} | |
return $typeLists; | |
} | |
/** | |
* @param $typeLists | |
* @param $typeListItem | |
*/ | |
private static function addTypeListLookup(&$typeLists, $typeListItem) | |
{ | |
foreach ($typeListItem['applysTo'] as $applyTo) { | |
$fieldNames = explode('.', $applyTo); | |
if (!$typeListItem['typeList']['isHierarchical']) { | |
if (is_array($typeListItem['typeList']['types']) && !empty($typeListItem['typeList']['types'])) { | |
foreach ($typeListItem['typeList']['types'] as $type) { | |
$typeLists[$fieldNames[0]][] = [ | |
'fieldToMatch' => $fieldNames[1] ?? null, | |
'name' => $type['name'], | |
'displayName' => $type['displayName'], | |
'isHierarchical' => false, | |
]; | |
} | |
} | |
} else { | |
$typeLists[$fieldNames[0]][] = $typeListItem['typeList']; | |
} | |
} | |
} | |
/** | |
* @param $response | |
* @param $typeLists | |
* @param $fieldName | |
* @param $fieldValue | |
* @param $documentIndex | |
* @param null $fieldKey | |
*/ | |
private static function matchTypeListValue( | |
&$response, | |
$typeLists, | |
$fieldName, | |
$fieldValue, | |
$documentIndex, | |
$fieldKey = null | |
) { | |
foreach ($typeLists[$fieldName] as $key => $value) { | |
if ($value['isHierarchical']) { | |
$newValue = self::recursiveTypeListSearch($fieldValue, $value); | |
$response['documents'][$documentIndex][$fieldName] = $newValue; | |
} else { | |
$newValue = [ | |
'name' => $value['name'], | |
'displayName' => $value['displayName'] | |
]; | |
self::matchNonHierarchicalTypes( | |
$response, | |
$fieldName, | |
$fieldValue, | |
$value, | |
$documentIndex, | |
$newValue, | |
$fieldKey | |
); | |
} | |
} | |
} | |
/** | |
* @param $response | |
* @param $fieldName | |
* @param $fieldValue | |
* @param $typeListValue | |
* @param $documentIndex | |
* @param $newValue | |
* @param null $fieldKey | |
*/ | |
private static function matchNonHierarchicalTypes( | |
&$response, | |
$fieldName, | |
$fieldValue, | |
$typeListValue, | |
$documentIndex, | |
$newValue, | |
$fieldKey = null | |
) { | |
if (!is_array($fieldValue)) { | |
if ($fieldValue == $typeListValue['name']) { | |
if (null === $fieldKey) { | |
$response['documents'][$documentIndex][$fieldName] = $newValue; | |
} else { | |
$response['documents'][$documentIndex][$fieldName][$fieldKey] = $newValue; | |
} | |
} | |
} elseif ($fieldValue[$typeListValue['fieldToMatch']] == $typeListValue['name']) { | |
$response['documents'][$documentIndex][$fieldName][$fieldKey][$typeListValue['fieldToMatch']] = $newValue; | |
} | |
} | |
/** | |
* @param $fieldValue | |
* @param $value | |
* | |
* @return array|bool | |
*/ | |
private static function recursiveTypeListSearch($fieldValue, $value) | |
{ | |
foreach ($value as $key => $innerValue) { | |
if (isset($innerValue['name']) && $fieldValue == $innerValue['name']) { | |
return self::buildRecursiveResult($innerValue); | |
} elseif (is_array($innerValue)) { | |
$result = self::recursiveTypeListSearch($fieldValue, $innerValue); | |
if ($result !== false){ | |
return self::buildRecursiveResult($result); | |
} | |
} | |
} | |
return false; | |
} | |
/** | |
* @param $value | |
* | |
* @return array | |
*/ | |
private function buildRecursiveResult($value) | |
{ | |
return [ | |
'name' => $value['name'], | |
'displayName' => $value['displayName'] | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment