Add api mappers support to RecordListStateProvider

This commit is contained in:
Clemente Raposo 2024-11-06 09:51:10 +00:00 committed by Jack Anderson
parent 473c3a9826
commit 74b477dbcf
2 changed files with 36 additions and 3 deletions

View file

@ -29,8 +29,10 @@ namespace App\Data\DataProvider;
use ApiPlatform\Metadata\Operation; use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface; use ApiPlatform\State\ProviderInterface;
use App\Data\Entity\Record;
use App\Data\Entity\RecordList; use App\Data\Entity\RecordList;
use App\Data\LegacyHandler\RecordListHandler; use App\Data\LegacyHandler\RecordListHandler;
use App\Data\Service\Record\Mappers\RecordMapperRunnerInterface;
/** /**
* Class RecordListStateProvider * Class RecordListStateProvider
@ -42,14 +44,19 @@ class RecordListStateProvider implements ProviderInterface
* @var RecordListHandler * @var RecordListHandler
*/ */
protected $recordListHandler; protected $recordListHandler;
protected RecordMapperRunnerInterface $apiRecordMapperRunner;
/** /**
* RecordListStateProvider constructor. * RecordListStateProvider constructor.
* @param RecordListHandler $recordListHandler * @param RecordListHandler $recordListHandler
* @param RecordMapperRunnerInterface $apiRecordMapperRunner
*/ */
public function __construct(RecordListHandler $recordListHandler) public function __construct(
{ RecordListHandler $recordListHandler,
RecordMapperRunnerInterface $apiRecordMapperRunner
) {
$this->recordListHandler = $recordListHandler; $this->recordListHandler = $recordListHandler;
$this->apiRecordMapperRunner = $apiRecordMapperRunner;
} }
/** /**
@ -60,6 +67,19 @@ class RecordListStateProvider implements ProviderInterface
*/ */
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?RecordList public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?RecordList
{ {
return $this->recordListHandler->getList($uriVariables['id'] ?? ''); $list = $this->recordListHandler->getList($uriVariables['id'] ?? '');
$mappedRecords = [];
foreach ($list->getRecords() as $recordArray) {
$record = new Record();
$record->fromArray($recordArray);
$this->apiRecordMapperRunner->toExternal($record, 'list');
$mappedRecords[] = $record->toArray();
}
$list->setRecords($mappedRecords);
return $list;
} }
} }

View file

@ -153,6 +153,19 @@ class Record
]; ];
} }
/**
* @param array $recordData
* @return void
*/
public function fromArray(array $recordData): void
{
$this->setId($recordData['id'] ?? '');
$this->setModule($recordData['module']?? '');
$this->setType($recordData['type']?? '');
$this->setAttributes($recordData['attributes'] ?? []);
$this->setAcls($recordData['acls'] ?? []);
}
/** /**
* @return string|null * @return string|null
*/ */