Completed
Push — master ( c0f76f...37333c )
by Jeroen De
299:55 queued 234:55
created

ConstraintViolationListMapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Validation;
6
7
use Symfony\Component\PropertyAccess\PropertyAccess;
8
use Symfony\Component\Validator\ConstraintViolationInterface;
9
use Symfony\Component\Validator\ConstraintViolationListInterface;
10
11
/**
12
 * Maps a list of ConstraintViolations into an array of messages grouped by the property causing them
13
 */
14
class ConstraintViolationListMapper {
15
16
	private $propertyAccessor;
17
18
	public function __construct() {
19
		$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
20
	}
21
22
	public function map( ConstraintViolationListInterface $violations ): array {
23
		$errors = [];
24
25
		foreach ( $violations as $violation ) {
26
			/** @var ConstraintViolationInterface $violation */
27
			$entryErrors = (array) $this->propertyAccessor->getValue( $errors, $violation->getPropertyPath() );
28
			$entryErrors[] = $violation->getMessage();
29
			$this->propertyAccessor->setValue( $errors, $violation->getPropertyPath(), $entryErrors );
30
		}
31
32
		return $errors;
33
	}
34
}
35