-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCountryFieldHandler.php
More file actions
53 lines (42 loc) · 1.56 KB
/
CountryFieldHandler.php
File metadata and controls
53 lines (42 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
declare(strict_types=1);
namespace Netgen\InformationCollection\Core\Persistence\FieldHandler\Custom;
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition;
use Ibexa\Core\FieldType\Country\Value as CountryValue;
use Ibexa\Core\FieldType\Country\Type as CountryType;
use Ibexa\Core\FieldType\Value;
use Netgen\InformationCollection\API\FieldHandler\CustomLegacyFieldHandlerInterface;
use Netgen\InformationCollection\API\Value\Legacy\FieldValue;
use function array_column;
use function implode;
/**
* Overrides the original country handler to save country alpha2 code to collected info
* attribute instead of the country name.
*/
final class CountryFieldHandler implements CustomLegacyFieldHandlerInterface
{
private CountryType $countryType;
public function __construct(CountryType $countryType)
{
$this->countryType = $countryType;
}
public function supports(Value $value): bool
{
return $value instanceof CountryValue;
}
public function toString(Value $value, FieldDefinition $fieldDefinition): string
{
return (string) $value;
}
/**
* @param \Ibexa\Core\FieldType\Country\Value $value
*/
public function getLegacyValue(Value $value, FieldDefinition $fieldDefinition): FieldValue
{
return FieldValue::withStringValue($fieldDefinition->id, implode(', ', array_column($value->countries, 'Alpha2')));
}
public function fromLegacyValue(FieldValue $legacyData)
{
return $this->countryType->fromHash(explode(', ', $legacyData->getDataText()));
}
}