Created
July 5, 2018 00:32
-
-
Save Pictor13/a0370ebf49f677d39fb791b946fcf894 to your computer and use it in GitHub Desktop.
To understand flatten() method. This is an example of hierarchy of errors resulted from validation of command_state generated by the command builder (state would be passed to created Command, if there wouldn't be errors.
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
/** | |
* Contains conventions for errors array format. | |
* Probably used just for errors array. | |
* ^.|.(not "values" or "embedded_entity_commands").(decimal|.).. | |
* eg key for attribute path: .book.2. | |
$errors = [ | |
[ // error | |
] | |
'books' => [ | |
'@incidents' => [ | |
[ | |
'path' => author.books.1.title', | |
'incidents' => [ 'mandatory_missing', [ 'title', 'title_en' ] ] | |
] | |
], | |
'books.1.@type' => [] | |
] | |
] | |
[][$attribute_name]['@incidents'][] | |
====================== | |
flatten(command->build()Error) = [ | |
'property_name' => [ | |
[ | |
[ | |
// validateEmbeddedEntityListAttribute()Result | |
[ | |
'embedded_list_attribute_name' = [ | |
'@incidents' = [ | |
[ | |
'path' => 'embedded_list_attribute_path', | |
'incidents' => [ 'min_count' => [ 'incident_parameters' ] ] | |
] | |
] | |
] | |
] | |
], | |
[ | |
// getEmbeddedCommands()Result | |
[ | |
'embed_attribute_name.position.@type' => [ | |
'@incidents' => [ | |
[ | |
'path' => 'attribute_path', | |
'incidents' => [ | |
[ 'incident_name' => 'incident_params'] | |
] | |
] | |
] | |
] | |
] | |
], | |
[ | |
// sanitizeAttributeValue()Result | |
[ | |
'attribute_name' => [ | |
'@incidents' => [ | |
[ | |
'path' => 'attribute_path', | |
'incidents' => [ | |
[ 'incident_name' => 'incident_params'] | |
] | |
] | |
] | |
] | |
] | |
], | |
// attributes validation | |
[ | |
'attribute_name' => [ | |
'@incidents' => [ | |
[ | |
'path' => 'attribute_path', | |
'incidents' => [ | |
[ 'incident_name' => 'incident_params'] | |
] | |
] | |
] | |
] | |
], | |
[ | |
'title' => [ | |
'@incidents' => [ | |
[ | |
'path' => 'author.books.book.title', | |
'incidents' => [ [ 'mandatory' => 'title'] ] | |
] | |
] | |
] | |
] | |
] | |
] | |
] | |
*/ | |
protected static function flatten(array $array, $parent_prefix = '') | |
{ | |
$flattened = []; | |
foreach ($array as $key => $value) { | |
$key = $parent_prefix . $key; | |
$key = preg_replace('#(^\.|\.?(values|embedded_entity_commands)\.(\d|\.)?\.?)#', '', $key); | |
if (is_array($value)) { | |
if (isset($value['@incidents'])) { | |
$flattened[$key] = $value['@incidents']; | |
} else { | |
$flattened = array_merge(self::flatten($value, $key ? $key . '.' : ''), $flattened); | |
} | |
} else { | |
$flattened[$key] = $value; | |
} | |
} | |
return $flattened; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment