Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ e85fae4f7290329ac9cfae159e19c1bb55062e4b
c400a3c655b1157b8ff30f5c2ead30563fad972a
13d4e52a568b9263cefd6830a1759c7c2a2f0ea3
9ceb35f77a0bfefe8bf4b51d5026399f12ad79b0
8614c245e767ac74c2ca65697553b70bf54394ad
e7d3e86c6e045f09131ec7cdb3e2cf0887e7eb21
11 changes: 10 additions & 1 deletion src/Objects/Once.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
<?php

/*
* Copyright (c) 2025. Encore Digital Group.
* All Rights Reserved.
*/

namespace EncoreDigitalGroup\StdLib\Objects;

use EncoreDigitalGroup\StdLib\Exceptions\MissingMinimumDependencyException;
use EncoreDigitalGroup\StdLib\Support\Internal\Composer\Composer;
use Illuminate\Support\Once as BaseOnce;

/** @codeCoverageIgnore Ignored since this is a wrapper around an Illuminate class */
/**
* @codeCoverageIgnore Ignored since this is a wrapper around an Illuminate class
*
* @deprecated use StaticCache instead.
*/
class Once
{
public static function handle(callable $callable): mixed
Expand Down
1 change: 1 addition & 0 deletions src/Objects/Serializers/Attributes/MapInputName.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2025. Encore Digital Group.
* All Rights Reserved.
Expand Down
6 changes: 3 additions & 3 deletions src/Objects/Serializers/Attributes/MapName.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2025. Encore Digital Group.
* All Rights Reserved.
Expand All @@ -17,10 +18,9 @@
class MapName
{
public function __construct(
public string|IPropertyMapper $input,
public string|IPropertyMapper $input,
public string|IPropertyMapper|null $output = null
)
{
) {
/** @phpstan-ignore-next-line False positive from doc block */
if (is_null($this->output)) {
$this->output = $input;
Expand Down
1 change: 1 addition & 0 deletions src/Objects/Serializers/Attributes/MapOutputName.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2025. Encore Digital Group.
* All Rights Reserved.
Expand Down
69 changes: 36 additions & 33 deletions src/Objects/Serializers/JsonSerializer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2025. Encore Digital Group.
* All Rights Reserved.
Expand All @@ -12,26 +13,17 @@
use EncoreDigitalGroup\StdLib\Objects\Serializers\Mappers\IPropertyMapper;
use Illuminate\Support\Collection;
use ReflectionClass;
use ReflectionParameter;
use ReflectionProperty;
use RuntimeException;
use Symfony\Component\Serializer\Encoder\JsonEncoder;

class JsonSerializer extends AbstractSerializer
{
private const string MAPPER_DIRECTION_INPUT = "input";
private const string MAPPER_DIRECTION_OUTPUT = "output";

protected static Collection $normalizers;

protected static function format(): string
{
return "json";
}

protected static function encoders(): array
{
return [(new JsonEncoder)];
}
private const string MAPPER_DIRECTION_INPUT = "input";
private const string MAPPER_DIRECTION_OUTPUT = "output";

public static function serialize(object $object): string
{
Expand All @@ -52,23 +44,33 @@ public static function deserialize(string $class, string $data): mixed
return parent::deserialize($class, $data);
}

protected static function format(): string
{
return "json";
}

protected static function encoders(): array
{
return [(new JsonEncoder)];
}

/**
* @param class-string|object $classOrObject
* @param class-string|object $classOrObject
*/
private static function hasMapNameAttributes(string|object $classOrObject): bool
{
$reflection = new ReflectionClass($classOrObject);

if (!empty($reflection->getAttributes(MapName::class)) ||
!empty($reflection->getAttributes(MapInputName::class)) ||
!empty($reflection->getAttributes(MapOutputName::class))) {
if ($reflection->getAttributes(MapName::class) !== [] ||
$reflection->getAttributes(MapInputName::class) !== [] ||
$reflection->getAttributes(MapOutputName::class) !== []) {
return true;
}

foreach ($reflection->getProperties() as $property) {
if (!empty($property->getAttributes(MapName::class)) ||
!empty($property->getAttributes(MapInputName::class)) ||
!empty($property->getAttributes(MapOutputName::class))) {
foreach ($reflection->getProperties() as $reflectionProperty) {
if (!empty($reflectionProperty->getAttributes(MapName::class)) ||
!empty($reflectionProperty->getAttributes(MapInputName::class)) ||
!empty($reflectionProperty->getAttributes(MapOutputName::class))) {
return true;
}
}
Expand All @@ -81,25 +83,25 @@ private static function serializeWithMapName(object $object): string
$reflection = new ReflectionClass($object);
$data = [];

foreach ($reflection->getProperties() as $property) {
$property->setAccessible(true);
$propertyName = $property->getName();
$value = $property->getValue($object);
foreach ($reflection->getProperties() as $reflectionProperty) {
$reflectionProperty->setAccessible(true);
$propertyName = $reflectionProperty->getName();
$value = $reflectionProperty->getValue($object);

$mappedName = self::getMappedOutputName($property, $reflection) ?? $propertyName;
$mappedName = self::getMappedOutputName($reflectionProperty, $reflection) ?? $propertyName;
$data[$mappedName] = $value;
}

$json = json_encode($data);
if ($json === false) {
throw new RuntimeException('Failed to encode JSON');
throw new RuntimeException("Failed to encode JSON");
}

return $json;
}

/**
* @param class-string $class
* @param class-string $class
*/
private static function deserializeWithMapName(string $class, string $jsonData): mixed
{
Expand All @@ -112,14 +114,14 @@ private static function deserializeWithMapName(string $class, string $jsonData):
}

$args = [];
foreach ($constructor->getParameters() as $parameter) {
$args[] = self::resolveParameterValue($parameter, $reflection, $data);
foreach ($constructor->getParameters() as $reflectionParameter) {
$args[] = self::resolveParameterValue($reflectionParameter, $reflection, $data);
}

return $reflection->newInstanceArgs($args);
}

private static function resolveParameterValue(\ReflectionParameter $parameter, ReflectionClass $reflection, array $data): mixed
private static function resolveParameterValue(ReflectionParameter $parameter, ReflectionClass $reflection, array $data): mixed
{
$parameterName = $parameter->getName();
$property = $reflection->hasProperty($parameterName) ? $reflection->getProperty($parameterName) : null;
Expand Down Expand Up @@ -154,11 +156,12 @@ private static function getMapperResult(string|IPropertyMapper $mapper, string $
{
if (is_string($mapper)) {
if (class_exists($mapper)) {
$mapperInstance = new $mapper();
$mapperInstance = new $mapper;
if ($mapperInstance instanceof IPropertyMapper) {
return $mapperInstance->map($propertyName);
}
}

return $mapper;
}

Expand All @@ -170,12 +173,12 @@ private static function getMapperForDirection(ReflectionProperty|ReflectionClass
$specificAttributeClass = $direction === self::MAPPER_DIRECTION_INPUT ? MapInputName::class : MapOutputName::class;

$specificAttributes = $reflector->getAttributes($specificAttributeClass);
if (!empty($specificAttributes)) {
if ($specificAttributes !== []) {
return self::getMapperResult($specificAttributes[0]->newInstance()->mapper, $propertyName);
}

$mapNameAttributes = $reflector->getAttributes(MapName::class);
if (!empty($mapNameAttributes)) {
if ($mapNameAttributes !== []) {
$mapNameInstance = $mapNameAttributes[0]->newInstance();
$mapper = $direction === self::MAPPER_DIRECTION_OUTPUT
? ($mapNameInstance->output ?? $mapNameInstance->input)
Expand Down
1 change: 1 addition & 0 deletions src/Objects/Serializers/Mappers/CamelCaseMapper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2025. Encore Digital Group.
* All Rights Reserved.
Expand Down
1 change: 1 addition & 0 deletions src/Objects/Serializers/Mappers/IPropertyMapper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2025. Encore Digital Group.
* All Rights Reserved.
Expand Down
1 change: 1 addition & 0 deletions src/Objects/Serializers/Mappers/PascalCaseMapper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2025. Encore Digital Group.
* All Rights Reserved.
Expand Down
1 change: 1 addition & 0 deletions src/Objects/Serializers/Mappers/ProvidedNameMapper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2025. Encore Digital Group.
* All Rights Reserved.
Expand Down
1 change: 1 addition & 0 deletions src/Objects/Serializers/Mappers/SnakeCaseMapper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2025. Encore Digital Group.
* All Rights Reserved.
Expand Down