mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-25 03:08:51 +02:00
Added API endpoints for more data structures
This commit is contained in:
parent
9bd1b86f6e
commit
e04b635c98
8 changed files with 233 additions and 18 deletions
|
@ -22,13 +22,15 @@ declare(strict_types=1);
|
|||
*/
|
||||
namespace App\Serializer;
|
||||
|
||||
use Brick\Math\BigDecimal;
|
||||
use Brick\Math\BigNumber;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
/**
|
||||
* @see \App\Tests\Serializer\BigNumberNormalizerTest
|
||||
*/
|
||||
class BigNumberNormalizer implements NormalizerInterface
|
||||
class BigNumberNormalizer implements NormalizerInterface, DenormalizerInterface
|
||||
{
|
||||
|
||||
public function supportsNormalization($data, string $format = null, array $context = []): bool
|
||||
|
@ -52,6 +54,22 @@ class BigNumberNormalizer implements NormalizerInterface
|
|||
{
|
||||
return [
|
||||
BigNumber::class => true,
|
||||
BigDecimal::class => true,
|
||||
];
|
||||
}
|
||||
|
||||
public function denormalize(mixed $data, string $type, string $format = null, array $context = [])
|
||||
{
|
||||
if (!is_a($type, BigNumber::class, true)) {
|
||||
throw new \InvalidArgumentException('This normalizer only supports BigNumber objects!');
|
||||
}
|
||||
|
||||
return $type::of($data);
|
||||
}
|
||||
|
||||
public function supportsDenormalization(mixed $data, string $type, string $format = null)
|
||||
{
|
||||
//data must be a string or a number (int, float, etc.) and the type must be BigNumber or BigDecimal
|
||||
return (is_string($data) || is_numeric($data)) && (is_subclass_of($type, BigNumber::class));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue