From a962fc6abcfe6335a3a862054be52c3686262ec0 Mon Sep 17 00:00:00 2001 From: Ruben van der Linde Date: Thu, 19 Jan 2023 21:23:33 +0100 Subject: [PATCH 1/6] Firts mapper code --- Service/MappingService.php | 44 +++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/Service/MappingService.php b/Service/MappingService.php index b7a21c44..5ff0e77d 100644 --- a/Service/MappingService.php +++ b/Service/MappingService.php @@ -4,19 +4,57 @@ use App\Entity\Mapping; use Doctrine\ORM\EntityManagerInterface; +use Monolog\Logger; class MappingService { - private EntityManagerInterface $em; + private EntityManagerInterface $em;; + private Logger $logger; public function __construct( EntityManagerInterface $em ) { $this->em = $em; + $this->logger = New Logger('cache'); } - public function mapping(Mapping $mappingObject, array $requestMapping): array + public function mapping(Mapping $mappingObject, array $input): array { - return $requestMapping; + $output = []; + + // Check for troughput + if($mappingObject->getPassTrough()){ + $output = $input; + } + + // Lets get the dot array bassed on https://github.com/adbario/php-dot-notation + $dotArray = dot($output); + + // Lets do the actual mapping + foreach($mappingObject->getMapping() as $key => $value){ + $dotArray->set($key, $value); + } + + // Back to arrray + $output = $dotArray->all(); + + // Unset unwanted key's + foreach ($mappingObject->getUnset() as $unset){ + if(!isset($output[$unset])){ + $this->logger->error("Trying to unset and property that doensnt exist during mapping"); + continue; + } + $unset($output[$unset]); + } + + // Log the result + $this->logger->info('Mapped object',[ + "input" => $input, + "output" => $output, + "passTrough" => $mappingObject->getPassTrough(), + "mapping" => $mappingObject->getMapping(), + ]); + + return $output; } } From 0baea029505ca3ccd0fbd2f3d0a361e28c835082 Mon Sep 17 00:00:00 2001 From: Ruben van der Linde Date: Thu, 19 Jan 2023 21:46:32 +0100 Subject: [PATCH 2/6] Operationalizing the service --- Service/MappingService.php | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/Service/MappingService.php b/Service/MappingService.php index 5ff0e77d..64fbd3c5 100644 --- a/Service/MappingService.php +++ b/Service/MappingService.php @@ -15,7 +15,7 @@ public function __construct( EntityManagerInterface $em ) { $this->em = $em; - $this->logger = New Logger('cache'); + $this->logger = New Logger('mapping'); } public function mapping(Mapping $mappingObject, array $input): array @@ -35,18 +35,41 @@ public function mapping(Mapping $mappingObject, array $input): array $dotArray->set($key, $value); } - // Back to arrray - $output = $dotArray->all(); - // Unset unwanted key's foreach ($mappingObject->getUnset() as $unset){ - if(!isset($output[$unset])){ - $this->logger->error("Trying to unset and property that doensnt exist during mapping"); + if(!$dotArray->has($unset)){ + $this->logger->error("Trying to unset an property that doensnt exist during mapping"); + continue; + } + $dotArray->delete($unset); + } + + // Cast values to a specific type + foreach ($mappingObject->getCast() as $key => $cast){ + if(!$dotArray->has($key)){ + $this->logger->error("Trying to cast an property that doensnt exist during mapping"); continue; } - $unset($output[$unset]); + + $value = $dotArray->get($key); + + switch ($cast) { + case 'int': + case 'integer': + $value = intval($value); + break; + // Todo: Add more casts + default: + $this->logger->error("Trying to cast to an unsuported cast type: ".$cast); + continue; + } + + $dotArray->set($key, $value); } + // Back to arrray + $output = $dotArray->all(); + // Log the result $this->logger->info('Mapped object',[ "input" => $input, From 2d5ec7bbdac9532701f7c6318fb951aa2eaff9c1 Mon Sep 17 00:00:00 2001 From: Ruben van der Linde Date: Thu, 19 Jan 2023 21:56:04 +0100 Subject: [PATCH 3/6] Twig Render --- Service/MappingService.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Service/MappingService.php b/Service/MappingService.php index 64fbd3c5..36fcc274 100644 --- a/Service/MappingService.php +++ b/Service/MappingService.php @@ -5,16 +5,22 @@ use App\Entity\Mapping; use Doctrine\ORM\EntityManagerInterface; use Monolog\Logger; +use Twig\Environment; class MappingService { private EntityManagerInterface $em;; private Logger $logger; + // Create a private variable to store the twig environment + private Environment $twig; + public function __construct( - EntityManagerInterface $em + EntityManagerInterface $em, + Environment $twig ) { $this->em = $em; + $this->twig = $twig; $this->logger = New Logger('mapping'); } @@ -32,7 +38,8 @@ public function mapping(Mapping $mappingObject, array $input): array // Lets do the actual mapping foreach($mappingObject->getMapping() as $key => $value){ - $dotArray->set($key, $value); + // Render the value from twig + $dotArray->set($key, $twig->createTemplate($value)->render($input)); } // Unset unwanted key's @@ -69,7 +76,7 @@ public function mapping(Mapping $mappingObject, array $input): array // Back to arrray $output = $dotArray->all(); - + // Log the result $this->logger->info('Mapped object',[ "input" => $input, From 8e018a38fbb2d591303c7b138e628e8eed409e03 Mon Sep 17 00:00:00 2001 From: Ruben van der Linde Date: Fri, 20 Jan 2023 06:33:45 +0100 Subject: [PATCH 4/6] Add logig, documentation and fixes set id fix for installer --- Service/CacheService.php | 23 ++++++- Service/InstallationService.php | 116 ++++++++++++++++++++++++++------ Service/MappingService.php | 21 +++++- Service/RequestService.php | 22 +++--- 4 files changed, 145 insertions(+), 37 deletions(-) diff --git a/Service/CacheService.php b/Service/CacheService.php index d33f1e7a..64434b5c 100644 --- a/Service/CacheService.php +++ b/Service/CacheService.php @@ -10,6 +10,7 @@ use Doctrine\ORM\NonUniqueResultException; use Exception; use MongoDB\Client; +use Monolog\Logger; use Ramsey\Uuid\Uuid; use Symfony\Component\Cache\Adapter\AdapterInterface as CacheInterface; use Symfony\Component\Console\Command\Command; @@ -38,10 +39,16 @@ class CacheService private ParameterBagInterface $parameters; private SerializerInterface $serializer; + // Add monolog logger bundle for the generic logging interface + private Logger $logger; + /** - * @param AuthenticationService $authenticationService + * Setting up the base class with required services + * * @param EntityManagerInterface $entityManager - * @param FileService $fileService + * @param CacheInterface $cache + * @param ParameterBagInterface $parameters + * @param SerializerInterface $serializer */ public function __construct( EntityManagerInterface $entityManager, @@ -56,6 +63,7 @@ public function __construct( if ($this->parameters->get('cache_url', false)) { $this->client = new Client($this->parameters->get('cache_url')); } + $this->logger = New Logger('cache'); } /** @@ -239,8 +247,12 @@ public function cacheObject(ObjectEntity $objectEntity): ObjectEntity ['upsert'=>true] )) { (isset($this->io) ? $this->io->writeln('Updated object '.$objectEntity->getId()->toString().' of type '.$objectEntity->getEntity()->getName().' to cache') : ''); + + $this->logger->debug("updated object to cache"); } else { (isset($this->io) ? $this->io->writeln('Wrote object '.$objectEntity->getId()->toString().' of type '.$objectEntity->getEntity()->getName().' to cache') : ''); + + $this->logger->debug("Added object to cache"); } return $objectEntity; @@ -264,6 +276,8 @@ public function removeObject(ObjectEntity $object): void $collection = $this->client->objects->json; $collection->findOneAndDelete(['_id'=>$id]); + + $this->logger->debug("Removed object from cache"); } /** @@ -284,11 +298,13 @@ public function getObject(string $id) // Check if object is in the cache ???? if ($object = $collection->findOne(['_id'=>$id])) { + $this->logger->debug("Retrieved cached object from cache"); return $object; } // Fall back tot the entity manager if ($object = $this->entityManager->getRepository('App:ObjectEntity')->findOneBy(['id'=>$id])) { + $this->logger->debug("Retrieved uncached object from cache"); return $this->cacheObject($object)->toArray(['embedded' => true]); } @@ -365,6 +381,9 @@ public function searchObjects(string $search = null, array $filter = [], array $ $results = $collection->find($filter, ['limit' => $limit, 'skip' => $start, 'sort' => $order])->toArray(); $total = $collection->count($filter); + + $this->logger->debug("Searched cache for objects"); + // Make sure to add the pagination properties in response return $this->handleResultPagination($completeFilter, $results, $total); } diff --git a/Service/InstallationService.php b/Service/InstallationService.php index cdd7b221..0a95d9ac 100644 --- a/Service/InstallationService.php +++ b/Service/InstallationService.php @@ -11,6 +11,7 @@ use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Finder\Finder; +use Monolog\Logger; class InstallationService { @@ -19,6 +20,16 @@ class InstallationService private SymfonyStyle $io; private $container; + // Add monolog logger bundle for the generic logging interface + private Logger $logger; + + /** + * Setting up the base class with required services + * + * @param ComposerService $composerService + * @param EntityManagerInterface $em + * @param Kernel $kernel + */ public function __construct( ComposerService $composerService, EntityManagerInterface $em, @@ -28,6 +39,8 @@ public function __construct( $this->em = $em; $this->container = $kernel->getContainer(); $this->collection = null; + $this->logger = New Logger('installation'); + } /** @@ -59,7 +72,10 @@ public function composerupdate(): int ]); } + $this->logger->debug("Running plugin installer"); + foreach ($plugins as $plugin) { + $this->install($plugin['name']); } @@ -84,6 +100,8 @@ public function install(string $bundle, bool $noSchema = false): int ]); } + $this->logger->debug('Trying to install: '.$bundle); + $packages = $this->composerService->getAll(); $found = array_filter($packages, function ($v, $k) use ($bundle) { @@ -123,6 +141,8 @@ public function install(string $bundle, bool $noSchema = false): int // We want each plugin to also be a collection (if it contains schema's that is) if (count($schemas) > 0) { if (!$this->collection = $this->em->getRepository('App:CollectionEntity')->findOneBy(['plugin' => $package['name']])) { + + $this->logger->debug('Created a collection for plugin '.$bundle); $this->io->writeln(['Created a collection for this plugin', '']); $this->collection = new CollectionEntity(); $this->collection->setName($package['name']); @@ -130,6 +150,7 @@ public function install(string $bundle, bool $noSchema = false): int isset($package['description']) && $this->collection->setDescription($package['description']); } else { $this->io->writeln(['Found a collection for this plugin', '']); + $this->logger->debug('Found a collection for plugin '.$bundle); } } @@ -145,6 +166,7 @@ public function install(string $bundle, bool $noSchema = false): int //$progressBar->finish(); } else { $this->io->writeln('No schema folder found'); + $this->logger->debug('No schema folder found for plugin '.$bundle); } // Handling the data @@ -163,6 +185,7 @@ public function install(string $bundle, bool $noSchema = false): int // We need to clear the finder } else { + $this->logger->debug('No data folder found for plugin '.$bundle); $this->io->writeln('No data folder found'); } @@ -179,10 +202,12 @@ public function install(string $bundle, bool $noSchema = false): int $this->handleInstaller($installer); } } else { + $this->logger->debug('No Installation folder found for plugin '.$bundle); $this->io->writeln('No Installation folder found'); } $this->io->success('All Done'); + $this->logger->debug('All Done installing plugin '.$bundle); return Command::SUCCESS; } @@ -295,31 +320,15 @@ public function handleData($file) if (array_key_exists('_id', $object) && $objectEntity = $this->em->getRepository('App:ObjectEntity')->findOneBy(['id' => $object['_id']])) { $this->io->writeln(['', 'Object '.$object['_id'].' already exists, so updating']); - } elseif (array_key_exists('_id', $object)) { - $this->io->writeln('Set id to '.$object['_id']); - - // Nice doctrine setId shizzle - $objectEntity = new ObjectEntity(); - $this->em->persist($objectEntity); - $objectEntity->setId($object['_id']); - $this->em->persist($objectEntity); - $this->em->flush(); - $this->em->refresh($objectEntity); - - $objectEntity = $this->em->getRepository('App:ObjectEntity')->findOneBy(['id' => $object['_id']]); - - $objectEntity->setEntity($entity); - - $this->io->writeln('Creating new object with existing id '.$objectEntity->getId()); } else { $objectEntity = new ObjectEntity($entity); - $this->io->writeln(['', 'Creating new object']); } $this->io->writeln('Writing data to the object'); $objectEntity->hydrate($object); - $this->em->persist($objectEntity); - $this->em->flush(); + + $this->saveOnFixedId($objectEntity); + $this->io->writeln(['Object saved as '.$objectEntity->getId(), '']); } } @@ -349,4 +358,73 @@ public function handleInstaller($file) return $installationService->install(); } + + /** + * Handles forced id's on object entities + * + * @param ObjectEntity $objectEntity + * @return ObjectEntity + */ + private function saveOnFixedId(ObjectEntity $objectEntity): ObjectEntity{ + // This savetey dosn't make sense but we need it + if(!$objectEntity->getEntity()){ + + $this->logger->error('Object can\'t be persisted due to missing schema'); + $this->io->writeln(['', 'Object can\'t be persisted due to missing schema']); + return $objectEntity; + } + + // Save the values + $values = $objectEntity->getObjectValues()->toArray(); + $objectEntity->clearAllValues(); + + // We have an object entity with a fixed id that isn't in the database, so we need to act + if($objectEntity->getId() && !$this->em->contains($objectEntity)){ + + $this->io->writeln(['', 'Creating new object ('.$objectEntity->getEntity()->getName().') on a fixed id ('.$objectEntity->getId().')']); + + // Sve the id + $id = $objectEntity->getId(); + // Create the entity + $this->em->persist($objectEntity); + $this->em->flush(); + $this->em->refresh($objectEntity); + // Reset the id + $objectEntity->setId($id); + $this->em->persist($objectEntity); + $this->em->flush(); + $objectEntity = $this->em->getRepository('App:ObjectEntity')->findOneBy(['id' => $id]); + } + else{ + $this->io->writeln(['', 'Creating new object ('.$objectEntity->getEntity()->getName().') on a generated id']); + } + + // Loop trough the values + foreach ($values as $objectValue){ + $objectEntity->addObjectValue($objectValue); + + // If the value itsself is an object it might also contain fixed id's + foreach ($objectValue->getObjects() as $subobject){ + + $this->io->writeln(['', 'Found sub object ('.$subobject->getEntity()->getName().')']); + + if($subobject->getEntity()){ + $this->io->writeln(['subobject has schema so can be saved']); + $subobject = $this->saveOnFixedId($subobject); + } + else{ + $this->io->warning(['subobject has NO schema so can\'t be saved']); + $objectValue->removeObject($subobject); + } + } + } + + $this->em->persist($objectEntity); + $this->em->flush(); + + return $objectEntity; + } + + + } diff --git a/Service/MappingService.php b/Service/MappingService.php index 36fcc274..b40957ae 100644 --- a/Service/MappingService.php +++ b/Service/MappingService.php @@ -7,23 +7,38 @@ use Monolog\Logger; use Twig\Environment; +/** + * The mapping service handels the mapping (or transformation) of array A (input) to array B (output) + * + * More information on how to write your own mappings can be found at [/docs/mapping.md](/docs/mapping.md). + */ class MappingService { - private EntityManagerInterface $em;; + // Add monolog logger bundle for the generic logging interface private Logger $logger; // Create a private variable to store the twig environment private Environment $twig; + /** + * Setting up the base class with required services + * + * @param Environment $twig + */ public function __construct( - EntityManagerInterface $em, Environment $twig ) { - $this->em = $em; $this->twig = $twig; $this->logger = New Logger('mapping'); } + /** + * Maps (transforms) an array (input) to a differend array (output) + * + * @param Mapping $mappingObject The mapping object that formes the recepy for the mapping + * @param array $input The array that need to be mapped (transformed) otherwise known as input + * @return array The result (output) of the mapping procces + */ public function mapping(Mapping $mappingObject, array $input): array { $output = []; diff --git a/Service/RequestService.php b/Service/RequestService.php index 71557876..af8f02d3 100644 --- a/Service/RequestService.php +++ b/Service/RequestService.php @@ -14,15 +14,15 @@ use App\Service\ResponseService; use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\EntityManagerInterface; +use Monolog\Logger; use Ramsey\Uuid\Uuid; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\Security\Core\Security; -use Symfony\Component\Serializer\SerializerInterface; /** - * Handles incomming request from endpoints or controllers that relate to the gateways object structure (eav). + * Handles incomming request and generates a response */ class RequestService { @@ -40,9 +40,13 @@ class RequestService private CallService $callService; private Security $security; private EventDispatcherInterface $eventDispatcher; - private SerializerInterface $serializer; + + // Add monolog logger bundle for the generic logging interface + private Logger $logger; /** + * Setting up the base class with required services + * * @param EntityManagerInterface $entityManager * @param CacheService $cacheService * @param ResponseService $responseService @@ -51,7 +55,6 @@ class RequestService * @param CallService $callService * @param Security $security * @param EventDispatcherInterface $eventDispatcher - * @param SerializerInterface $serializer */ public function __construct( EntityManagerInterface $entityManager, @@ -61,8 +64,7 @@ public function __construct( LogService $logService, CallService $callService, Security $security, - EventDispatcherInterface $eventDispatcher, - SerializerInterface $serializer + EventDispatcherInterface $eventDispatcher ) { $this->entityManager = $entityManager; $this->cacheService = $cacheService; @@ -72,7 +74,6 @@ public function __construct( $this->callService = $callService; $this->security = $security; $this->eventDispatcher = $eventDispatcher; - $this->serializer = $serializer; } /** @@ -393,12 +394,7 @@ public function requestHandler(array $data, array $configuration): Response // If we do not have an object we throw an 404 if (!$result) { - return new Response($this->serializer->serialize([ - 'message' => 'Could not find an object with id '.$this->id, - 'type' => 'Bad Request', - 'path' => implode(', ', $allowedSchemas['name']), - 'data' => ['id' => $this->id], - ], 'json'), Response::HTTP_NOT_FOUND); + return new Response('Object '.$this->id.' not found', '404'); } // Lets see if the found result is allowd for this endpoint From a6210ac0352622a70537fd4a183c4d947bd12e0d Mon Sep 17 00:00:00 2001 From: GitHub Actions <> Date: Fri, 20 Jan 2023 05:34:16 +0000 Subject: [PATCH 5/6] Update phpdoc --- docs/classes/Service/CacheService.md | 11 +++++++---- docs/classes/Service/InstallationService.md | 10 ++++++---- docs/classes/Service/RequestService.md | 17 ++++++++++++----- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/docs/classes/Service/CacheService.md b/docs/classes/Service/CacheService.md index 3c92a1fb..663b214f 100644 --- a/docs/classes/Service/CacheService.md +++ b/docs/classes/Service/CacheService.md @@ -12,7 +12,7 @@ This service provides a guzzle wrapper to work with sources in the common gatewa | Name | Description | |------|-------------| -|[__construct](#cacheservice__construct)|| +|[__construct](#cacheservice__construct)|Setting up the base class with required services| |[cacheEndpoint](#cacheservicecacheendpoint)|Put a single endpoint into the cache.| |[cacheObject](#cacheservicecacheobject)|Put a single object into the cache.| |[cacheShema](#cacheservicecacheshema)|Put a single schema into the cache.| @@ -37,16 +37,19 @@ This service provides a guzzle wrapper to work with sources in the common gatewa **Description** ```php - __construct (void) +public __construct (\EntityManagerInterface $entityManager, \CacheInterface $cache, \ParameterBagInterface $parameters, \SerializerInterface $serializer) ``` - +Setting up the base class with required services **Parameters** -`This function has no parameters.` +* `(\EntityManagerInterface) $entityManager` +* `(\CacheInterface) $cache` +* `(\ParameterBagInterface) $parameters` +* `(\SerializerInterface) $serializer` **Return Values** diff --git a/docs/classes/Service/InstallationService.md b/docs/classes/Service/InstallationService.md index 80bd9e8a..c412e882 100644 --- a/docs/classes/Service/InstallationService.md +++ b/docs/classes/Service/InstallationService.md @@ -10,7 +10,7 @@ | Name | Description | |------|-------------| -|[__construct](#installationservice__construct)|| +|[__construct](#installationservice__construct)|Setting up the base class with required services| |[composerupdate](#installationservicecomposerupdate)|| |[handleData](#installationservicehandledata)|| |[handleInstaller](#installationservicehandleinstaller)|| @@ -29,16 +29,18 @@ **Description** ```php - __construct (void) +public __construct (\ComposerService $composerService, \EntityManagerInterface $em, \Kernel $kernel) ``` - +Setting up the base class with required services **Parameters** -`This function has no parameters.` +* `(\ComposerService) $composerService` +* `(\EntityManagerInterface) $em` +* `(\Kernel) $kernel` **Return Values** diff --git a/docs/classes/Service/RequestService.md b/docs/classes/Service/RequestService.md index 932aa387..23cf904e 100644 --- a/docs/classes/Service/RequestService.md +++ b/docs/classes/Service/RequestService.md @@ -1,6 +1,6 @@ # CommonGateway\CoreBundle\Service\RequestService -Handles incomming request from endpoints or controllers that relate to the gateways object structure (eav). +Handles incomming request and generates a response @@ -10,7 +10,7 @@ Handles incomming request from endpoints or controllers that relate to the gatew | Name | Description | |------|-------------| -|[__construct](#requestservice__construct)|| +|[__construct](#requestservice__construct)|Setting up the base class with required services| |[checkEmbedded](#requestservicecheckembedded)|If embedded should be shown or not.| |[createResponse](#requestservicecreateresponse)|Creating the responce object.| |[getId](#requestservicegetid)|Get the ID from given parameters.| @@ -30,16 +30,23 @@ Handles incomming request from endpoints or controllers that relate to the gatew **Description** ```php - __construct (void) +public __construct (\EntityManagerInterface $entityManager, \CacheService $cacheService, \ResponseService $responseService, \ObjectEntityService $objectEntityService, \LogService $logService, \CallService $callService, \Security $security, \EventDispatcherInterface $eventDispatcher) ``` - +Setting up the base class with required services **Parameters** -`This function has no parameters.` +* `(\EntityManagerInterface) $entityManager` +* `(\CacheService) $cacheService` +* `(\ResponseService) $responseService` +* `(\ObjectEntityService) $objectEntityService` +* `(\LogService) $logService` +* `(\CallService) $callService` +* `(\Security) $security` +* `(\EventDispatcherInterface) $eventDispatcher` **Return Values** From 10f63f9e7b17715ebb465e7404e763735523705b Mon Sep 17 00:00:00 2001 From: Sarai Misidjan Date: Fri, 20 Jan 2023 15:10:48 +0100 Subject: [PATCH 6/6] fix grammer --- Service/MappingService.php | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/Service/MappingService.php b/Service/MappingService.php index b40957ae..68d9dddb 100644 --- a/Service/MappingService.php +++ b/Service/MappingService.php @@ -8,7 +8,7 @@ use Twig\Environment; /** - * The mapping service handels the mapping (or transformation) of array A (input) to array B (output) + * The mapping service handles the mapping (or transformation) of array A (input) to array B (output) * * More information on how to write your own mappings can be found at [/docs/mapping.md](/docs/mapping.md). */ @@ -33,34 +33,34 @@ public function __construct( } /** - * Maps (transforms) an array (input) to a differend array (output) + * Maps (transforms) an array (input) to a different array (output) * - * @param Mapping $mappingObject The mapping object that formes the recepy for the mapping + * @param Mapping $mappingObject The mapping object that forms the recipe for the mapping * @param array $input The array that need to be mapped (transformed) otherwise known as input - * @return array The result (output) of the mapping procces + * @return array The result (output) of the mapping process */ public function mapping(Mapping $mappingObject, array $input): array { $output = []; - // Check for troughput + // Check for throughput if($mappingObject->getPassTrough()){ $output = $input; } - // Lets get the dot array bassed on https://github.com/adbario/php-dot-notation + // Let's get the dot array based on https://github.com/adbario/php-dot-notation $dotArray = dot($output); - // Lets do the actual mapping + // Let's do the actual mapping foreach($mappingObject->getMapping() as $key => $value){ // Render the value from twig - $dotArray->set($key, $twig->createTemplate($value)->render($input)); + $dotArray->set($key, $this->twig->createTemplate($value)->render($input)); } // Unset unwanted key's foreach ($mappingObject->getUnset() as $unset){ if(!$dotArray->has($unset)){ - $this->logger->error("Trying to unset an property that doensnt exist during mapping"); + $this->logger->error("Trying to unset an property that doesn't exist during mapping"); continue; } $dotArray->delete($unset); @@ -69,7 +69,7 @@ public function mapping(Mapping $mappingObject, array $input): array // Cast values to a specific type foreach ($mappingObject->getCast() as $key => $cast){ if(!$dotArray->has($key)){ - $this->logger->error("Trying to cast an property that doensnt exist during mapping"); + $this->logger->error("Trying to cast an property that doesn't exist during mapping"); continue; } @@ -80,16 +80,27 @@ public function mapping(Mapping $mappingObject, array $input): array case 'integer': $value = intval($value); break; + case 'string': + break; + case 'array': + // + break; + case 'date': + /// + break; + case 'datetime': + //// + break; // Todo: Add more casts default: - $this->logger->error("Trying to cast to an unsuported cast type: ".$cast); - continue; + $this->logger->error("Trying to cast to an unsupported cast type: ".$cast); + break; } $dotArray->set($key, $value); } - // Back to arrray + // Back to array $output = $dotArray->all(); // Log the result