generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3001230
commit dc92bf2
Showing
6 changed files
with
149 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Morrislaptop\SymfonyCustomNormalizers; | ||
|
||
use Carbon\Carbon; | ||
use Carbon\CarbonInterface; | ||
use InvalidArgumentException; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
class CarbonNormalizer implements NormalizerInterface, DenormalizerInterface | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function normalize($object, string $format = null, array $context = []) | ||
{ | ||
if (! $object instanceof CarbonInterface) { | ||
throw new InvalidArgumentException('Cannot serialize an object that is not a CarbonInterface in CarbonNormalizer.'); | ||
} | ||
|
||
return $object->toRfc3339String(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function supportsNormalization($data, string $format = null) | ||
{ | ||
return $data instanceof CarbonInterface; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function denormalize($data, string $type, string $format = null, array $context = []) | ||
{ | ||
return new Carbon($data); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function supportsDenormalization($data, string $type, string $format = null) | ||
{ | ||
return is_a($type, CarbonInterface::class, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Morrislaptop\SymfonyCustomNormalizers; | ||
|
||
use Illuminate\Contracts\Database\ModelIdentifier; | ||
use Illuminate\Contracts\Queue\QueueableCollection; | ||
use Illuminate\Contracts\Queue\QueueableEntity; | ||
use Illuminate\Queue\SerializesAndRestoresModelIdentifiers; | ||
use InvalidArgumentException; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
class ModelIdentifierNormalizer implements NormalizerInterface, DenormalizerInterface | ||
{ | ||
use SerializesAndRestoresModelIdentifiers; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function normalize($object, string $format = null, array $context = []) | ||
{ | ||
if (! $this->supportsNormalization($object)) { | ||
throw new InvalidArgumentException('Cannot serialize an object that is not a QueueableEntity or QueueableCollection in ModelIdentifierNormalizer.'); | ||
} | ||
|
||
return $this->getSerializedPropertyValue($object); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function supportsNormalization($data, string $format = null) | ||
{ | ||
return ($data instanceof QueueableEntity || $data instanceof QueueableCollection); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function denormalize($data, $type, string $format = null, array $context = []) | ||
{ | ||
$identifier = $data instanceof ModelIdentifier | ||
? $data | ||
: new ModelIdentifier($data['class'], $data['id'], $data['relations'], $data['connection']); | ||
|
||
return $this->getRestoredPropertyValue($identifier); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function supportsDenormalization($data, $type, string $format = null) | ||
{ | ||
return $this->normalizedDataIsModelIdentifier($data) | ||
&& $this->isNormalizedToModelIdentifier($type); | ||
} | ||
|
||
protected function normalizedDataIsModelIdentifier($data): bool | ||
{ | ||
return $data instanceof ModelIdentifier | ||
|| isset($data['class'], $data['id'], $data['relations'], $data['connection']); | ||
} | ||
|
||
protected function isNormalizedToModelIdentifier($class): bool | ||
{ | ||
return is_a($class, QueueableEntity::class, true) | ||
|| is_a($class, QueueableCollection::class, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Morrislaptop\SymfonyCustomNormalizers; | ||
|
||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; | ||
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; | ||
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; | ||
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface; | ||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; | ||
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; | ||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer as SymfonyObjectNormalizer; | ||
|
||
class ObjectWithDocblocksNormalizer extends SymfonyObjectNormalizer | ||
{ | ||
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = []) | ||
{ | ||
parent::__construct( | ||
$classMetadataFactory, | ||
$nameConverter, | ||
$propertyAccessor, | ||
$propertyTypeExtractor ?? new PhpDocExtractor(), | ||
$classDiscriminatorResolver, | ||
$objectClassResolver, | ||
$defaultContext | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters