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

ConstraintViolationListMapper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 21
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A map() 0 12 2
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