diff --git a/generator/SerializerGenerator.php b/generator/SerializerGenerator.php index fbf2bee..d9d83e4 100644 --- a/generator/SerializerGenerator.php +++ b/generator/SerializerGenerator.php @@ -123,6 +123,7 @@ protected function decorateClass(ClassType $class): void $getSupportedTypes = $class->addMethod('getSupportedTypes') ->addComment("{@inheritDoc}\n") ->setVisibility('public') + ->setReturnType('array') ->setBody("return [\n{$supportedTypes}\n];"); $getSupportedTypes->addParameter('format')->setType('string')->setNullable(true); diff --git a/src/LiquetsoftFiasBundleServiceProvider.php b/src/LiquetsoftFiasBundleServiceProvider.php index 53c0390..d882edc 100644 --- a/src/LiquetsoftFiasBundleServiceProvider.php +++ b/src/LiquetsoftFiasBundleServiceProvider.php @@ -486,7 +486,7 @@ private function getOptionBool(string $name): bool /** * Возвращает значение опции по ее названию. */ - private function getOptionByName(string $name) + private function getOptionByName(string $name): mixed { return config($this->prefixString($name)); } diff --git a/src/Serializer/CompiledEntitesDenormalizer.php b/src/Serializer/CompiledEntitesDenormalizer.php index 985dbc0..6d784f2 100644 --- a/src/Serializer/CompiledEntitesDenormalizer.php +++ b/src/Serializer/CompiledEntitesDenormalizer.php @@ -172,7 +172,7 @@ public function denormalize($data, string $type, ?string $format = null, array $ /** * {@inheritDoc} */ - public function getSupportedTypes(?string $format) + public function getSupportedTypes(?string $format): array { return [ Apartments::class => true, diff --git a/src/Serializer/EloquentDenormalizer.php b/src/Serializer/EloquentDenormalizer.php deleted file mode 100644 index 097d93d..0000000 --- a/src/Serializer/EloquentDenormalizer.php +++ /dev/null @@ -1,146 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer; - -use Illuminate\Database\Eloquent\Model; -use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\TypeCaster\EloquentTypeCaster; -use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\TypeCaster\TypeCaster; -use Symfony\Component\Serializer\Exception\InvalidArgumentException; -use Symfony\Component\Serializer\Exception\NotNormalizableValueException; -use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; - -/** - * Нормализатор для объектов eloquent. - */ -class EloquentDenormalizer implements DenormalizerInterface -{ - /** - * @var TypeCaster|null - */ - protected $typeCaster; - - public function __construct(?TypeCaster $typeCaster = null) - { - if ($typeCaster === null) { - $typeCaster = new EloquentTypeCaster(); - } - - $this->typeCaster = $typeCaster; - } - - /** - * {@inheritdoc} - * - * @throws NotNormalizableValueException - * - * @psalm-suppress InvalidStringClass - */ - public function denormalize($data, $type, $format = null, array $context = []) - { - if (!\is_array($data)) { - throw new InvalidArgumentException('Data for denormalization must be an array instance.'); - } - - $entity = !empty($context['object_to_populate']) ? $context['object_to_populate'] : new $type(); - - if (!($entity instanceof Model)) { - throw new InvalidArgumentException( - "Bad class for populating entity, need '" . Model::class . "' instance.'" - ); - } - - try { - $dataArray = $this->createDataArrayForModel($data, $entity); - $entity->fill($dataArray); - } catch (\Throwable $e) { - throw new NotNormalizableValueException( - "Can't denormalize data to eloquent model.", - 0, - $e - ); - } - - return $entity; - } - - /** - * {@inheritdoc} - */ - public function supportsDenormalization($data, $type, $format = null) - { - return is_subclass_of($type, Model::class); - } - - /** - * Создает массив данных для вставки в модель на основании полей модели. - * - * @param mixed[] $data - * - * @throws \Exception - */ - protected function createDataArrayForModel(array $data, Model $entity): array - { - $dataArray = []; - - foreach ($data as $propertyName => $propertyValue) { - $modelAttribute = $this->mapParameterNameToModelAttributeName((string) $propertyName, $entity); - if ($modelAttribute === null) { - continue; - } - $modelValue = $this->castValueForModel($propertyValue, $modelAttribute, $entity); - $dataArray[$modelAttribute] = $modelValue; - } - - return $dataArray; - } - - /** - * Пробует преобразовать имя параметра так, чтобы получить соответствие из модели. - */ - protected function mapParameterNameToModelAttributeName(string $name, Model $entity): ?string - { - $mappedName = null; - - if (strpos($name, '@') === 0) { - $name = substr($name, 1); - } - - $nameVariants = [ - strtolower($name), - str_replace('_', '', strtolower($name)), - strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $name)), - ]; - - /** @var string[] */ - $fields = $entity->getFillable(); - foreach ($fields as $field) { - $loweredField = strtolower($field); - if (\in_array($loweredField, $nameVariants)) { - $mappedName = $field; - break; - } - } - - return $mappedName; - } - - /** - * Преобразует значение атрибута к тому типу, который указан в модели. - * - * @throws \Exception - */ - protected function castValueForModel($value, string $attributeName, Model $entity) - { - /** @var array<string, string> */ - $casts = $entity->getCasts(); - $type = $casts[$attributeName] ?? ''; - - if ($value !== null && $this->typeCaster && $this->typeCaster->canCast($type, $value)) { - $value = $this->typeCaster->cast($type, $value); - } - - return $value; - } -} diff --git a/src/Serializer/FiasSerializer.php b/src/Serializer/FiasSerializer.php index e7e1adf..fcb122c 100644 --- a/src/Serializer/FiasSerializer.php +++ b/src/Serializer/FiasSerializer.php @@ -28,7 +28,6 @@ public function __construct(?array $normalizers = null, ?array $encoders = null) if ($normalizers === null) { $normalizers = [ new CompiledEntitesDenormalizer(), - new EloquentDenormalizer(), new ObjectNormalizer( null, new FiasNameConverter(), @@ -36,7 +35,9 @@ public function __construct(?array $normalizers = null, ?array $encoders = null) new ReflectionExtractor(), null, null, - [ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true] + [ + ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true, + ] ), ]; } diff --git a/src/Serializer/TypeCaster/BoolCaster.php b/src/Serializer/TypeCaster/BoolCaster.php deleted file mode 100644 index a66ec9e..0000000 --- a/src/Serializer/TypeCaster/BoolCaster.php +++ /dev/null @@ -1,27 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\TypeCaster; - -/** - * Преобразует данные в bool. - */ -class BoolCaster implements TypeCaster -{ - /** - * {@inheritDoc} - */ - public function canCast(string $type, $value): bool - { - return $type === 'bool' || $type === 'boolean'; - } - - /** - * {@inheritDoc} - */ - public function cast(string $type, $value) - { - return (bool) $value; - } -} diff --git a/src/Serializer/TypeCaster/CompositeTypeCaster.php b/src/Serializer/TypeCaster/CompositeTypeCaster.php deleted file mode 100644 index 278fdfa..0000000 --- a/src/Serializer/TypeCaster/CompositeTypeCaster.php +++ /dev/null @@ -1,58 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\TypeCaster; - -/** - * Кастер, который выбирает подходящий кастер из внутреннего набора кастеров. - */ -class CompositeTypeCaster implements TypeCaster -{ - /** - * @var TypeCaster[] - */ - protected $casters = []; - - /** - * @param TypeCaster[] $casters - */ - public function __construct(array $casters = []) - { - $this->casters = $casters; - } - - /** - * {@inheritDoc} - */ - public function canCast(string $type, $value): bool - { - $canCast = false; - - foreach ($this->casters as $caster) { - if ($caster->canCast($type, $value)) { - $canCast = true; - break; - } - } - - return $canCast; - } - - /** - * {@inheritDoc} - */ - public function cast(string $type, $value) - { - $castedValue = $value; - - foreach ($this->casters as $caster) { - if ($caster->canCast($type, $value)) { - $castedValue = $caster->cast($type, $value); - break; - } - } - - return $castedValue; - } -} diff --git a/src/Serializer/TypeCaster/DateCaster.php b/src/Serializer/TypeCaster/DateCaster.php deleted file mode 100644 index 04c69c1..0000000 --- a/src/Serializer/TypeCaster/DateCaster.php +++ /dev/null @@ -1,29 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\TypeCaster; - -/** - * Преобразует данные в дату. - */ -class DateCaster implements TypeCaster -{ - /** - * {@inheritDoc} - */ - public function canCast(string $type, $value): bool - { - return strpos($type, 'date') === 0; - } - - /** - * {@inheritDoc} - * - * @throws \Exception - */ - public function cast(string $type, $value) - { - return new \DateTimeImmutable((string) $value); - } -} diff --git a/src/Serializer/TypeCaster/EloquentTypeCaster.php b/src/Serializer/TypeCaster/EloquentTypeCaster.php deleted file mode 100644 index 86150c7..0000000 --- a/src/Serializer/TypeCaster/EloquentTypeCaster.php +++ /dev/null @@ -1,30 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\TypeCaster; - -/** - * Кастер для преобразования типов в eloquent модели. - */ -class EloquentTypeCaster extends CompositeTypeCaster -{ - /** - * @param TypeCaster[]|null $casters - */ - public function __construct(?array $casters = null) - { - if ($casters === null) { - $casters = [ - new BoolCaster(), - new DateCaster(), - new FloatCaster(), - new IntCaster(), - new StringCaster(), - new TimestampCaster(), - ]; - } - - parent::__construct($casters); - } -} diff --git a/src/Serializer/TypeCaster/FloatCaster.php b/src/Serializer/TypeCaster/FloatCaster.php deleted file mode 100644 index fce69f7..0000000 --- a/src/Serializer/TypeCaster/FloatCaster.php +++ /dev/null @@ -1,27 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\TypeCaster; - -/** - * Преобразует данные в float. - */ -class FloatCaster implements TypeCaster -{ - /** - * {@inheritDoc} - */ - public function canCast(string $type, $value): bool - { - return $type === 'real' || $type === 'float' || $type === 'double' || strpos($type, 'decimal') === 0; - } - - /** - * {@inheritDoc} - */ - public function cast(string $type, $value) - { - return (float) $value; - } -} diff --git a/src/Serializer/TypeCaster/IntCaster.php b/src/Serializer/TypeCaster/IntCaster.php deleted file mode 100644 index 97d3800..0000000 --- a/src/Serializer/TypeCaster/IntCaster.php +++ /dev/null @@ -1,27 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\TypeCaster; - -/** - * Преобразует данные в int. - */ -class IntCaster implements TypeCaster -{ - /** - * {@inheritDoc} - */ - public function canCast(string $type, $value): bool - { - return $type === 'int' || $type === 'integer'; - } - - /** - * {@inheritDoc} - */ - public function cast(string $type, $value) - { - return (int) $value; - } -} diff --git a/src/Serializer/TypeCaster/StringCaster.php b/src/Serializer/TypeCaster/StringCaster.php deleted file mode 100644 index e3d9aca..0000000 --- a/src/Serializer/TypeCaster/StringCaster.php +++ /dev/null @@ -1,27 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\TypeCaster; - -/** - * Преобразует данные в string. - */ -class StringCaster implements TypeCaster -{ - /** - * {@inheritDoc} - */ - public function canCast(string $type, $value): bool - { - return $type === 'string' || $type === 'str'; - } - - /** - * {@inheritDoc} - */ - public function cast(string $type, $value) - { - return (string) $value; - } -} diff --git a/src/Serializer/TypeCaster/TimestampCaster.php b/src/Serializer/TypeCaster/TimestampCaster.php deleted file mode 100644 index 200d6ce..0000000 --- a/src/Serializer/TypeCaster/TimestampCaster.php +++ /dev/null @@ -1,29 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\TypeCaster; - -/** - * Преобразует данные в timestamp. - */ -class TimestampCaster implements TypeCaster -{ - /** - * {@inheritDoc} - */ - public function canCast(string $type, $value): bool - { - return strpos($type, 'timestamp') === 0; - } - - /** - * {@inheritDoc} - * - * @throws \Exception - */ - public function cast(string $type, $value) - { - return is_numeric($value) ? (int) $value : (new \DateTimeImmutable((string) $value))->getTimestamp(); - } -} diff --git a/src/Serializer/TypeCaster/TypeCaster.php b/src/Serializer/TypeCaster/TypeCaster.php deleted file mode 100644 index 60e18ca..0000000 --- a/src/Serializer/TypeCaster/TypeCaster.php +++ /dev/null @@ -1,21 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\TypeCaster; - -/** - * Интерфейс для объекта, который приводит значение к определенному типу. - */ -interface TypeCaster -{ - /** - * Проверяет подходит ли данный кастер к указанному типу данных. - */ - public function canCast(string $type, $value): bool; - - /** - * Приводит значение к указанному типу. - */ - public function cast(string $type, $value); -} diff --git a/tests/BaseCase.php b/tests/BaseCase.php index 033d793..beed58e 100644 --- a/tests/BaseCase.php +++ b/tests/BaseCase.php @@ -17,20 +17,11 @@ */ abstract class BaseCase extends TestCase { - /** - * @var Generator|null - */ - private $faker; + private ?Generator $faker; - /** - * @var FileSystemHelperInterface|null - */ - private $fs; + private ?FileSystemHelperInterface $fs; - /** - * @var string|null - */ - private $tempDir; + private ?string $tempDir; /** * Возвращает объект php faker для генерации случайных данных. @@ -125,7 +116,7 @@ protected function getPathToTestFile(string $name = '', ?string $content = null) */ protected function tearDown(): void { - if ($this->tempDir) { + if ($this->tempDir !== null) { $this->fs()->remove($this->tempDir); } diff --git a/tests/MockModel/EloquentStorageTestModel.php b/tests/MockModel/EloquentStorageTestModel.php index fab4177..d384ef4 100644 --- a/tests/MockModel/EloquentStorageTestModel.php +++ b/tests/MockModel/EloquentStorageTestModel.php @@ -23,7 +23,7 @@ class EloquentStorageTestModel extends Model /** @var string */ protected $primaryKey = 'id'; - /** @var string[] */ + /** @var array<int, string> */ protected $fillable = [ 'id', 'name', diff --git a/tests/MockModel/FiasSerializerMock.php b/tests/MockModel/FiasSerializerMock.php index 1849fd3..f2ae6c2 100644 --- a/tests/MockModel/FiasSerializerMock.php +++ b/tests/MockModel/FiasSerializerMock.php @@ -16,9 +16,7 @@ class FiasSerializerMock extends Model */ protected $dateFormat = 'Y-m-d H:i'; - /** - * @var array<array-key, string> - */ + /** @var array<int, string> */ protected $fillable = [ 'ACTSTATID', 'name', diff --git a/tests/MockModel/PipelineTestMockModel.php b/tests/MockModel/PipelineTestMockModel.php index d4dce0c..8c4f630 100644 --- a/tests/MockModel/PipelineTestMockModel.php +++ b/tests/MockModel/PipelineTestMockModel.php @@ -23,7 +23,7 @@ class PipelineTestMockModel extends Model /** @var string */ protected $primaryKey = 'testId'; - /** @var string[] */ + /** @var array<int, string> */ protected $fillable = [ 'testId', 'testName', diff --git a/tests/VersionManager/EloquentVersionManagerTest.php b/tests/VersionManager/EloquentVersionManagerTest.php index b8bfc28..c708c98 100644 --- a/tests/VersionManager/EloquentVersionManagerTest.php +++ b/tests/VersionManager/EloquentVersionManagerTest.php @@ -86,6 +86,7 @@ public function testGetCurrentVersion(): void $versionManager->setCurrentVersion($info); $versionResponse = $versionManager->getCurrentVersion(); + $this->assertNotNull($versionResponse); $this->assertSame($version, $versionResponse->getVersion()); $this->assertSame($fullUrl, $versionResponse->getFullUrl()); $this->assertSame($deltaUrl, $versionResponse->getDeltaUrl());