Skip to content

Commit

Permalink
Rename Value to Result (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed May 27, 2023
1 parent 1c1bff5 commit bce6ee0
Show file tree
Hide file tree
Showing 20 changed files with 103 additions and 103 deletions.
4 changes: 2 additions & 2 deletions src/Attribute/Parameter/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Yiisoft\Hydrator\Context;
use Yiisoft\Hydrator\ParameterAttributeInterface;
use Yiisoft\Hydrator\ParameterAttributeResolverInterface;
use Yiisoft\Hydrator\Value;
use Yiisoft\Hydrator\Result;
use Yiisoft\Hydrator\UnexpectedAttributeException;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
Expand All @@ -22,7 +22,7 @@ public function __construct(
) {
}

public function getParameterValue(ParameterAttributeInterface $attribute, Context $context): Value
public function getParameterValue(ParameterAttributeInterface $attribute, Context $context): Result
{
if (!$attribute instanceof self) {
throw new UnexpectedAttributeException(self::class, $attribute);
Expand Down
10 changes: 5 additions & 5 deletions src/Attribute/Parameter/DiResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Yiisoft\Hydrator\Context;
use Yiisoft\Hydrator\ParameterAttributeInterface;
use Yiisoft\Hydrator\ParameterAttributeResolverInterface;
use Yiisoft\Hydrator\Value;
use Yiisoft\Hydrator\Result;
use Yiisoft\Hydrator\UnexpectedAttributeException;

final class DiResolver implements ParameterAttributeResolverInterface
Expand All @@ -26,7 +26,7 @@ public function __construct(
* @throws ContainerExceptionInterface
* @throws DiNotFoundException
*/
public function getParameterValue(ParameterAttributeInterface $attribute, Context $context): Value
public function getParameterValue(ParameterAttributeInterface $attribute, Context $context): Result
{
if (!$attribute instanceof Di) {
throw new UnexpectedAttributeException(Di::class, $attribute);
Expand All @@ -37,7 +37,7 @@ public function getParameterValue(ParameterAttributeInterface $attribute, Contex
$id = $attribute->getId();
if ($id !== null) {
try {
return Value::success(
return Result::success(
$this->container->get($id)
);
} catch (NotFoundExceptionInterface $e) {
Expand All @@ -49,7 +49,7 @@ public function getParameterValue(ParameterAttributeInterface $attribute, Contex
if ($type instanceof ReflectionNamedType) {
if (!$type->isBuiltin()) {
try {
return Value::success(
return Result::success(
$this->container->get($type->getName())
);
} catch (NotFoundExceptionInterface $e) {
Expand All @@ -61,7 +61,7 @@ public function getParameterValue(ParameterAttributeInterface $attribute, Contex
/** @psalm-suppress RedundantConditionGivenDocblockType Need for PHP less than 8.2 */
if ($type instanceof ReflectionNamedType && !$type->isBuiltin()) {
try {
return Value::success(
return Result::success(
$this->container->get($type->getName())
);
} catch (NotFoundExceptionInterface) {
Expand Down
10 changes: 5 additions & 5 deletions src/Attribute/Parameter/ToString.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Yiisoft\Hydrator\Context;
use Yiisoft\Hydrator\ParameterAttributeInterface;
use Yiisoft\Hydrator\ParameterAttributeResolverInterface;
use Yiisoft\Hydrator\Value;
use Yiisoft\Hydrator\Result;
use Yiisoft\Hydrator\UnexpectedAttributeException;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::IS_REPEATABLE)]
Expand All @@ -20,7 +20,7 @@ public function getResolver(): self
return $this;
}

public function getParameterValue(ParameterAttributeInterface $attribute, Context $context): Value
public function getParameterValue(ParameterAttributeInterface $attribute, Context $context): Result
{
if (!$attribute instanceof self) {
throw new UnexpectedAttributeException(self::class, $attribute);
Expand All @@ -29,12 +29,12 @@ public function getParameterValue(ParameterAttributeInterface $attribute, Contex
if ($context->isResolved()) {
$resolvedValue = $context->getResolvedValue();
if (is_scalar($resolvedValue) || null === $resolvedValue || $resolvedValue instanceof Stringable) {
return Value::success((string) $resolvedValue);
return Result::success((string) $resolvedValue);
}

return Value::success('');
return Result::success('');
}

return Value::fail();
return Result::fail();
}
}
10 changes: 5 additions & 5 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class Context
*/
public function __construct(
private ReflectionParameter|ReflectionProperty $parameter,
private Value $resolvedValue,
private Result $resolveResult,
private array $data,
private array $map,
) {
Expand All @@ -32,21 +32,21 @@ public function getParameter(): ReflectionParameter|ReflectionProperty

public function isResolved(): bool
{
return $this->resolvedValue->isResolved();
return $this->resolveResult->isResolved();
}

public function getResolvedValue(): mixed
{
return $this->resolvedValue->getValue();
return $this->resolveResult->getValue();
}

/**
* @param string|string[]|null $key
*/
public function getData(array|string|null $key = null): Value
public function getData(array|string|null $key = null): Result
{
if ($key === null) {
return Value::success($this->data);
return Result::success($this->data);
}

if (is_string($key)) {
Expand Down
10 changes: 5 additions & 5 deletions src/DataHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ final class DataHelper
/**
* @param string|string[] $path
*/
public static function getValueByPath(array $data, string|array $path): Value
public static function getValueByPath(array $data, string|array $path): Result
{
if (is_string($path)) {
$path = StringHelper::parsePath($path);
}

$result = Value::success($data);
$result = Result::success($data);
foreach ($path as $pathKey) {
$currentValue = $result->getValue();
if (!is_array($currentValue)) {
return Value::fail();
return Result::fail();
}
$result = self::getValueByKey($currentValue, $pathKey);
if (!$result->isResolved()) {
Expand All @@ -39,7 +39,7 @@ public static function getValueByPath(array $data, string|array $path): Value
return $result;
}

private static function getValueByKey(array $data, string $pathKey): Value
private static function getValueByKey(array $data, string $pathKey): Result
{
$found = false;
$result = null;
Expand All @@ -66,6 +66,6 @@ private static function getValueByKey(array $data, string $pathKey): Value
}
}

return $found ? Value::success($result) : Value::fail();
return $found ? Result::success($result) : Result::fail();
}
}
38 changes: 19 additions & 19 deletions src/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,24 @@ private function getConstructorArguments(string $class, array $sourceData, array
}

$parameterName = $parameter->getName();
$resolvedValue = Value::fail();
$resolveResult = Result::fail();

if ($parameter->isPromoted()) {
$excludeParameterNames[] = $parameterName;
$resolvedValue = $this->resolve($parameterName, $data);
$resolveResult = $this->resolve($parameterName, $data);
}

$valueFromAttributes = $this->parameterAttributesHandler->handle(
$attributesHandleResult = $this->parameterAttributesHandler->handle(
$parameter,
$resolvedValue,
$resolveResult,
$data
);
if ($valueFromAttributes->isResolved()) {
$resolvedValue = $valueFromAttributes;
if ($attributesHandleResult->isResolved()) {
$resolveResult = $attributesHandleResult;
}

if ($resolvedValue->isResolved()) {
$typeCastedValue = $this->typeCaster->cast($resolvedValue->getValue(), $parameter->getType());
if ($resolveResult->isResolved()) {
$typeCastedValue = $this->typeCaster->cast($resolveResult->getValue(), $parameter->getType());
if ($typeCastedValue->isResolved()) {
$constructorArguments[$parameterName] = $typeCastedValue->getValue();
}
Expand Down Expand Up @@ -132,34 +132,34 @@ private function getHydrateData(
continue;
}

$resolvedValue = $this->resolve($propertyName, $data);
$resolveResult = $this->resolve($propertyName, $data);

$valueFromAttributes = $this->parameterAttributesHandler->handle(
$attributesHandleResult = $this->parameterAttributesHandler->handle(
$property,
$resolvedValue,
$resolveResult,
$data
);
if ($valueFromAttributes->isResolved()) {
$resolvedValue = $valueFromAttributes;
if ($attributesHandleResult->isResolved()) {
$resolveResult = $attributesHandleResult;
}

if ($resolvedValue->isResolved()) {
$typeCastedValue = $this->typeCaster->cast($resolvedValue->getValue(), $property->getType());
if ($typeCastedValue->isResolved()) {
$hydrateData[$propertyName] = $typeCastedValue->getValue();
if ($resolveResult->isResolved()) {
$result = $this->typeCaster->cast($resolveResult->getValue(), $property->getType());
if ($result->isResolved()) {
$hydrateData[$propertyName] = $result->getValue();
}
}
}

return $hydrateData;
}

private function resolve(string $name, Data $data): Value
private function resolve(string $name, Data $data): Result
{
$map = $data->getMap();

if ($data->isStrict() && !array_key_exists($name, $map)) {
return Value::fail();
return Result::fail();
}

return DataHelper::getValueByPath($data->getData(), $map[$name] ?? $name);
Expand Down
2 changes: 1 addition & 1 deletion src/ParameterAttributeResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface ParameterAttributeResolverInterface
{
public function getParameterValue(ParameterAttributeInterface $attribute, Context $context): Value;
public function getParameterValue(ParameterAttributeInterface $attribute, Context $context): Result;
}
22 changes: 11 additions & 11 deletions src/ParameterAttributesHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,37 @@ public function __construct(

public function handle(
ReflectionParameter|ReflectionProperty $parameter,
?Value $resolvedValue = null,
?Result $resolveResult = null,
?Data $data = null
): Value {
$resolvedValue ??= Value::fail();
): Result {
$resolveResult ??= Result::fail();

$reflectionAttributes = $parameter
->getAttributes(ParameterAttributeInterface::class, ReflectionAttribute::IS_INSTANCEOF);

$hereResolvedValue = Value::fail();
$hereResolveResult = Result::fail();
foreach ($reflectionAttributes as $reflectionAttribute) {
$attribute = $reflectionAttribute->newInstance();
$resolver = $this->getParameterResolver($attribute);

$context = new Context(
$parameter,
$hereResolvedValue->isResolved() ? $hereResolvedValue : $resolvedValue,
$hereResolveResult->isResolved() ? $hereResolveResult : $resolveResult,
$data?->getData() ?? [],
$data?->getMap() ?? [],
);

$hereResolvedValue = $resolver->getParameterValue($attribute, $context);
$hereResolveResult = $resolver->getParameterValue($attribute, $context);
}

if ($this->typeCaster !== null && $hereResolvedValue->isResolved()) {
$typeCastedValue = $this->typeCaster->cast($hereResolvedValue->getValue(), $parameter->getType());
if ($typeCastedValue->isResolved()) {
$hereResolvedValue = $typeCastedValue;
if ($this->typeCaster !== null && $hereResolveResult->isResolved()) {
$result = $this->typeCaster->cast($hereResolveResult->getValue(), $parameter->getType());
if ($result->isResolved()) {
$hereResolveResult = $result;
}
}

return $hereResolvedValue;
return $hereResolveResult;
}

private function getParameterResolver(ParameterAttributeInterface $attribute): ParameterAttributeResolverInterface
Expand Down
2 changes: 1 addition & 1 deletion src/Value.php → src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Hydrator;

final class Value
final class Result
{
private function __construct(
private bool $isResolved,
Expand Down
6 changes: 3 additions & 3 deletions src/TypeCaster/CompositeTypeCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use ReflectionType;
use Yiisoft\Hydrator\TypeCasterInterface;
use Yiisoft\Hydrator\Value;
use Yiisoft\Hydrator\Result;

final class CompositeTypeCaster implements TypeCasterInterface
{
Expand All @@ -21,7 +21,7 @@ public function __construct(
$this->typeCasters = $typeCasters;
}

public function cast(mixed $value, ?ReflectionType $type): Value
public function cast(mixed $value, ?ReflectionType $type): Result
{
foreach ($this->typeCasters as $typeCaster) {
$result = $typeCaster->cast($value, $type);
Expand All @@ -30,6 +30,6 @@ public function cast(mixed $value, ?ReflectionType $type): Value
}
}

return Value::fail();
return Result::fail();
}
}
6 changes: 3 additions & 3 deletions src/TypeCaster/NoTypeCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

use ReflectionType;
use Yiisoft\Hydrator\TypeCasterInterface;
use Yiisoft\Hydrator\Value;
use Yiisoft\Hydrator\Result;

final class NoTypeCaster implements TypeCasterInterface
{
public function cast(mixed $value, ?ReflectionType $type): Value
public function cast(mixed $value, ?ReflectionType $type): Result
{
return Value::success($value);
return Result::success($value);
}
}

0 comments on commit bce6ee0

Please sign in to comment.