Skip to content

Commit

Permalink
Implement ValueResolver (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Jul 27, 2016
1 parent 7c5f7dd commit f961428
Show file tree
Hide file tree
Showing 44 changed files with 1,367 additions and 655 deletions.
2 changes: 1 addition & 1 deletion fixtures/Nelmio/Alice/Definition/Fixture/DummyFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DummyFixture implements FixtureInterface

public function __construct(string $reference)
{
$this->id = uniqid($reference);
$this->id = __CLASS__.'#'.$reference;
$this->reference = $reference;
}

Expand Down
28 changes: 28 additions & 0 deletions fixtures/Nelmio/Alice/Definition/Value/FakeValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\Alice\Definition\Value;

use Nelmio\Alice\Definition\ValueInterface;
use Nelmio\Alice\NotCallableTrait;

class FakeValue implements ValueInterface
{
use NotCallableTrait;

/**
* @inheritdoc
*/
public function getValue()
{
$this->__call();
}
}
28 changes: 28 additions & 0 deletions fixtures/Nelmio/Alice/Generator/ResolvedFixtureSetFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\Alice\Generator;

use Nelmio\Alice\FixtureBag;
use Nelmio\Alice\ObjectBag;
use Nelmio\Alice\ParameterBag;

class ResolvedFixtureSetFactory
{
public static function create(ParameterBag $parameters = null, FixtureBag $fixtures = null, ObjectBag $objects = null): ResolvedFixtureSet
{
return new ResolvedFixtureSet(
(null === $parameters) ? new ParameterBag() : $parameters,
(null === $fixtures) ? new FixtureBag() : $fixtures,
(null === $objects) ? new ObjectBag() : $objects
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,33 @@

namespace Nelmio\Alice\Generator\Resolver\Value;

use Nelmio\Alice\Definition\ValueInterface;
use Nelmio\Alice\FixtureInterface;
use Nelmio\Alice\Generator\ResolvedFixtureSet;
use Nelmio\Alice\Generator\ResolvedValueWithFixtureSet;
use Nelmio\Alice\Generator\ValueResolverInterface;
use Nelmio\Alice\NotCallableTrait;

final class PartsResolver implements ValueResolverInterface
class FakeChainableValueResolver implements ChainableValueResolverInterface
{
use NotCallableTrait;

/**
* @inheritdoc
*/
public function canResolve(ValueInterface $value): bool
{
$this->__call();
}

/**
* @inheritdoc
*/
public function resolve(
$value,
ValueInterface $value,
FixtureInterface $fixture,
ResolvedFixtureSet $fixtureSet,
array $scope = []
): ResolvedValueWithFixtureSet
array $scope = []): ResolvedValueWithFixtureSet
{
var_dump($value);

return new ResolvedValueWithFixtureSet($value, $fixtureSet);
$this->__call();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Nelmio\Alice\Generator\Resolver\Value;

use Nelmio\Alice\Definition\ValueInterface;
use Nelmio\Alice\FixtureInterface;
use Nelmio\Alice\Generator\ResolvedFixtureSet;
use Nelmio\Alice\Generator\ResolvedValueWithFixtureSet;
Expand All @@ -25,7 +26,7 @@ class FakeValueResolver implements ValueResolverInterface
* @inheritdoc
*/
public function resolve(
$value,
ValueInterface $value,
FixtureInterface $fixture,
ResolvedFixtureSet $fixtureSet,
array $scope = []): ResolvedValueWithFixtureSet
Expand Down
6 changes: 3 additions & 3 deletions src/Nelmio/Alice/Definition/Value/DynamicArrayValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class DynamicArrayValue implements ValueInterface

/**
* @param string|ValueInterface $quantifier
* @param string|ValueInterface $element
* @param string|ValueInterface $element
*/
public function __construct($quantifier, $element)
{
Expand All @@ -39,11 +39,11 @@ public function __construct($quantifier, $element)
}

/**
* @return string|ValueInterface
* @return int|ValueInterface
*/
public function getQuantifier()
{
return is_object($this->quantifier) ? clone $this->quantifier : $this->quantifier;
return is_object($this->quantifier) ? clone $this->quantifier : (int) $this->quantifier;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/Nelmio/Alice/Definition/Value/UniqueValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,24 @@ final class UniqueValue implements ValueInterface
public function __construct(string $id, $value)
{
$this->id = $id;

if ($value instanceof self) {
throw new \InvalidArgumentException(
sprintf(
'Cannot create a unique value of a unique value for value "%s".',
$id
)
);
}

$this->value = $value;
}

public function withValue($value): self
{
return new self($this->id, $value);
}

public function getId(): string
{
return $this->id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@

class FlagParserNotFoundException extends \RuntimeException
{
public static function create(string $element): self
{
return new static(
sprintf(
'No suitable flag parser found to handle the element "%s".',
$element
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@

class ParserNotFoundException extends \RuntimeException implements ParseThrowable
{
public static function create(string $file): self
{
return new static(
sprintf(
'No suitable parser found for the file "%s".',
$file
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Nelmio\Alice\Exception\Resolver;
namespace Nelmio\Alice\Exception\Generator\Resolver;

use Nelmio\Alice\Throwable\ResolveThrowable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Nelmio\Alice\Exception\Resolver;
namespace Nelmio\Alice\Exception\Generator\Resolver;

use Nelmio\Alice\Exception\ParameterNotFoundException as RootParameterNotFoundException;
use Nelmio\Alice\Throwable\ResolveThrowable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Nelmio\Alice\Exception\Resolver;
namespace Nelmio\Alice\Exception\Generator\Resolver;

use Nelmio\Alice\Throwable\ResolveThrowable;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\Alice\Exception\Generator\Resolver;

use Nelmio\Alice\Definition\Value\UniqueValue;
use Nelmio\Alice\Throwable\ResolutionThrowable;

class UniqueValueGenerationLimit extends \RuntimeException implements ResolutionThrowable
{
public static function create(UniqueValue $value, int $limit): self
{
return new static(
sprintf(
'Could not generate a unique value after %d attempts for "%s".',
$limit,
$value->getId()
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Nelmio\Alice\Definition\Flag\UniqueFlag;
use Nelmio\Alice\Definition\FlagBag;
use Nelmio\Alice\Definition\Value\DynamicArrayValue;
use Nelmio\Alice\Definition\Value\UniqueValue;
use Nelmio\Alice\Definition\ValueInterface;
use Nelmio\Alice\ExpressionLanguage\ParserInterface;
Expand Down Expand Up @@ -89,7 +90,13 @@ protected function handleArgumentFlags(FixtureInterface $scope, FlagBag $flags =

if ($this->requiresUnique($flags)) {
$uniqueId = uniqid($scope->getId());


if ($argument instanceof DynamicArrayValue) {
return new DynamicArrayValue(
$argument->getQuantifier(),
new UniqueValue($uniqueId, $argument->getValue())
);
}
return new UniqueValue($uniqueId, $argument);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Nelmio\Alice\Definition\Flag\UniqueFlag;
use Nelmio\Alice\Definition\FlagBag;
use Nelmio\Alice\Definition\Property;
use Nelmio\Alice\Definition\Value\DynamicArrayValue;
use Nelmio\Alice\Definition\Value\UniqueValue;
use Nelmio\Alice\ExpressionLanguage\ParserInterface;
use Nelmio\Alice\FixtureInterface;
Expand Down Expand Up @@ -53,7 +54,18 @@ public function denormalize(FixtureInterface $scope, string $name, $value, FlagB
if ($flag instanceof UniqueFlag) {
$uniqueId = $scope->getId().'::'.$name;

return new Property($name, new UniqueValue($uniqueId, $value));
if ($value instanceof DynamicArrayValue) {
$value = new DynamicArrayValue(
$value->getQuantifier(),
new UniqueValue($uniqueId, $value->getValue())
);

break;
}

$value = new UniqueValue($uniqueId, $value);

break;
}
}

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

namespace Nelmio\Alice\FixtureBuilder\Denormalizer\FlagParser;

use Nelmio\Alice\Definition\FlagBag;
use Nelmio\Alice\FixtureBuilder\Denormalizer\FlagParserInterface;

interface ChainableFlagParserInterface extends FlagParserInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
final class FlagParserRegistry implements FlagParserInterface
{
use NotClonableTrait;

/**
* @var ChainableFlagParserInterface[]
*/
Expand All @@ -34,9 +34,9 @@ final class FlagParserRegistry implements FlagParserInterface
public function __construct(array $parsers)
{
$this->parsers = (
function (ChainableFlagParserInterface ...$parsers) {
return $parsers;
}
function (ChainableFlagParserInterface ...$parsers) {
return $parsers;
}
)(...$parsers);
}

Expand All @@ -51,11 +51,6 @@ public function parse(string $element): FlagBag
}
}

throw new FlagParserNotFoundException(
sprintf(
'No suitable flag parser found to handle the element "%s".',
$element
)
);
throw FlagParserNotFoundException::create($element);
}
}
Loading

0 comments on commit f961428

Please sign in to comment.