From b9e5b76766dd812a64f5e3959719022b0a0e378f Mon Sep 17 00:00:00 2001 From: iglocska Date: Fri, 19 Aug 2022 13:00:19 +0200 Subject: [PATCH] new: [component] APIRearrange component added - alter the data's format before passing it back via the RestResponseComponent - to be used to clean up UI specific artifacts / junk - also to maintain compability between versions/tools --- .../Component/APIRearrangeComponent.php | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/Controller/Component/APIRearrangeComponent.php diff --git a/src/Controller/Component/APIRearrangeComponent.php b/src/Controller/Component/APIRearrangeComponent.php new file mode 100644 index 0000000..cd10ec6 --- /dev/null +++ b/src/Controller/Component/APIRearrangeComponent.php @@ -0,0 +1,106 @@ + 'rearrangeOrganisation' + ]; + + public function rearrange(object $data): object + { + if (get_class($data) === 'Cake\ORM\ResultSet') { + $data->each(function ($value, $key) { + $value = $this->rearrangeEntity($value); + }); + } else { + $data = $this->rearrangeEntity($data); + } + return $data; + } + + private function rearrangeEntity(object $entity): object + { + $entityClass = get_class($entity); + if (isset($this->rearrangeFunctions[$entityClass])) { + $entity = $this->{$this->rearrangeFunctions[$entityClass]}($entity); + } + return $entity; + } + + private function rearrangeOrganisation(object $entity): object + { + if (!empty($entity['tags'])) { + $entity['tags'] = $this->rearrangeTags($entity['tags']); + } + if (!empty($entity['alignments'])) { + $entity['alignments'] = $this->rearrangeAlignments($entity['alignments']); + } + if (!empty($entity['meta_fields'])) { + $entity = $this->rearrangeMetaFields($entity); + } + if (!empty($entity['MetaTemplates'])) { + unset($entity['MetaTemplates']); + } + return $entity; + } + + private function rearrangeMetaFields(object $entity): object + { + $entity['meta_fields'] = []; + foreach ($entity['MetaTemplates'] as $template) { + foreach ($template['meta_template_fields'] as $field) { + if ($field['counter'] > 0) { + foreach ($field['metaFields'] as $metaField) { + if (!empty($entity['meta_fields'][$template['name']][$field['field']])) { + if (!is_array($entity['meta_fields'][$template['name']])) { + $entity['meta_fields'][$template['name']][$field['field']] = [$entity['meta_fields'][$template['name']][$field['field']]]; + } + $entity['meta_fields'][$template['name']][$field['field']][] = $metaField['value']; + } else { + $entity['meta_fields'][$template['name']][$field['field']] = $metaField['value']; + } + } + } + } + } + return $entity; + } + + private function rearrangeTags(array $tags): array + { + foreach ($tags as &$tag) { + unset($tag['_joinData']); + } + return $tags; + } + + private function rearrangeAlignments(array $alignments): array + { + $rearrangedAlignments = []; + $validAlignmentTypes = ['individual', 'organisation']; + foreach ($alignments as $alignment) { + //debug($alignment); + foreach ($validAlignmentTypes as $type) { + if (isset($alignment[$type])) { + $alignment[$type]['type'] = $alignment['type']; + $rearrangedAlignments[$type][] = $alignment[$type]; + } + } + } + return $rearrangedAlignments; + } +} \ No newline at end of file