|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\FieldHandler; |
|
4
|
|
|
|
|
5
|
|
|
use eZ\Publish\Core\FieldType\Selection\Value as SelectionValue; |
|
6
|
|
|
use Kaliop\eZMigrationBundle\API\FieldValueImporterInterface; |
|
7
|
|
|
use Kaliop\eZMigrationBundle\API\FieldDefinitionConverterInterface; |
|
8
|
|
|
use \eZ\Publish\API\Repository\Repository; |
|
9
|
|
|
|
|
10
|
|
|
class EzSelection extends AbstractFieldHandler implements FieldValueImporterInterface, FieldDefinitionConverterInterface |
|
11
|
|
|
{ |
|
12
|
|
|
protected $repository; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct(Repository $repository) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->repository = $repository; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Creates a value object to use as the field value when setting a selection field type. |
|
21
|
|
|
* |
|
22
|
|
|
* @param $fieldValue int|string|string[]|int[] should all work |
|
23
|
|
|
* @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration |
|
24
|
|
|
* @return SelectionValue |
|
25
|
|
|
*/ |
|
26
|
|
|
public function hashToFieldValue($fieldValue, array $context = array()) |
|
27
|
|
|
{ |
|
28
|
|
|
if ($fieldValue === null) { |
|
29
|
|
|
return new SelectionValue(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
// allow user to pass in a single value |
|
33
|
|
|
if (is_string($fieldValue) || is_int($fieldValue)) { |
|
34
|
|
|
$fieldValue = array($fieldValue); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// allow user to pass in selection values by name |
|
38
|
|
|
$fieldSettings = null; |
|
39
|
|
|
foreach($fieldValue as $key => $val) { |
|
|
|
|
|
|
40
|
|
|
if (is_string($val)) { |
|
41
|
|
|
if (ctype_digit($val)) { |
|
42
|
|
|
$fieldValue[$key] = (int)$val; |
|
43
|
|
|
} else { |
|
44
|
|
|
if ($fieldSettings === null) { |
|
45
|
|
|
$fieldSettings = $this->loadFieldSettings($context['contentTypeIdentifier'], $context['fieldIdentifier']); |
|
46
|
|
|
} |
|
47
|
|
|
foreach($fieldSettings['options'] as $pos => $name) { |
|
48
|
|
|
if ($name === $val) { |
|
49
|
|
|
$fieldValue[$key] = $pos; |
|
50
|
|
|
break; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return new SelectionValue($fieldValue); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $contentTypeIdentifier |
|
62
|
|
|
* @param string $fieldIdentifier |
|
63
|
|
|
* @return mixed |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function loadFieldSettings($contentTypeIdentifier, $fieldIdentifier) |
|
66
|
|
|
{ |
|
67
|
|
|
$contentTypeService = $this->repository->getContentTypeService(); |
|
68
|
|
|
$contentType = $contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier); |
|
69
|
|
|
$fieldDefinition = $contentType->fieldDefinitionsByIdentifier[$fieldIdentifier]; |
|
|
|
|
|
|
70
|
|
|
return $fieldDefinition->fieldSettings; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function fieldSettingsToHash($settingsValue, array $context = array()) |
|
74
|
|
|
{ |
|
75
|
|
|
return $settingsValue; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function hashToFieldSettings($settingsHash, array $context = array()) |
|
79
|
|
|
{ |
|
80
|
|
|
foreach ($settingsHash['options'] as $key => $value) { |
|
81
|
|
|
if (!is_int($key) && !ctype_digit($key)) { |
|
82
|
|
|
throw new \Exception("The list of values allowed for an eZSelection field can only use integer keys, found: '$key'"); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
return $settingsHash; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.