Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(11): Fixed InitialValueCallback() attribute #12

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion src/Expression/Assets/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ public function build(Property $source, Property $target): self

$hasFunction = !empty($this->function);
$isPathUsed = (bool) $this->function?->pathVariable;
$initialCallback = $target->hasDedicatedInitCallback(true) ? $target->getInitialCallbackAttribute(true) : null;
$useInitialCallbackInsteadOfGetter = $initialCallback?->useSourceInsteadIfExists ?? false;

if ($target->isCollection()) {
$itemExpressionArgs = [
$target->hasDedicatedInitCallback(true),
(bool) $initialCallback,
$useInitialCallbackInsteadOfGetter,
true,
false,
!empty($this->collectionItemCallbacksExpression),
Expand All @@ -97,8 +100,13 @@ public function build(Property $source, Property $target): self
$itemExpression = str_replace(array_keys($itemExpressionPlaceholders), array_values($itemExpressionPlaceholders), $itemExpression);
} while ($this->hasNotFilledPlaceholders(array_keys($itemExpressionPlaceholders), $itemExpression));


$initialCallback = $target->hasDedicatedInitCallback(false) ? $target->getInitialCallbackAttribute(false) : null;
$useInitialCallbackInsteadOfGetter = $initialCallback?->useSourceInsteadIfExists ?? false;

$expressionArgs = [
$target->hasDedicatedInitCallback(false),
$useInitialCallbackInsteadOfGetter,
$this->throwExceptionOnMissingRequiredValue,
$target->hasDefaultValue(),
!empty($this->callbacksExpression),
Expand All @@ -113,8 +121,11 @@ public function build(Property $source, Property $target): self
[$expression, $expressionPlaceholders] = $this->newExpression(...$expressionArgs);
$expressionPlaceholders['{{preAssignmentExpression}}'] = $itemExpression;
} else {
$initialCallback = $target->hasDedicatedInitCallback(false) ? $target->getInitialCallbackAttribute(false) : null;
$useInitialCallbackInsteadOfGetter = $initialCallback?->useSourceInsteadIfExists ?? false;
$expressionArgs = [
$target->hasDedicatedInitCallback(false),
$useInitialCallbackInsteadOfGetter,
$this->throwExceptionOnMissingRequiredValue,
$target->hasDefaultValue(),
!empty($this->callbacksExpression),
Expand Down Expand Up @@ -197,6 +208,7 @@ private function applyCallbacks(Property $source, Property $target): void
*/
private function newExpression(
bool $hasDedicatedGetter,
bool $useInitialCallbackInsteadOfGetter,
bool $throwExceptionOnMissingRequiredValue,
bool $hasDefaultValue,
bool $hasCallbacks,
Expand All @@ -209,6 +221,7 @@ private function newExpression(
): array {
$getterExpressionArgs = [
$hasDedicatedGetter,
$useInitialCallbackInsteadOfGetter,
$throwExceptionOnMissingRequiredValue,
$hasDefaultValue,
$hasCallbacks,
Expand Down
3 changes: 3 additions & 0 deletions src/Expression/Assets/Getter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Getter
{
public const STATEMENTS_ORDER = [
'hasDedicatedGetter',
'useInitialCallbackInsteadOfGetter',
'throwExceptionOnMissingRequiredValue',
'hasDefaultValue',
'hasCallbacks',
Expand All @@ -28,6 +29,7 @@ public function __construct(

public function getExpressionTemplate(
bool $hasDedicatedGetter,
bool $useInitialCallbackInsteadOfGetter,
bool $throwExceptionOnMissingRequiredValue,
bool $hasDefaultValue,
bool $hasCallbacks,
Expand All @@ -45,6 +47,7 @@ public function getExpressionTemplate(
*/
public function getExpressions(
bool $hasDedicatedGetter,
bool $useInitialCallbackInsteadOfGetter,
bool $throwExceptionOnMissingRequiredValue,
bool $hasDefaultValue,
bool $hasCallbacks,
Expand Down
12 changes: 7 additions & 5 deletions src/Expression/Builder/AbstractBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ public function getSetterFinalExpression(Blueprint $blueprint, string $functionI

/**
* @return string
* Placeholders list:
* {{name}}
*
* Placeholders list:
* {{name}}
*
* {{setterAssignment:var}}
* {{setterAssignment:basic}}
Expand Down Expand Up @@ -128,9 +129,10 @@ protected function getGetterExpressionTemplate(

/**
* @return string
* Placeholders list:
* {{getterExpression}}
* {{sourceIteratorAssignment}}
*
* Placeholders list:
* {{getterExpression}}
* {{sourceIteratorAssignment}}
*
* {{function}}
* {{functionVariable}}
Expand Down
19 changes: 10 additions & 9 deletions src/Expression/Builder/AnonymousObjectExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,16 @@ public function getGetter(Property $property): Getter

$expressions = [];
$expressionTemplates = [];
for ($i = 0; $i < 128; ++$i) {
$key = str_pad(decbin($i), 7, '0', STR_PAD_LEFT);
for ($i = 0; $i < 256; ++$i) {
$key = str_pad(decbin($i), 8, '0', STR_PAD_LEFT);
$hasDedicatedGetter = '1' === $key[0];
$throwExceptionOnMissingRequiredValue = '1' === $key[1];
$hasDefaultValue = '1' === $key[2];
$hasCallbacks = '1' === $key[3];
$hasValueNotFoundCallbacks = '1' === $key[4];
$isCollection = '1' === $key[5];
$preAssignmentExpression = '1' === $key[6];
$useInitialCallbackInsteadOfGetter = '1' === $key[1];
$throwExceptionOnMissingRequiredValue = '1' === $key[2];
$hasDefaultValue = '1' === $key[3];
$hasCallbacks = '1' === $key[4];
$hasValueNotFoundCallbacks = '1' === $key[5];
$isCollection = '1' === $key[6];
$preAssignmentExpression = '1' === $key[7];

if ($hasDefaultValue) {
$throwExceptionOnMissingRequiredValue = true;
Expand Down Expand Up @@ -94,7 +95,7 @@ public function getGetter(Property $property): Getter
'{{varAssignment:basic:default}}' => $preAssignmentExpression ? "\${{var}} ??= {{defaultValue}};\n" : "\${{var}} = {{getterAssignment:basic}} ?? {{defaultValue}};\n",
'{{varAssignnmet:item}}' => "\${{var}} = \$item;\n",
'{{varAssignment:dedicated}}' => "\${{var}} = {{dedicatedGetter}};\n",
'{{varAssignment:dedicated:default}}' => "if ({{existsStatement}}) {\n"
'{{varAssignment:dedicated:default}}' => ($useInitialCallbackInsteadOfGetter ? "if (!{{existsStatement}}) {\n" : "if ({{existsStatement}}) {\n")
."\t\${{var}} = {{dedicatedGetter}};\n"
."} else {\n"
."\t\${{var}} = {{defaultValue}};\n"
Expand Down
19 changes: 10 additions & 9 deletions src/Expression/Builder/ArrayExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,16 @@ public function getGetter(Property $property): Getter

$expressions = [];
$expressionTemplates = [];
for ($i = 0; $i < 128; ++$i) {
$key = str_pad(decbin($i), 7, '0', STR_PAD_LEFT);
for ($i = 0; $i < 256; ++$i) {
$key = str_pad(decbin($i), 8, '0', STR_PAD_LEFT);
$hasDedicatedGetter = '1' === $key[0];
$throwExceptionOnMissingRequiredValue = '1' === $key[1];
$hasDefaultValue = '1' === $key[2];
$hasCallbacks = '1' === $key[3];
$hasValueNotFoundCallbacks = '1' === $key[4];
$isCollection = '1' === $key[5];
$preAssignmentExpression = '1' === $key[6];
$useInitialCallbackInsteadOfGetter = '1' === $key[1];
$throwExceptionOnMissingRequiredValue = '1' === $key[2];
$hasDefaultValue = '1' === $key[3];
$hasCallbacks = '1' === $key[4];
$hasValueNotFoundCallbacks = '1' === $key[5];
$isCollection = '1' === $key[6];
$preAssignmentExpression = '1' === $key[7];

if ($hasDefaultValue) {
$throwExceptionOnMissingRequiredValue = true;
Expand Down Expand Up @@ -95,7 +96,7 @@ public function getGetter(Property $property): Getter
'{{varAssignment:basic:default}}' => $preAssignmentExpression ? "\${{var}} ??= {{defaultValue}};\n" : "\${{var}} = {{getterAssignment:basic}} ?? {{defaultValue}};\n",
'{{varAssignnmet:item}}' => "\${{var}} = \$item;\n",
'{{varAssignment:dedicated}}' => "\${{var}} = {{dedicatedGetter}};\n",
'{{varAssignment:dedicated:default}}' => "if ({{existsStatement}}) {\n"
'{{varAssignment:dedicated:default}}' => ($useInitialCallbackInsteadOfGetter ? "if (!{{existsStatement}}) {\n" : "if ({{existsStatement}}) {\n")
."\t\${{var}} = {{dedicatedGetter}};\n"
."} else {\n"
."\t\${{var}} = {{defaultValue}};\n"
Expand Down
19 changes: 10 additions & 9 deletions src/Expression/Builder/ReflectionClassExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,16 @@ public function getGetter(Property $property): Getter

$expressions = [];
$expressionTemplates = [];
for ($i = 0; $i < 128; ++$i) {
$key = str_pad(decbin($i), 7, '0', STR_PAD_LEFT);
for ($i = 0; $i < 256; ++$i) {
$key = str_pad(decbin($i), 8, '0', STR_PAD_LEFT);
$hasDedicatedGetter = '1' === $key[0];
$throwExceptionOnMissingRequiredValue = '1' === $key[1];
$hasDefaultValue = '1' === $key[2];
$hasCallbacks = '1' === $key[3];
$hasValueNotFoundCallbacks = '1' === $key[4];
$isCollection = '1' === $key[5];
$preAssignmentExpression = '1' === $key[6];
$useInitialCallbackInsteadOfGetter = '1' === $key[1];
$throwExceptionOnMissingRequiredValue = '1' === $key[2];
$hasDefaultValue = '1' === $key[3];
$hasCallbacks = '1' === $key[4];
$hasValueNotFoundCallbacks = '1' === $key[5];
$isCollection = '1' === $key[6];
$preAssignmentExpression = '1' === $key[7];

if ($hasDefaultValue) {
$throwExceptionOnMissingRequiredValue = true;
Expand Down Expand Up @@ -143,7 +144,7 @@ public function getGetter(Property $property): Getter
'{{varAssignment:basic:default}}' => $preAssignmentExpression ? "\${{var}} ??= {{defaultValue}};\n" : "\${{var}} = {{getterAssignment:basic:default}};\n",
'{{varAssignnmet:item}}' => "\${{var}} = \$item;\n",
'{{varAssignment:dedicated}}' => "\${{var}} = {{dedicatedGetter}};\n",
'{{varAssignment:dedicated:default}}' => "if ({{existsStatement}} && {{defaultValue}} !== {{getterAssignment:basic}}) {\n"
'{{varAssignment:dedicated:default}}' => ($useInitialCallbackInsteadOfGetter ? "if (!{{existsStatement}}) {\n" : "if ({{existsStatement}}) {\n")
."\t\${{var}} = {{dedicatedGetter}};\n"
."} else {\n"
."\t\${{var}} = {{defaultValue}};\n"
Expand Down
98 changes: 98 additions & 0 deletions tests/Func/Attribute/InitialValueCallbackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace PBaszak\DedicatedMapper\Tests\Func\Attribute;

use PBaszak\DedicatedMapper\Attribute\InitialValueCallback;
use PBaszak\DedicatedMapper\Expression\Builder\ArrayExpressionBuilder;
use PBaszak\DedicatedMapper\Expression\Builder\FunctionExpressionBuilder;
use PBaszak\DedicatedMapper\Expression\Builder\ReflectionClassExpressionBuilder;
use PBaszak\DedicatedMapper\Expression\ExpressionBuilder;
use PBaszak\DedicatedMapper\Properties\Blueprint;
use PHPUnit\Framework\TestCase;

class InitialValueCallbackTester
{
#[InitialValueCallback('new \DateTime(\'2023-01-01 00:00:00\')')]
public \DateTime $test;
}

class InitialValueCallbackUseSourceTester
{
#[InitialValueCallback('new \DateTime(\'2023-01-01 00:00:00\')', true)]
public \DateTime $test;
}

class InitialValueCallbackNullableTester
{
#[InitialValueCallback('new \DateTime(\'2023-01-01 00:00:00\')')]
public ?\DateTime $test = null;
}

class InitialValueCallbackNullableUseSourceTester
{
#[InitialValueCallback('new \DateTime(\'2023-01-01 00:00:00\')', true)]
public ?\DateTime $test = null;
}

/** @group func */
class InitialValueCallbackTest extends TestCase
{
/** @test */
public function shouldAddSpecialTimeToOutput(): void
{
$data = [];
foreach ([true, false] as $throwExceptionOnMissing) {
foreach ([true, false] as $allowNullable) {
foreach ([true, false] as $useSource) {
$output = $this->map($data, $throwExceptionOnMissing, $allowNullable, $useSource);
$this->assertEquals(new \DateTime('2023-01-01 00:00:00'), $output->test);
}
}
}
}

/** @test */
public function shouldNotUseIncomingTimeForOutput(): void
{
$data = [
'test' => new \DateTime('2021-01-01 00:00:00'),
];
foreach ([true, false] as $throwExceptionOnMissing) {
foreach ([true, false] as $allowNullable) {
$output = $this->map($data, $throwExceptionOnMissing, $allowNullable, false);
$this->assertEquals(new \DateTime('2023-01-01 00:00:00'), $output->test);
}
}
}

/** @test */
public function shouldUseIncomingTimeForOutput(): void
{
$data = [
'test' => new \DateTime('2021-01-01 00:00:00'),
];
foreach ([true, false] as $throwExceptionOnMissing) {
foreach ([true, false] as $allowNullable) {
$output = $this->map($data, $throwExceptionOnMissing, $allowNullable, true);
$this->assertEquals(new \DateTime('2021-01-01 00:00:00'), $output->test);
}
}
}

private function map(array $data, bool $throwExceptionOnMissing, bool $allowNullable, bool $useSource): InitialValueCallbackNullableUseSourceTester|InitialValueCallbackNullableTester|InitialValueCallbackUseSourceTester|InitialValueCallbackTester
{
return (new ExpressionBuilder(
Blueprint::create(
$allowNullable
? ($useSource ? InitialValueCallbackNullableUseSourceTester::class : InitialValueCallbackNullableTester::class)
: ($useSource ? InitialValueCallbackUseSourceTester::class : InitialValueCallbackTester::class)
),
new ArrayExpressionBuilder(),
new ReflectionClassExpressionBuilder(),
new FunctionExpressionBuilder(),
false
))->build($throwExceptionOnMissing)->getMapper()->map($data);
}
}