Skip to content
This repository was archived by the owner on Jun 8, 2020. It is now read-only.
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
2 changes: 1 addition & 1 deletion features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function iRunBehat($argumentsString = '')
$argumentsString,
'--lang=en',
'--no-colors',
'--format-settings=\'{"timer": false}\''
'--format-settings=\'{"timer": false}\'',
], __DIR__.'/../../tests/app');
$this->process->run();
}
Expand Down
38 changes: 38 additions & 0 deletions src/Populator/Guesser/JsonGuesser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the API Extension project.
*
* (c) Vincent Chalamon <vincentchalamon@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiExtension\Populator\Guesser;

use Doctrine\DBAL\Types\Type;

/**
* @author Mathieu Dewet <mathieu.dewet@gmail.com>
*/
class JsonGuesser extends AbstractGuesser implements GuesserAwareInterface
{
use GuesserAwareTrait;

public function supports(array $mapping): bool
{
$class_name = Type::class;
$constant = "$class_name::JSON";
$constant_value = \defined($constant) ? $constant : null;

return \in_array($mapping['type'], (null !== $constant_value) ? [Type::JSON, Type::JSON_ARRAY] : [Type::JSON_ARRAY], true);
}

public function getValue(array $mapping): array
{
return $this->guesser->getValue(['type' => Type::TARRAY]);
}
}
9 changes: 9 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@
<tag name="coop_tilleuls.api_extension.guesser" />
</service>

<service id="ApiExtension\Populator\Guesser\JsonGuesser" class="ApiExtension\Populator\Guesser\JsonChain" public="false" parent="ApiExtension\Populator\Guesser\AbstractGuesser">
<argument type="service" id="Faker\Generator" />
<argument type="collection" />
</service>

<service id="ApiExtension\Populator\Guesser\GuesserChain" class="ApiExtension\Populator\Guesser\GuesserChain" public="true">
<argument type="service" id="Faker\Generator" />
<argument type="collection" />
Expand Down Expand Up @@ -211,6 +216,10 @@
<tag name="coop_tilleuls.api_extension.transformer" />
</service>

<service id="ApiExtension\Transformer\JsonTransformer" class="ApiExtension\Transformer\JsonTransformer" public="false">
<tag name="coop_tilleuls.api_extension.transformer" />
</service>

<service id="ApiExtension\Transformer\TransformerChain" class="ApiExtension\Transformer\TransformerChain" public="true">
<argument type="collection" />
</service>
Expand Down
39 changes: 39 additions & 0 deletions src/Transformer/JsonTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the API Extension project.
*
* (c) Vincent Chalamon <vincentchalamon@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiExtension\Transformer;

use Doctrine\DBAL\Types\Type;

/**
* @author Mathieu Dewet <mathieu.dewet@gmail.com>
*/
final class JsonTransformer implements TransformerInterface
{
public function supports(array $mapping, $value): bool
{
$typeClass = Type::class;

return \in_array($mapping['type'], \defined("$typeClass::JSON") ? [Type::JSON, Type::JSON_ARRAY] : [Type::JSON_ARRAY], true) && \is_string($value);
}

public function toObject(array $mapping, $value): array
{
return \json_decode($value, true);
}

public function toScalar(array $mapping, $value): array
{
return $this->toObject($mapping, $value);
}
}
2 changes: 1 addition & 1 deletion tests/app/Entity/Beer.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Beer
private $volume;

/**
* @var boolean
* @var bool
* @ORM\Column(type="boolean", name="is_active")
* @Groups({"beer_read", "beer_write"})
*/
Expand Down