Passed
Push — master ( 3456a7...4e79a2 )
by Alexander
02:34
created

TranslatedAttributesHandlerTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 28
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getTranslatedAttributes() 0 13 2
A getFormattedAttributesString() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\Trait;
6
7
use Yiisoft\Validator\ValidationContext;
8
9
trait TranslatedAttributesHandlerTrait
10
{
11
    /**
12
     * @param string[] $attributes
13
     */
14
    private function getFormattedAttributesString(array $attributes, ValidationContext $context): string
15
    {
16
        return '"' . implode('", "', $this->getTranslatedAttributes($attributes, $context)) . '"';
17
    }
18
19
    /**
20
     * @param string[] $attributes
21
     *
22
     * @return string[]
23
     */
24
    private function getTranslatedAttributes(array $attributes, ValidationContext $context): array
25
    {
26
        $initialAttribute = $context->getAttribute();
27
        $translatedAttributes = [];
28
        foreach ($attributes as $attribute) {
29
            $translatedAttributes[] = $context->setAttribute($attribute)->getTranslatedAttribute();
30
        }
31
32
        /** @var string[] $translatedAttributes */
33
34
        $context->setAttribute($initialAttribute);
35
36
        return $translatedAttributes;
37
    }
38
}
39