Skip to content

Commit

Permalink
Returned all errors at a time, #9
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Thinardon committed Mar 6, 2019
1 parent 910e43c commit 406074b
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 68 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,25 @@ condition:
- c
```

#### Get all errors mapping at a time

If you want to return all errors at a time you can set the next parameters in config

```yaml
# mapping.yaml
transformer:
parameters:
show_all_errors: true
mapping:
title:
from: 'title'
to: 'title'
participants_1_civility:
from: 'user.civility'
to: 'participants.0.civility'
participants_1_name:
from: 'user.lastname'
to: 'participants.0.name'
```


3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
},
"require": {
"php": "^5.6|>=7.0.8",
"symfony/property-access": "^3.4| ^4.0"
"symfony/property-access": "^3.4| ^4.0",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
Expand Down
33 changes: 30 additions & 3 deletions src/AbstractMappingTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Biig\Optimus;

use Biig\Optimus\Exception\JsonOptimusException;
use Biig\Optimus\Exception\OptimusException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
Expand Down Expand Up @@ -40,16 +41,32 @@ abstract public function transform(array $data);
protected function transformFromMapping(array $mapping, array $data)
{
$result = [];
$errors = [];
$returnAllErrors = false;

foreach ($mapping as $node) {
if (array_key_exists('parameters', $mapping) && array_key_exists('show_all_errors', $mapping['parameters'])) {
$returnAllErrors = boolval($mapping['parameters']['show_all_errors']);
}

if (!array_key_exists('mapping', $mapping)) {
throw new OptimusException('mapping key is missing.');
}

foreach ($mapping['mapping'] as $node) {
// Only transform current node if no condition prevent it
if ($this->areConditionsValidated($node, $data)) {
// Get our node value
$nodeValue = $this->getNodeValue($node, $data);

// If we didn't get value but field was required, throw OptimusException
if (isset($node['required']) && true === $node['required'] && null === $nodeValue) {
throw new OptimusException('Field ' . $node['from'] . ' required.');
$message = 'Field ' . $node['from'] . ' required.';
if (!$returnAllErrors) {
throw new OptimusException($message);
} else {
$errors[] = $message;
continue;
}
}

if (isset($node['dependencies'])) {
Expand All @@ -62,7 +79,13 @@ protected function transformFromMapping(array $mapping, array $data)
}

if ($dependenciesExist && null === $nodeValue) {
throw new OptimusException('Field ' . $node['from'] . ' required if dependencies true.');
$message = 'Field ' . $node['from'] . ' required if dependencies true.';
if (!$returnAllErrors) {
throw new OptimusException($message);
} else {
$errors[] = $message;
continue;
}
}
}

Expand All @@ -73,6 +96,10 @@ protected function transformFromMapping(array $mapping, array $data)
}
}

if ($returnAllErrors && !empty($errors)) {
throw new JsonOptimusException($errors);
}

return $result;
}

Expand Down
11 changes: 11 additions & 0 deletions src/Exception/JsonOptimusException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Biig\Optimus\Exception;

class JsonOptimusException extends OptimusException
{
public function __construct(array $message = [], $code = 0, \Throwable $previous = null)
{
parent::__construct(json_encode($message), $code, $previous);
}
}
194 changes: 130 additions & 64 deletions tests/AbstractMappingTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ class AbstractMappingTransformerTest extends TestCase
public function testTransformFromTo()
{
$mapping = [
'node' => [
'from' => 'foo',
'to' => 'bar',
],
'mapping' => [
'node' => [
'from' => 'foo',
'to' => 'bar',
],
]
];
$data = [
'foo' => 'baz',
Expand All @@ -32,10 +34,12 @@ public function testTransformFromTo()
public function testTransformFromToWithDepth()
{
$mapping = [
'node' => [
'from' => 'foo1.foo2',
'to' => 'bar1.bar2.bar3',
],
'mapping' => [
'node' => [
'from' => 'foo1.foo2',
'to' => 'bar1.bar2.bar3',
],
]
];
$data = [
'foo1' => ['foo2' => 'foo']
Expand All @@ -53,13 +57,15 @@ public function testTransformFromToWithDepth()
public function testTransformToFunction()
{
$mapping = [
'node' => [
'to' => 'bar',
'function' => [
'name' => 'getString',
'params' => ['foo']
]
],
'mapping' => [
'node' => [
'to' => 'bar',
'function' => [
'name' => 'getString',
'params' => ['foo']
]
],
]
];
$data = [
'foo' => 'baz',
Expand All @@ -77,11 +83,13 @@ public function testTransformToFunction()
public function testTransformFromToDefaultConstant()
{
$mapping = [
'node' => [
'from' => 'foo',
'to' => 'bar',
'default' => 'default',
],
'mapping' => [
'node' => [
'from' => 'foo',
'to' => 'bar',
'default' => 'default',
],
]
];
$data = [];
$expected = [
Expand All @@ -97,15 +105,17 @@ public function testTransformFromToDefaultConstant()
public function testTransformFromToDefaultFunction()
{
$mapping = [
'node' => [
'to' => 'bar',
'default' => [
'function' => [
'name' => 'getString',
'params' => ['foo']
'mapping' => [
'node' => [
'to' => 'bar',
'default' => [
'function' => [
'name' => 'getString',
'params' => ['foo']
],
],
],
],
]
];
$data = [
'foo' => 'I am a string'
Expand All @@ -127,11 +137,13 @@ public function testTransformFromToDefaultFunction()
public function testItThrowAnException()
{
$mapping = [
'node' => [
'from' => 'foo',
'to' => 'bar',
'required' => true,
],
'mapping' => [
'node' => [
'from' => 'foo',
'to' => 'bar',
'required' => true,
],
]
];

$transformer = new ProxyDummyTransformer();
Expand All @@ -141,13 +153,15 @@ public function testItThrowAnException()
public function testExistsCondition()
{
$mapping = [
'node' => [
'from' => 'foo1.foo2',
'to' => 'bar1.bar2.bar3',
'condition' => [
'exists' => 'baz'
]
],
'mapping' => [
'node' => [
'from' => 'foo1.foo2',
'to' => 'bar1.bar2.bar3',
'condition' => [
'exists' => 'baz'
]
],
]
];

$data = [
Expand All @@ -168,13 +182,15 @@ public function testExistsCondition()
public function testNotValidatedCondition()
{
$mapping = [
'node' => [
'from' => 'foo1.foo2',
'to' => 'bar1.bar2.bar3',
'condition' => [
'exists' => 'baz'
]
],
'mapping' => [
'node' => [
'from' => 'foo1.foo2',
'to' => 'bar1.bar2.bar3',
'condition' => [
'exists' => 'baz'
]
],
]
];

$data = [
Expand All @@ -192,16 +208,18 @@ public function testNotValidatedCondition()
public function testArrayCondition()
{
$mapping = [
'node' => [
'from' => 'foo1.foo2',
'to' => 'bar1.bar2.bar3',
'condition' => [
'exists' => [
'baz',
'boz'
'mapping' => [
'node' => [
'from' => 'foo1.foo2',
'to' => 'bar1.bar2.bar3',
'condition' => [
'exists' => [
'baz',
'boz'
]
]
]
],
],
]
];

$data = [
Expand All @@ -223,16 +241,18 @@ public function testArrayCondition()
public function testNotValidatedArrayCondition()
{
$mapping = [
'node' => [
'from' => 'foo1.foo2',
'to' => 'bar1.bar2.bar3',
'condition' => [
'exists' => [
'baz',
'boz'
'mapping' => [
'node' => [
'from' => 'foo1.foo2',
'to' => 'bar1.bar2.bar3',
'condition' => [
'exists' => [
'baz',
'boz'
]
]
]
],
],
]
];

$data = [
Expand All @@ -247,6 +267,52 @@ public function testNotValidatedArrayCondition()

$this->assertEquals($expected, $result);
}

/**
* @expectedException \Biig\Optimus\Exception\JsonOptimusException
* @expectedExceptionMessage ["Field foo required.","Field bar required."]
*/
public function testItThrowAnJsonOptimusException()
{
$mapping = [
'parameters' => [
'show_all_errors' => true
],
'mapping' => [
'node' => [
'from' => 'foo',
'to' => 'bar',
'required' => true,
],
'node2' => [
'from' => 'bar',
'to' => 'foo',
'required' => true,
],
]
];

$transformer = new ProxyDummyTransformer();
$transformer->transformFromMapping($mapping, []);
}

/**
* @expectedException \Biig\Optimus\Exception\OptimusException
* @expectedExceptionMessage mapping key is missing.
*/
public function testItThrowAnOptimusExceptionOnMissingMappingKey()
{
$mapping = [
'node' => [
'from' => 'foo',
'to' => 'bar',
'required' => true,
]
];

$transformer = new ProxyDummyTransformer();
$transformer->transformFromMapping($mapping, []);
}
}

class ProxyDummyTransformer extends AbstractMappingTransformer
Expand Down

0 comments on commit 406074b

Please sign in to comment.