Skip to content

Commit 7eda368

Browse files
committed
Switch to monorepo
0 parents  commit 7eda368

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2019 Yann Eugoné
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Yokai Batch
2+
3+
[![Latest Stable Version](https://img.shields.io/packagist/v/yokai/batch-symfony-validator?style=flat-square)](https://packagist.org/packages/yokai/batch-symfony-validator)
4+
[![Downloads Monthly](https://img.shields.io/packagist/dm/yokai/batch-symfony-validator?style=flat-square)](https://packagist.org/packages/yokai/batch-symfony-validator)
5+
6+
Bridge of [`symfony/validator`](https://github.com/symfony/validator) for [Batch](https://github.com/yokai-php/batch-src).
7+
8+
9+
## Documentation
10+
11+
Please refer to [main repository](https://github.com/yokai-php/batch-src) documentation.
12+
13+
14+
## Contribution
15+
16+
Please feel free to open an [issue](https://github.com/yokai-php/batch-src/issues)
17+
or a [pull request](https://github.com/yokai-php/batch-src/pulls)
18+
in the [main repository](https://github.com/yokai-php/batch-src).
19+
20+
21+
## MIT License
22+
23+
License can be found [here](LICENSE).

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "yokai/batch-symfony-validator",
3+
"description": "Bridge of symfony/validator for yokai/batch",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Yann Eugoné",
9+
"email": "eugone.yann@gmail.com"
10+
}
11+
],
12+
"require": {
13+
"php": "^7.2",
14+
"symfony/validator": "^4.4"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"Yokai\\Batch\\Bridge\\Symfony\\Validator\\": "src/"
19+
}
20+
}
21+
}

src/SkipInvalidItemProcessor.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\Batch\Bridge\Symfony\Validator;
6+
7+
use DateTimeInterface;
8+
use Symfony\Component\Validator\ConstraintViolationInterface;
9+
use Symfony\Component\Validator\Validator\ValidatorInterface;
10+
use Yokai\Batch\Job\Item\InvalidItemException;
11+
use Yokai\Batch\Job\Item\ItemProcessorInterface;
12+
13+
final class SkipInvalidItemProcessor implements ItemProcessorInterface
14+
{
15+
/**
16+
* @var ValidatorInterface
17+
*/
18+
private $validator;
19+
20+
/**
21+
* @var array|null
22+
*/
23+
private $groups;
24+
25+
public function __construct(ValidatorInterface $validator, array $groups = null)
26+
{
27+
$this->validator = $validator;
28+
$this->groups = $groups;
29+
}
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
public function process($item)
35+
{
36+
$violations = $this->validator->validate($item, null, $this->groups);
37+
if (count($violations) === 0) {
38+
return $item;
39+
}
40+
41+
$issues = [];
42+
/** @var ConstraintViolationInterface $violation */
43+
foreach ($violations as $violation) {
44+
$issues[] = sprintf(
45+
'%s: %s: %s',
46+
$violation->getPropertyPath(),
47+
$violation->getMessage(),
48+
$this->normalizeInvalidValue($violation->getInvalidValue())
49+
);
50+
}
51+
52+
throw new InvalidItemException(implode(PHP_EOL, $issues));
53+
}
54+
55+
/**
56+
* @param mixed $invalidValue
57+
*
58+
* @return integer|float|string|boolean
59+
*/
60+
private function normalizeInvalidValue($invalidValue)
61+
{
62+
if ($invalidValue === '') {
63+
return '""';
64+
}
65+
if ($invalidValue === null) {
66+
return 'NULL';
67+
}
68+
if (is_scalar($invalidValue)) {
69+
return $invalidValue;
70+
}
71+
72+
if (is_iterable($invalidValue)) {
73+
$invalidValues = [];
74+
foreach ($invalidValue as $value) {
75+
$invalidValues[] = $this->normalizeInvalidValue($value);
76+
}
77+
78+
return implode(', ', $invalidValues);
79+
}
80+
81+
if (is_object($invalidValue)) {
82+
if ($invalidValue instanceof DateTimeInterface) {
83+
return $invalidValue->format(DateTimeInterface::ISO8601);
84+
}
85+
86+
if (method_exists($invalidValue, '__toString')) {
87+
return (string)$invalidValue;
88+
}
89+
90+
return sprintf('%s:%s', get_class($invalidValue), spl_object_hash($invalidValue));
91+
}
92+
93+
return gettype($invalidValue);
94+
}
95+
}

0 commit comments

Comments
 (0)