mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-22 01:49:05 +02:00
Allow alternative names for import for parts
This commit is contained in:
parent
a1f4b35749
commit
4437f206af
1 changed files with 21 additions and 1 deletions
|
@ -30,8 +30,13 @@ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
|||
|
||||
class PartNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
|
||||
{
|
||||
private const DENORMALIZE_KEY_MAPPING = [
|
||||
'notes' => 'comment',
|
||||
'quantity' => 'instock',
|
||||
'amount' => 'instock',
|
||||
];
|
||||
|
||||
private NormalizerInterface $normalizer;
|
||||
private ObjectNormalizer $normalizer;
|
||||
private StructuralElementFromNameDenormalizer $locationDenormalizer;
|
||||
|
||||
public function __construct(ObjectNormalizer $normalizer, StructuralElementFromNameDenormalizer $locationDenormalizer)
|
||||
|
@ -68,8 +73,23 @@ class PartNormalizer implements NormalizerInterface, DenormalizerInterface, Cach
|
|||
return is_array($data) && is_a($type, Part::class, true);
|
||||
}
|
||||
|
||||
private function normalizeKeys(array &$data): array
|
||||
{
|
||||
//Rename keys based on the mapping, while leaving the data untouched
|
||||
foreach ($data as $key => $value) {
|
||||
if (isset(self::DENORMALIZE_KEY_MAPPING[$key])) {
|
||||
$data[self::DENORMALIZE_KEY_MAPPING[$key]] = $value;
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function denormalize($data, string $type, string $format = null, array $context = [])
|
||||
{
|
||||
$this->normalizeKeys($data);
|
||||
|
||||
$object = $this->normalizer->denormalize($data, $type, $format, $context);
|
||||
|
||||
if (!$object instanceof Part) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue