Skip to content

Commit

Permalink
Refactoring (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Sep 27, 2023
1 parent bfbe2b9 commit f7d854d
Show file tree
Hide file tree
Showing 50 changed files with 207 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Yiisoft\Hydrator;
namespace Yiisoft\Hydrator\Attribute\Data;

/**
* An interface for data attributes (allowed only in classes). Can be used to change the way data to be assigned is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

declare(strict_types=1);

namespace Yiisoft\Hydrator;
namespace Yiisoft\Hydrator\Attribute\Data;

use Yiisoft\Hydrator\Data;

/**
* An interface for resolvers of attributes that implement {@see DataAttributeInterface}.
Expand Down
4 changes: 1 addition & 3 deletions src/Attribute/Data/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

use Attribute;
use Yiisoft\Hydrator\Data;
use Yiisoft\Hydrator\DataAttributeInterface;
use Yiisoft\Hydrator\DataAttributeResolverInterface;
use Yiisoft\Hydrator\HydratorInterface;
use Yiisoft\Hydrator\UnexpectedAttributeException;
use Yiisoft\Hydrator\AttributeHandling\UnexpectedAttributeException;

/**
* Override mapping of object property names to keys in the data array in hydrator.
Expand Down
8 changes: 3 additions & 5 deletions src/Attribute/Parameter/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
namespace Yiisoft\Hydrator\Attribute\Parameter;

use Attribute;
use Yiisoft\Hydrator\Context;
use Yiisoft\Hydrator\ParameterAttributeInterface;
use Yiisoft\Hydrator\ParameterAttributeResolverInterface;
use Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext;
use Yiisoft\Hydrator\Result;
use Yiisoft\Hydrator\UnexpectedAttributeException;
use Yiisoft\Hydrator\AttributeHandling\UnexpectedAttributeException;

/**
* Resolve value from the data array used for object hydration by key specified.
Expand All @@ -25,7 +23,7 @@ public function __construct(
) {
}

public function getParameterValue(ParameterAttributeInterface $attribute, Context $context): Result
public function getParameterValue(ParameterAttributeInterface $attribute, ParameterAttributeResolveContext $context): Result
{
if (!$attribute instanceof self) {
throw new UnexpectedAttributeException(self::class, $attribute);
Expand Down
1 change: 0 additions & 1 deletion src/Attribute/Parameter/Di.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Yiisoft\Hydrator\Attribute\Parameter;

use Attribute;
use Yiisoft\Hydrator\ParameterAttributeInterface;

/**
* Resolve value as instance obtained from container by the specified ID or autoresolvied ID by PHP type.
Expand Down
8 changes: 3 additions & 5 deletions src/Attribute/Parameter/DiResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
use Psr\Container\NotFoundExceptionInterface;
use ReflectionNamedType;
use ReflectionUnionType;
use Yiisoft\Hydrator\Context;
use Yiisoft\Hydrator\ParameterAttributeInterface;
use Yiisoft\Hydrator\ParameterAttributeResolverInterface;
use Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext;
use Yiisoft\Hydrator\Result;
use Yiisoft\Hydrator\UnexpectedAttributeException;
use Yiisoft\Hydrator\AttributeHandling\UnexpectedAttributeException;

/**
* Resolver for {@see Di} attribute. Obtains dependency from container by ID specified or autoresolved ID by PHP type.
Expand All @@ -32,7 +30,7 @@ public function __construct(
* @throws ContainerExceptionInterface
* @throws DiNotFoundException When object not found in container or object ID autoresolving is fail.
*/
public function getParameterValue(ParameterAttributeInterface $attribute, Context $context): Result
public function getParameterValue(ParameterAttributeInterface $attribute, ParameterAttributeResolveContext $context): Result
{
if (!$attribute instanceof Di) {
throw new UnexpectedAttributeException(Di::class, $attribute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Yiisoft\Hydrator;
namespace Yiisoft\Hydrator\Attribute\Parameter;

/**
* An interface for parameters' attributes (allowed only in class properties and constructor parameters). Can be used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

declare(strict_types=1);

namespace Yiisoft\Hydrator;
namespace Yiisoft\Hydrator\Attribute\Parameter;

use Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext;
use Yiisoft\Hydrator\Result;

/**
* An interface for resolvers of attributes that implement {@see ParameterAttributeInterface}.
Expand All @@ -13,9 +16,12 @@ interface ParameterAttributeResolverInterface
* Returns the resolved from specified attribute value object.
*
* @param ParameterAttributeInterface $attribute The attribute to be resolved.
* @param Context $context The context of value resolving from attribute.
* @param ParameterAttributeResolveContext $context The context of value resolving from attribute.
*
* @return Result The resolved from specified attribute value object.
*/
public function getParameterValue(ParameterAttributeInterface $attribute, Context $context): Result;
public function getParameterValue(
ParameterAttributeInterface $attribute,
ParameterAttributeResolveContext $context
): Result;
}
8 changes: 3 additions & 5 deletions src/Attribute/Parameter/ToString.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@

use Attribute;
use Stringable;
use Yiisoft\Hydrator\Context;
use Yiisoft\Hydrator\ParameterAttributeInterface;
use Yiisoft\Hydrator\ParameterAttributeResolverInterface;
use Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext;
use Yiisoft\Hydrator\Result;
use Yiisoft\Hydrator\UnexpectedAttributeException;
use Yiisoft\Hydrator\AttributeHandling\UnexpectedAttributeException;

/**
* Converts the resolved value to string. Non-resolved values is skip.
Expand All @@ -23,7 +21,7 @@ public function getResolver(): self
return $this;
}

public function getParameterValue(ParameterAttributeInterface $attribute, Context $context): Result
public function getParameterValue(ParameterAttributeInterface $attribute, ParameterAttributeResolveContext $context): Result
{
if (!$attribute instanceof self) {
throw new UnexpectedAttributeException(self::class, $attribute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

declare(strict_types=1);

namespace Yiisoft\Hydrator;
namespace Yiisoft\Hydrator\AttributeHandling;

use ReflectionAttribute;
use ReflectionClass;
use RuntimeException;
use Yiisoft\Hydrator\Exception\NonInstantiableException;
use Yiisoft\Hydrator\ResolverFactory\AttributeResolverFactoryInterface;
use Yiisoft\Hydrator\AttributeHandling\ResolverFactory\AttributeResolverFactoryInterface;
use Yiisoft\Hydrator\Attribute\Data\DataAttributeInterface;
use Yiisoft\Hydrator\Attribute\Data\DataAttributeResolverInterface;
use Yiisoft\Hydrator\Data;
use Yiisoft\Hydrator\NonInstantiableException;

/**
* Handles data attributes that implement {@see DataAttributeInterface}.
Expand All @@ -24,15 +28,20 @@ public function __construct(
/**
* Handle data attributes.
*
* @param ReflectionAttribute[] $reflectionAttributes Reflections of attributes to handle.
* @param ReflectionClass $reflectionClass Reflection of class to attributes handle.
* @param Data $data Current {@see Data} object.
*
* @throws NonInstantiableException
*
* @psalm-param ReflectionAttribute<DataAttributeInterface>[] $reflectionAttributes
*/
public function handle(array $reflectionAttributes, Data $data): void
public function handle(ReflectionClass $reflectionClass, Data $data): void
{
$reflectionAttributes = $reflectionClass->getAttributes(
DataAttributeInterface::class,
ReflectionAttribute::IS_INSTANCEOF
);

foreach ($reflectionAttributes as $reflectionAttribute) {
$attribute = $reflectionAttribute->newInstance();

Expand Down
10 changes: 7 additions & 3 deletions src/Context.php → ...ling/ParameterAttributeResolveContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

declare(strict_types=1);

namespace Yiisoft\Hydrator;
namespace Yiisoft\Hydrator\AttributeHandling;

use ReflectionParameter;
use ReflectionProperty;
use Yiisoft\Hydrator\HydratorInterface;
use Yiisoft\Hydrator\Internal\DataExtractor;

use Yiisoft\Hydrator\Result;

use function is_string;

Expand All @@ -14,7 +18,7 @@
*
* @psalm-import-type MapType from HydratorInterface
*/
final class Context
final class ParameterAttributeResolveContext
{
/**
* @param ReflectionParameter|ReflectionProperty $parameter Resolved parameter or property reflection.
Expand Down Expand Up @@ -86,6 +90,6 @@ public function getData(array|string|null $key = null): Result
$path = $this->map[$path] ?? $key;
}

return DataHelper::getValueByPath($this->data, $path);
return DataExtractor::getValueByPath($this->data, $path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

declare(strict_types=1);

namespace Yiisoft\Hydrator;
namespace Yiisoft\Hydrator\AttributeHandling;

use ReflectionAttribute;
use ReflectionParameter;
use ReflectionProperty;
use RuntimeException;
use Yiisoft\Hydrator\Exception\NonInstantiableException;
use Yiisoft\Hydrator\ResolverFactory\AttributeResolverFactoryInterface;
use Yiisoft\Hydrator\AttributeHandling\ResolverFactory\AttributeResolverFactoryInterface;
use Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeInterface;
use Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeResolverInterface;
use Yiisoft\Hydrator\Data;
use Yiisoft\Hydrator\NonInstantiableException;
use Yiisoft\Hydrator\Result;

/**
* Handles parameters attributes that implement {@see ParameterAttributeInterface}.
Expand All @@ -26,10 +30,10 @@ public function __construct(
*
* @param ReflectionParameter|ReflectionProperty $parameter Parameter or property reflection to handle attributes
* from.
* @param Result|null $resolveResult The resolved value object to pass to attribute resolver via {@see Context}.
* @param Data|null $data Raw data and map to pass to attribute resolver via {@see Context}.
* @param Result|null $resolveResult The resolved value object to pass to attribute resolver via {@see ParameterAttributeResolveContext}.
* @param Data|null $data Raw data and map to pass to attribute resolver via {@see ParameterAttributeResolveContext}.
*
* @throws NonInstantiableException
*@throws NonInstantiableException
* @return Result The resolved from attributes value object.
*/
public function handle(
Expand Down Expand Up @@ -57,7 +61,7 @@ public function handle(
);
}

$context = new Context(
$context = new ParameterAttributeResolveContext(
$parameter,
$hereResolveResult->isResolved() ? $hereResolveResult : $resolveResult,
$data?->getData() ?? [],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Hydrator\AttributeHandling\ResolverFactory;

use Yiisoft\Hydrator\Attribute\Data\DataAttributeInterface;
use Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeInterface;
use Yiisoft\Hydrator\NonInstantiableException;

interface AttributeResolverFactoryInterface
{
/**
* @throws NonInstantiableException
*/
public function create(DataAttributeInterface|ParameterAttributeInterface $attribute): object;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

declare(strict_types=1);

namespace Yiisoft\Hydrator\ResolverFactory;
namespace Yiisoft\Hydrator\AttributeHandling\ResolverFactory;

use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use RuntimeException;
use Yiisoft\Hydrator\DataAttributeInterface;
use Yiisoft\Hydrator\Exception\NonInstantiableException;
use Yiisoft\Hydrator\ParameterAttributeInterface;
use Yiisoft\Hydrator\Attribute\Data\DataAttributeInterface;
use Yiisoft\Hydrator\NonInstantiableException;
use Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeInterface;

use function is_object;
use function is_string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

declare(strict_types=1);

namespace Yiisoft\Hydrator\ResolverFactory;
namespace Yiisoft\Hydrator\AttributeHandling\ResolverFactory;

use ReflectionClass;
use ReflectionException;
use Yiisoft\Hydrator\DataAttributeInterface;
use Yiisoft\Hydrator\Exception\NonInstantiableException;
use Yiisoft\Hydrator\Attribute\Data\DataAttributeInterface;
use Yiisoft\Hydrator\NonInstantiableException;
use Yiisoft\Hydrator\ObjectFactory\ReflectionObjectFactory;
use Yiisoft\Hydrator\ParameterAttributeInterface;
use Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeInterface;

use function is_string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Yiisoft\Hydrator;
namespace Yiisoft\Hydrator\AttributeHandling;

use InvalidArgumentException;
use Throwable;
Expand Down
4 changes: 3 additions & 1 deletion src/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Hydrator;

use Yiisoft\Hydrator\Internal\DataExtractor;

use function array_key_exists;

/**
Expand Down Expand Up @@ -98,6 +100,6 @@ public function resolveValue(string $name): Result
return Result::fail();
}

return DataHelper::getValueByPath($this->getData(), $this->map[$name] ?? $name);
return DataExtractor::getValueByPath($this->getData(), $this->map[$name] ?? $name);
}
}

0 comments on commit f7d854d

Please sign in to comment.