Skip to content
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
11 changes: 10 additions & 1 deletion src/Aggregate/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
use Override;

use function assert;
use function is_array;
use function is_object;
use function sprintf;
use function trigger_error;

/**
* The base form element
Expand Down Expand Up @@ -216,7 +220,7 @@
}

/** @var T $value */
return $this->value = $value;
return $this->value = $this->generator->finalize($value);
}

#[Override]
Expand Down Expand Up @@ -296,6 +300,11 @@
#[Override]
public function attach($entity): FormInterface
{
/** @psalm-suppress RedundantCondition */
if (!is_object($entity) && !is_array($entity)) {

Check warning on line 304 in src/Aggregate/Form.php

View workflow job for this annotation

GitHub Actions / Analysis

Escaped Mutant for Mutator "LogicalAndNegation": @@ @@ public function attach($entity): FormInterface { /** @psalm-suppress RedundantCondition */ - if (!is_object($entity) && !is_array($entity)) { + if (!(!is_object($entity) && !is_array($entity))) { @trigger_error(sprintf('Attaching a non-object and non-array value is deprecated since bdf-form 2.0 and will be removed. Got %s.', get_debug_type($entity)), E_USER_DEPRECATED); }

Check warning on line 304 in src/Aggregate/Form.php

View workflow job for this annotation

GitHub Actions / Analysis

Escaped Mutant for Mutator "LogicalAndAllSubExprNegation": @@ @@ public function attach($entity): FormInterface { /** @psalm-suppress RedundantCondition */ - if (!is_object($entity) && !is_array($entity)) { + if (is_object($entity) && is_array($entity)) { @trigger_error(sprintf('Attaching a non-object and non-array value is deprecated since bdf-form 2.0 and will be removed. Got %s.', get_debug_type($entity)), E_USER_DEPRECATED); }

Check warning on line 304 in src/Aggregate/Form.php

View workflow job for this annotation

GitHub Actions / Analysis

Escaped Mutant for Mutator "LogicalAnd": @@ @@ public function attach($entity): FormInterface { /** @psalm-suppress RedundantCondition */ - if (!is_object($entity) && !is_array($entity)) { + if (!is_object($entity) || !is_array($entity)) { @trigger_error(sprintf('Attaching a non-object and non-array value is deprecated since bdf-form 2.0 and will be removed. Got %s.', get_debug_type($entity)), E_USER_DEPRECATED); }

Check warning on line 304 in src/Aggregate/Form.php

View workflow job for this annotation

GitHub Actions / Analysis

Escaped Mutant for Mutator "LogicalNot": @@ @@ public function attach($entity): FormInterface { /** @psalm-suppress RedundantCondition */ - if (!is_object($entity) && !is_array($entity)) { + if (!is_object($entity) && is_array($entity)) { @trigger_error(sprintf('Attaching a non-object and non-array value is deprecated since bdf-form 2.0 and will be removed. Got %s.', get_debug_type($entity)), E_USER_DEPRECATED); }

Check warning on line 304 in src/Aggregate/Form.php

View workflow job for this annotation

GitHub Actions / Analysis

Escaped Mutant for Mutator "LogicalNot": @@ @@ public function attach($entity): FormInterface { /** @psalm-suppress RedundantCondition */ - if (!is_object($entity) && !is_array($entity)) { + if (is_object($entity) && !is_array($entity)) { @trigger_error(sprintf('Attaching a non-object and non-array value is deprecated since bdf-form 2.0 and will be removed. Got %s.', get_debug_type($entity)), E_USER_DEPRECATED); }
@trigger_error(sprintf('Attaching a non-object and non-array value is deprecated since bdf-form 2.0 and will be removed. Got %s.', get_debug_type($entity)), E_USER_DEPRECATED);
}

$this->generator->attach($entity);
$this->value = null; // The value is only attached : it must be filled when calling value()

Expand Down
2 changes: 1 addition & 1 deletion src/Aggregate/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public function generator(ValueGeneratorInterface $generator): FormBuilderInterf
* {@inheritdoc}
*/
#[Override]
public function generates($entity): FormBuilderInterface
public function generates(mixed $entity): FormBuilderInterface
{
return $this->generator(new ValueGenerator($entity));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Aggregate/FormBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public function generator(ValueGeneratorInterface $generator): FormBuilderInterf
* });
* </code>
*
* @param callable|class-string|object|array $entity The entity to generate
* @param (callable(ElementInterface):array|object)|class-string|array|object $entity The entity to generate
*
* @return $this
*
Expand Down
2 changes: 1 addition & 1 deletion src/Aggregate/FormInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface FormInterface extends ChildAggregateInterface
* $this->repository->save($form->value());
* </code>
*
* @param T|class-string|callable():T $entity The entity object, or class name
* @param T $entity The entity object, or initial array values
*
* @return $this
*
Expand Down
51 changes: 51 additions & 0 deletions src/Aggregate/Value/ClosureValueGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Bdf\Form\Aggregate\Value;

use Bdf\Form\ElementInterface;
use Closure;
use Override;

/**
* Generate a value using a closure
*
* @template T as array|object
* @implements ValueGeneratorInterface<T>
*/
final class ClosureValueGenerator implements ValueGeneratorInterface
{
/**
* @var T|null
*/
private array|object|null $attachment = null;

public function __construct(
/**
* @var Closure(ElementInterface):T
*/
private readonly Closure $generator,
) {}

#[Override]
public function attach(mixed $entity): void
{
$this->attachment = $entity;
}

#[Override]
public function generate(ElementInterface $element): object|array
{
if ($this->attachment !== null) {
return $this->attachment;
}

return ($this->generator)($element);
}

#[Override]
public function finalize(object|array $value): object|array
{
/** @var T */
return $value;
}
}
62 changes: 62 additions & 0 deletions src/Aggregate/Value/ConstructorValueGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Bdf\Form\Aggregate\Value;

use Bdf\Form\ElementInterface;
use InvalidArgumentException;
use Override;

use function assert;
use function is_array;
use function sprintf;

/**
* Instantiate the form value using its constructor with named parameters
* Hydrators will fill an array which will be used as parameters of the class constructor
*
* @template T as object
* @implements ValueGeneratorInterface<T>
*/
final class ConstructorValueGenerator implements ValueGeneratorInterface
{
/**
* @var T|array|null
*/
private array|object|null $attachment = null;

public function __construct(
/**
* @var class-string<T>
*/
private readonly string $class,
) {}

#[Override]
public function attach(mixed $entity): void
{
if (is_array($entity) || $entity instanceof $this->class) {
$this->attachment = $entity;
return;
}

throw new InvalidArgumentException(sprintf('Expected array or instance of %s, %s given on %s::attach()', $this->class, get_debug_type($entity), self::class));
}

#[Override]
public function generate(ElementInterface $element): object|array
{
return $this->attachment ?? [];
}

#[Override]
public function finalize(object|array $value): object
{
if (is_array($value)) {
$value = new ($this->class)(...$value);
}

assert($value instanceof $this->class);

return $value;
}
}
62 changes: 62 additions & 0 deletions src/Aggregate/Value/DefaultConstructorValueGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Bdf\Form\Aggregate\Value;

use Bdf\Form\ElementInterface;
use InvalidArgumentException;
use Override;

use function assert;
use function get_class;
use function get_debug_type;
use function sprintf;

/**
* Generate a value by call its constructor without arguments
* Properties will be filled directly by hydrators
*
* @template T as object
* @implements ValueGeneratorInterface<T>
*/
final class DefaultConstructorValueGenerator implements ValueGeneratorInterface
{
/**
* @var T|null
*/
private ?object $attachment = null;

public function __construct(
/**
* @var class-string<T>
*/
private readonly string $class,
) {}

#[Override]
public function attach(mixed $entity): void
{
if (!$entity instanceof $this->class) {
throw new InvalidArgumentException(sprintf('Cannot attach a value of type %s, expecting %s', get_debug_type($entity), $this->class));
}

$this->attachment = $entity;
}

#[Override]
public function generate(ElementInterface $element): object
{
if ($this->attachment !== null) {
return $this->attachment;
}

return new $this->class;
}

#[Override]
public function finalize(object|array $value): object
{
assert($value instanceof $this->class);

return $value;
}
}
62 changes: 62 additions & 0 deletions src/Aggregate/Value/ObjectValueGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Bdf\Form\Aggregate\Value;

use Bdf\Form\ElementInterface;
use InvalidArgumentException;
use Override;

use function assert;
use function get_class;
use function get_debug_type;
use function sprintf;

/**
* Generate a value using an object as template which will be cloned for each generation
*
* @template T as object
* @implements ValueGeneratorInterface<T>
*/
final class ObjectValueGenerator implements ValueGeneratorInterface
{
/**
* @var T|null
*/
private ?object $attachment = null;

public function __construct(
/**
* @var T
*/
private readonly object $value,
) {}

#[Override]
public function attach(mixed $entity): void
{
if (!$entity instanceof $this->value) {
throw new InvalidArgumentException(sprintf('Cannot attach a value of type %s, expecting %s', get_debug_type($entity), get_class($this->value)));
}

$this->attachment = $entity;
}

#[Override]
public function generate(ElementInterface $element): object
{
if ($this->attachment !== null) {
return $this->attachment;
}

return clone $this->value;
}

#[Override]
public function finalize(object|array $value): object
{
assert($value instanceof $this->value);

/** @var T */
return $value;
}
}
41 changes: 41 additions & 0 deletions src/Aggregate/Value/SimpleValueGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Bdf\Form\Aggregate\Value;

use Bdf\Form\ElementInterface;
use Override;

/**
* Simply return the stored value
*
* @template T as array|object
* @implements ValueGeneratorInterface<T>
*/
final class SimpleValueGenerator implements ValueGeneratorInterface
{
public function __construct(
/**
* @var T
*/
private array|object $value = [],
) {}

#[Override]
public function attach(mixed $entity): void
{
$this->value = $entity;
}

#[Override]
public function generate(ElementInterface $element): object|array
{
return $this->value;
}

#[Override]
public function finalize(object|array $value): object|array
{
/** @var T */
return $value;
}
}
Loading
Loading