forked from ezsystems/ezplatform-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EzImageAsset.php
80 lines (69 loc) · 2.57 KB
/
EzImageAsset.php
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
namespace AppBundle\Migration\FieldHandler;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
use eZ\Publish\Core\FieldType\ImageAsset\Value as ImageValue;
use eZ\Publish\Core\FieldType\Value as BaseValue;
use Kaliop\eZMigrationBundle\API\FieldValueConverterInterface;
use Kaliop\eZMigrationBundle\Core\FieldHandler\AbstractFieldHandler;
use Kaliop\eZMigrationBundle\Core\ReferenceResolver\PrefixBasedResolverInterface;
use LogicException;
class EzImageAsset extends AbstractFieldHandler implements FieldValueConverterInterface
{
protected $resolver;
public function __construct(
PrefixBasedResolverInterface $resolver
) {
$this->resolver = $resolver;
}
/**
* @inheritdoc
*/
public function hashToFieldValue($fieldValue, array $context = array())
{
if (empty($fieldValue['destinationContentId']) || !is_scalar($fieldValue['destinationContentId'])) {
throw new LogicException('Definition of EzImageAsset field is incorrect: destinationContentId is missing');
}
if (!isset($fieldValue['alternativeText'])) {
$fieldValue['alternativeText'] = null;
}
return new ImageValue(
(int)$this->resolver->resolveReference($fieldValue['destinationContentId']),
$fieldValue['alternativeText']
);
}
/**
* @inheritdoc
*/
public function fieldValueToHash($fieldValue, array $context = array())
{
$this->checkFieldValueStructure($fieldValue);
return [
'destinationContentId' => $fieldValue->destinationContentId,
'alternativeText' => $fieldValue->alternativeText,
];
}
/**
* Throws an exception if value structure is not of expected format.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the value does not match the expected structure.
*
* @param \eZ\Publish\Core\FieldType\Relation\Value $value
*/
protected function checkFieldValueStructure(BaseValue $value)
{
if (!is_int($value->destinationContentId) && !is_string($value->destinationContentId)) {
throw new InvalidArgumentType(
'$value->destinationContentId',
'string|int',
$value->destinationContentId
);
}
if ($value->alternativeText !== null && !is_string($value->alternativeText)) {
throw new InvalidArgumentType(
'$value->alternativeText',
'null|string',
$value->alternativeText
);
}
}
}