Passed
Pull Request — master (#225)
by
unknown
08:43
created

EzCountry::findCountryKeyByValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 15
rs 10
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\FieldHandler;
4
5
use eZ\Publish\API\Repository\ContentTypeService;
6
use eZ\Publish\Core\FieldType\Country\Value as CountryValue;
7
use eZ\Publish\Core\FieldType\Selection\Value as SelectionValue;
8
use Kaliop\eZMigrationBundle\API\FieldValueImporterInterface;
9
10
class EzCountry extends AbstractFieldHandler implements FieldValueImporterInterface
11
{
12
    private $contentTypeService;
13
14
    private $countries;
15
16
    public function __construct(ContentTypeService $contentTypeService, array $countries)
17
    {
18
        $this->contentTypeService = $contentTypeService;
19
        $this->countries = $countries;
20
    }
21
22
    /**
23
     * Creates a value object to use as the field value when setting a country field type.
24
     *
25
     * @param $fieldValue string|string[] should work
26
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
27
     * @return CountryValue
28
     */
29
    public function hashToFieldValue($fieldValue, array $context = array())
30
    {
31
        if ($fieldValue === null) {
32
            return new CountryValue();
33
        }
34
35
        $fieldSettings = $this->loadFieldSettings($context['contentTypeIdentifier'], $context['fieldIdentifier']);
36
37
        $countries = [];
38
        if (!$fieldSettings['isMultiple'] && is_string($fieldValue)) {
39
            $key = $this->findCountryKeyByValue($this->countries, $fieldValue);
40
            $countryKey = array_keys($this->countries)[$key];
41
            $countries = [$fieldValue => $this->countries[$countryKey]];
42
            return new CountryValue($countries);
43
        }
44
45
        if (is_string($fieldValue)) {
46
            $fieldValue = array($fieldValue);
47
        }
48
49
        foreach ($fieldValue as $val) {
50
            $key = $this->findCountryKeyByValue($this->countries, $val);
51
            $countryKey = array_keys($this->countries)[$key];
52
            $countries[$countryKey] = $this->countries[$countryKey];
53
        }
54
55
        return new CountryValue($countries);
56
    }
57
58
    private function findCountryKeyByValue($countries, $value)
59
    {
60
        if (is_numeric($value)) {
61
            return array_search($value, array_column($countries, 'IDC'));
62
        }
63
64
        if (strlen($value) === 2) {
65
            return array_search($value, array_column($countries, 'Alpha2'));
66
        }
67
68
        if (strlen($value) === 3) {
69
            return array_search($value, array_column($countries, 'Alpha3'));
70
        }
71
72
        return array_search($value, array_column($countries, 'Name'));
73
    }
74
75
    /**
76
     * @param string $contentTypeIdentifier
77
     * @param string $fieldIdentifier
78
     * @return mixed
79
     */
80
    protected function loadFieldSettings($contentTypeIdentifier, $fieldIdentifier)
81
    {
82
        $contentType = $this->contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier);
83
        $fieldDefinition = $contentType->fieldDefinitionsByIdentifier[$fieldIdentifier];
84
        return $fieldDefinition->fieldSettings;
85
    }
86
}
87