chg: [rearrange] moved to Entity
parent
1077251f8b
commit
095dd4513c
|
@ -15,92 +15,15 @@ use Cake\Routing\Router;
|
|||
|
||||
class APIRearrangeComponent extends Component
|
||||
{
|
||||
|
||||
private $rearrangeFunctions = [
|
||||
'App\Model\Entity\Organisation' => 'rearrangeOrganisation'
|
||||
];
|
||||
|
||||
public function rearrange(object $data): object
|
||||
public function rearrangeForAPI(object $data): object
|
||||
{
|
||||
if (get_class($data) === 'Cake\ORM\ResultSet') {
|
||||
$data->each(function ($value, $key) {
|
||||
$value = $this->rearrangeEntity($value);
|
||||
$value->rearrangeForAPI();
|
||||
});
|
||||
} else {
|
||||
$data = $this->rearrangeEntity($data);
|
||||
$data->rearrangeForAPI();
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -559,7 +559,7 @@ class RestResponseComponent extends Component
|
|||
$data['errors'] = $errors;
|
||||
}
|
||||
if (!$raw) {
|
||||
$data = $this->APIRearrange->rearrange($data);
|
||||
$data = $this->APIRearrange->rearrangeForAPI($data);
|
||||
}
|
||||
return $this->__sendResponse($data, 200, $format, $raw, $download, $headers);
|
||||
}
|
||||
|
|
|
@ -32,4 +32,52 @@ class AppModel extends Entity
|
|||
{
|
||||
return $this->_accessibleOnNew ?? [];
|
||||
}
|
||||
|
||||
public function rearrangeForAPI(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function rearrangeMetaFields(): void
|
||||
{
|
||||
$this->meta_fields = [];
|
||||
foreach ($this->MetaTemplates as $template) {
|
||||
foreach ($template['meta_template_fields'] as $field) {
|
||||
if ($field['counter'] > 0) {
|
||||
foreach ($field['metaFields'] as $metaField) {
|
||||
if (!empty($this->meta_fields[$template['name']][$field['field']])) {
|
||||
if (!is_array($this->meta_fields[$template['name']])) {
|
||||
$this->meta_fields[$template['name']][$field['field']] = [$this->meta_fields[$template['name']][$field['field']]];
|
||||
}
|
||||
$this->meta_fields[$template['name']][$field['field']][] = $metaField['value'];
|
||||
} else {
|
||||
$this->meta_fields[$template['name']][$field['field']] = $metaField['value'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function rearrangeTags(array $tags): array
|
||||
{
|
||||
foreach ($tags as &$tag) {
|
||||
unset($tag['_joinData']);
|
||||
}
|
||||
return $tags;
|
||||
}
|
||||
|
||||
public function rearrangeAlignments(array $alignments): array
|
||||
{
|
||||
$rearrangedAlignments = [];
|
||||
$validAlignmentTypes = ['individual', 'organisation'];
|
||||
foreach ($alignments as $alignment) {
|
||||
foreach ($validAlignmentTypes as $type) {
|
||||
if (isset($alignment[$type])) {
|
||||
$alignment[$type]['type'] = $alignment['type'];
|
||||
$rearrangedAlignments[$type][] = $alignment[$type];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $rearrangedAlignments;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,4 +16,20 @@ class Organisation extends AppModel
|
|||
protected $_accessibleOnNew = [
|
||||
'created' => true
|
||||
];
|
||||
|
||||
public function rearrangeForAPI(): void
|
||||
{
|
||||
if (!empty($this->tags)) {
|
||||
$this->tags = $this->rearrangeTags($this->tags);
|
||||
}
|
||||
if (!empty($this->alignments)) {
|
||||
$this->alignments = $this->rearrangeAlignments($this->alignments);
|
||||
}
|
||||
if (!empty($this->meta_fields)) {
|
||||
$this->rearrangeMetaFields();
|
||||
}
|
||||
if (!empty($this->MetaTemplates)) {
|
||||
unset($this->MetaTemplates);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue