Skip to content

Commit

Permalink
Adjust all json transformers to minimize data overload
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelreichor committed Nov 15, 2024
1 parent 3ab651c commit 6babc1a
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 66 deletions.
6 changes: 2 additions & 4 deletions src/transformers/AddressTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ public function getTransformedData(array $predefinedFields = []): array
protected function getMetaData(): array
{
return [
'id' => $this->address->id,
'dateCreated' => $this->address->dateCreated,
'dateUpdated' => $this->address->dateUpdated,
];
'id' => $this->address->id,
];
}
}
36 changes: 18 additions & 18 deletions src/transformers/AssetTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ private function defaultImageTransformer(): array
{
$transformedFields = $this->getTransformedFields();
return array_merge([
'metadata' => $this->getMetaData(),
'height' => $this->asset->height,
'width' => $this->asset->width,
'focalPoint' => $this->asset->getFocalPoint(),
'url' => $this->asset->getUrl(),
], $transformedFields);
'metadata' => $this->getMetaData(),
'height' => $this->asset->getHeight(),
'width' => $this->asset->getWidth(),
'focalPoint' => $this->asset->getFocalPoint(),
'url' => $this->asset->getUrl(),
], $transformedFields);
}

/**
Expand All @@ -72,20 +72,20 @@ private function imagerXTransformer(): array
/**
* @return array
* @throws ImageTransformException
* @throws InvalidConfigException
*/
protected function getMetaData(): array
{
return [
'id' => $this->asset->id,
'filename' => $this->asset->filename,
'kind' => $this->asset->kind,
'size' => $this->asset->size,
'mimeType' => $this->asset->getMimeType(),
'extension' => $this->asset->extension,
'cpEditUrl' => $this->asset->cpEditUrl,
'dateCreated' => $this->asset->dateCreated,
'dateUpdated' => $this->asset->dateUpdated,
];
'id' => $this->asset->getId(),
'filename' => $this->asset->getFilename(),
'kind' => $this->asset->kind,
'size' => $this->asset->getFormattedSize(),
'mimeType' => $this->asset->getMimeType(),
'extension' => $this->asset->getExtension(),
'cpEditUrl' => $this->asset->getCpEditUrl(),
'volumeId' => $this->asset->volume->getId(),
];
}

/**
Expand All @@ -94,11 +94,11 @@ protected function getMetaData(): array
private function getAllAvailableSrcSets(): array
{
$transforms = $this->getTransformKeys();
$imagerx = Craft::$app->plugins->getPlugin('imager-x');
$imagerX = Craft::$app->plugins->getPlugin('imager-x');
$srcSetArr = [];

foreach ($transforms as $transform) {
$imagerClass = $imagerx->imager ?? null;
$imagerClass = $imagerX->imager ?? null;
if ($imagerClass) {
$transformedImages = $imagerClass->transformImage($this->asset, $transform);
$srcSetArr[$transform] = $imagerClass->srcset($transformedImages);
Expand Down
23 changes: 11 additions & 12 deletions src/transformers/BaseTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,8 @@ protected function transformNativeField(mixed $fieldValue, string $fieldClass):
protected function getMetaData(): array
{
return [
'id' => $this->element->id,
'dateCreated' => $this->element->dateCreated,
'dateUpdated' => $this->element->dateUpdated,
];
'id' => $this->element->getId(),
];
}

/**
Expand Down Expand Up @@ -200,6 +198,7 @@ protected function transformMatrixField(array $matrixFields): array
*
* @throws ImageTransformException
* @throws InvalidConfigException
* @throws InvalidFieldException
*/
protected function transformAssets(array $assets): array
{
Expand All @@ -222,11 +221,11 @@ protected function transformEntries(array $entries): array
$transformedData = [];
foreach ($entries as $entry) {
$transformedData[] = [
'title' => $entry->title,
'slug' => $entry->slug,
'url' => $entry->url,
'id' => $entry->id,
];
'title' => $entry->title,
'slug' => $entry->slug,
'url' => $entry->url,
'id' => $entry->id,
];
}
return $transformedData;
}
Expand Down Expand Up @@ -292,9 +291,9 @@ protected function transformLinks(mixed $link): array
}

return [
'elementType' => $link->type,
'value' => $link->value,
];
'elementType' => $link->type,
'value' => $link->value,
];
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/transformers/CategoryTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ public function getTransformedData(array $predefinedFields = []): array
protected function getMetaData(): array
{
return [
'id' => $this->category->id,
'dateCreated' => $this->category->dateCreated,
'dateUpdated' => $this->category->dateUpdated,
];
'id' => $this->category->id,
];
}
}
18 changes: 9 additions & 9 deletions src/transformers/EntryTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public function getTransformedData(array $predefinedFields = []): array
protected function getMetaData(): array
{
return array_merge(parent::getMetaData(), [
'sectionId' => $this->entry->sectionId,
'postDate' => $this->entry->postDate,
'siteId' => $this->entry->siteId,
'slug' => $this->entry->slug,
'uri' => $this->entry->uri,
'cpEditUrl' => $this->entry->cpEditUrl,
'status' => $this->entry->status,
'url' => $this->entry->url,
]);
'entryType' => $this->entry->type->getHandle(),
'sectionId' => $this->entry->section->getId(),
'siteId' => $this->entry->site->id,
'slug' => $this->entry->slug,
'uri' => $this->entry->uri,
'cpEditUrl' => $this->entry->getCpEditUrl(),
'status' => $this->entry->getStatus(),
'url' => $this->entry->getUrl(),
]);
}
}
14 changes: 6 additions & 8 deletions src/transformers/TagTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public function __construct(Tag $tag)
public function getTransformedData(array $predefinedFields = []): array
{
return [
'metadata' => $this->getMetaData(),
'title' => $this->tag->title,
'slug' => $this->tag->slug,
];
'metadata' => $this->getMetaData(),
'title' => $this->tag->title,
'slug' => $this->tag->slug,
];
}

/**
Expand All @@ -35,9 +35,7 @@ public function getTransformedData(array $predefinedFields = []): array
protected function getMetaData(): array
{
return [
'id' => $this->tag->id,
'dateCreated' => $this->tag->dateCreated,
'dateUpdated' => $this->tag->dateUpdated,
];
'id' => $this->tag->id,
];
}
}
20 changes: 9 additions & 11 deletions src/transformers/UserTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public function getTransformedData(array $predefinedFields = []): array
$transformedFields = $this->getTransformedFields();

return array_merge([
'metadata' => $this->getMetaData(),
'username' => $this->user->username,
'email' => $this->user->email,
'fullName' => $this->user->fullName,
], $transformedFields);
'metadata' => $this->getMetaData(),
'username' => $this->user->username,
'email' => $this->user->email,
'fullName' => $this->user->fullName,
], $transformedFields);
}

/**
Expand All @@ -37,11 +37,9 @@ public function getTransformedData(array $predefinedFields = []): array
protected function getMetaData(): array
{
return [
'id' => $this->user->id,
'status' => $this->user->status,
'cpEditUrl' => $this->user->cpEditUrl,
'dateCreated' => $this->user->dateCreated,
'dateUpdated' => $this->user->dateUpdated,
];
'id' => $this->user->id,
'status' => $this->user->status,
'cpEditUrl' => $this->user->cpEditUrl,
];
}
}

0 comments on commit 6babc1a

Please sign in to comment.