[TASK] Improve api utility, additional relations
This commit is contained in:
parent
a3c8acf3be
commit
c8112b13f5
@ -12,11 +12,13 @@ namespace Cjel\TemplatesAide\Utility;
|
|||||||
*
|
*
|
||||||
***/
|
***/
|
||||||
|
|
||||||
|
use TYPO3\CMS\Core\Resource\FileReference as CoreFileReference;
|
||||||
|
use TYPO3\CMS\Extbase\Domain\Model\FileReference as ExtbaseFileReference;
|
||||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||||
use TYPO3\CMS\Extbase\Object\ObjectManager;
|
use TYPO3\CMS\Extbase\Object\ObjectManager;
|
||||||
use TYPO3\CMS\Extbase\Service\ImageService;
|
|
||||||
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
|
|
||||||
use TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage;
|
use TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage;
|
||||||
|
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
|
||||||
|
use TYPO3\CMS\Extbase\Service\ImageService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -42,8 +44,6 @@ class ApiUtility
|
|||||||
$mapping = [],
|
$mapping = [],
|
||||||
$rootRowClass = null
|
$rootRowClass = null
|
||||||
) {
|
) {
|
||||||
$httpHost = GeneralUtility::getIndpEnv('HTTP_HOST');
|
|
||||||
$requestHost = GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST');
|
|
||||||
$this->objectManager = GeneralUtility::makeInstance(
|
$this->objectManager = GeneralUtility::makeInstance(
|
||||||
ObjectManager::class
|
ObjectManager::class
|
||||||
);
|
);
|
||||||
@ -70,7 +70,6 @@ class ApiUtility
|
|||||||
$result[] = $rowResult;
|
$result[] = $rowResult;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$propertieResults = [];
|
$propertieResults = [];
|
||||||
foreach ($methods as $method) {
|
foreach ($methods as $method) {
|
||||||
if (substr($method, 0, 3) === 'get') {
|
if (substr($method, 0, 3) === 'get') {
|
||||||
@ -96,24 +95,14 @@ class ApiUtility
|
|||||||
$rowResult[$attributeName] = $methodResult;
|
$rowResult[$attributeName] = $methodResult;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ---
|
|
||||||
if (array_key_exists($rowClass, $mapping)) {
|
|
||||||
foreach ($mapping[$rowClass] as $attributeName => $function) {
|
|
||||||
$rowResult[$attributeName] = $function(
|
|
||||||
$rowResult[$attributeName],
|
|
||||||
$row
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// ---
|
|
||||||
foreach ($propertieResults as $attributeName => $methodResult) {
|
foreach ($propertieResults as $attributeName => $methodResult) {
|
||||||
|
// Date Time
|
||||||
|
|
||||||
if (gettype($methodResult) == 'object'
|
if (gettype($methodResult) == 'object'
|
||||||
&& get_class($methodResult) == 'DateTime'
|
&& get_class($methodResult) == 'DateTime'
|
||||||
) {
|
) {
|
||||||
$rowResult[$attributeName] = $methodResult->format('c');
|
$rowResult[$attributeName] = $methodResult->format('c');
|
||||||
}
|
}
|
||||||
|
// Simple related types
|
||||||
if (gettype($methodResult) == 'object'
|
if (gettype($methodResult) == 'object'
|
||||||
&& get_class($methodResult) == ObjectStorage::class
|
&& get_class($methodResult) == ObjectStorage::class
|
||||||
) {
|
) {
|
||||||
@ -122,25 +111,45 @@ class ApiUtility
|
|||||||
} else {
|
} else {
|
||||||
$nextLevelClass = $rootRowClass;
|
$nextLevelClass = $rootRowClass;
|
||||||
}
|
}
|
||||||
$rowResult[$attributeName] = self::queryResultToArray(
|
$imageStorage = true;
|
||||||
|
foreach ($methodResult->toArray() as $current) {
|
||||||
|
if (get_class($current) != ExtbaseFileReference::class)
|
||||||
|
{
|
||||||
|
$imageStorage = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$attributeResult = self::queryResultToArray(
|
||||||
$methodResult,
|
$methodResult,
|
||||||
$additionalAttributes[$attributeName],
|
$additionalAttributes[$attributeName],
|
||||||
$mapping,
|
$mapping,
|
||||||
$nextLevelClass
|
$nextLevelClass
|
||||||
);
|
);
|
||||||
|
if ($imageStorage) {
|
||||||
|
foreach ($attributeResult as &$attributeResultRow) {
|
||||||
|
if (array_key_exists(
|
||||||
|
'originalResource',
|
||||||
|
$attributeResultRow)
|
||||||
|
) {
|
||||||
|
$attributeResultRow
|
||||||
|
= $attributeResultRow['originalResource'];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$rowResult[$attributeName] = $attributeResult;
|
||||||
|
}
|
||||||
|
// Related objects
|
||||||
if (
|
if (
|
||||||
gettype($methodResult) == 'object'
|
gettype($methodResult) == 'object'
|
||||||
&&
|
&&
|
||||||
!in_array(get_class($methodResult), [
|
!in_array(get_class($methodResult), [
|
||||||
LazyObjectStorage::class,
|
LazyObjectStorage::class,
|
||||||
ObjectStorage::class,
|
ObjectStorage::class,
|
||||||
|
ExtbaseFileReference::class,
|
||||||
|
CoreFileReference::class,
|
||||||
])
|
])
|
||||||
&&
|
&&
|
||||||
count(explode('\\', get_class($methodResult))) > 1
|
count(explode('\\', get_class($methodResult))) > 1
|
||||||
) {
|
) {
|
||||||
|
|
||||||
if ($rootRowClass == null) {
|
if ($rootRowClass == null) {
|
||||||
$nextLevelClass = $rowClass;
|
$nextLevelClass = $rowClass;
|
||||||
} else {
|
} else {
|
||||||
@ -155,13 +164,58 @@ class ApiUtility
|
|||||||
$rowResult[$attributeName . 'Uid']
|
$rowResult[$attributeName . 'Uid']
|
||||||
= $rowResult[$attributeName]['uid'];
|
= $rowResult[$attributeName]['uid'];
|
||||||
}
|
}
|
||||||
|
// Images in object storage
|
||||||
if (gettype($methodResult) == 'object'
|
if (gettype($methodResult) == 'object'
|
||||||
&& get_class($methodResult) == LazyObjectStorage::class
|
&& get_class($methodResult) == LazyObjectStorage::class
|
||||||
) {
|
) {
|
||||||
$rowResult[$attributeName] = [];
|
$rowResult[$attributeName] = [];
|
||||||
foreach ($methodResult as $object) {
|
foreach ($methodResult as $object) {
|
||||||
$publicUrl = $object->getOriginalResource()
|
$rowResult[$attributeName]
|
||||||
->getPublicUrl();
|
= $this->filereferenceToApi(
|
||||||
|
$methodResult->getOriginalResource()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Images as file refernce
|
||||||
|
if (gettype($methodResult) == 'object'
|
||||||
|
&& get_class($methodResult) == ExtbaseFileReference::class
|
||||||
|
) {
|
||||||
|
$rowResult[$attributeName]
|
||||||
|
= $this->filereferenceToApi(
|
||||||
|
$methodResult->getOriginalResource()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// Images as core file reference
|
||||||
|
if (gettype($methodResult) == 'object'
|
||||||
|
&& get_class($methodResult) == CoreFileReference::class
|
||||||
|
) {
|
||||||
|
$rowResult[$attributeName]
|
||||||
|
= $this->filereferenceToApi($methodResult);
|
||||||
|
}
|
||||||
|
// If resut is empty set at least null so attribute is preesent
|
||||||
|
// in api
|
||||||
|
if (!isset($rowResult[$attributeName])) {
|
||||||
|
$rowResult[$attributeName] = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (array_key_exists($rowClass, $mapping)) {
|
||||||
|
foreach ($mapping[$rowClass] as $attributeName => $function) {
|
||||||
|
$rowResult[$attributeName] = $function(
|
||||||
|
$rowResult[$attributeName],
|
||||||
|
$row,
|
||||||
|
$rowResult
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$result[] = $rowResult;
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function filereferenceToApi($object) {
|
||||||
|
$httpHost = GeneralUtility::getIndpEnv('HTTP_HOST');
|
||||||
|
$requestHost = GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST');
|
||||||
|
$publicUrl = $object->getPublicUrl();
|
||||||
$absoluteUrl = $requestHost
|
$absoluteUrl = $requestHost
|
||||||
. '/'
|
. '/'
|
||||||
. $publicUrl;
|
. $publicUrl;
|
||||||
@ -191,7 +245,7 @@ class ApiUtility
|
|||||||
$absoluteUrlPreview = $requestHost
|
$absoluteUrlPreview = $requestHost
|
||||||
. '/'
|
. '/'
|
||||||
. $publicUrlPreview;
|
. $publicUrlPreview;
|
||||||
$rowResult[$attributeName][] = [
|
return [
|
||||||
'uid' => $object->getUid(),
|
'uid' => $object->getUid(),
|
||||||
'publicUrl' => $publicUrl,
|
'publicUrl' => $publicUrl,
|
||||||
'absoluteUrl' => $absoluteUrl,
|
'absoluteUrl' => $absoluteUrl,
|
||||||
@ -199,10 +253,5 @@ class ApiUtility
|
|||||||
'absoluteUrlPreview' => $absoluteUrlPreview,
|
'absoluteUrlPreview' => $absoluteUrlPreview,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
$result[] = $rowResult;
|
|
||||||
}
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user