-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathHandler.php
More file actions
106 lines (87 loc) · 3.14 KB
/
Handler.php
File metadata and controls
106 lines (87 loc) · 3.14 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
declare(strict_types=1);
namespace Netgen\Bundle\OpenGraphBundle\Handler\FieldType;
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
use Ibexa\Core\Helper\FieldHelper;
use Netgen\Bundle\OpenGraphBundle\Exception\FieldEmptyException;
use Netgen\Bundle\OpenGraphBundle\Handler\Handler as BaseHandler;
use Netgen\Bundle\OpenGraphBundle\MetaTag\Item;
use function is_array;
abstract class Handler extends BaseHandler
{
public function __construct(
protected FieldHelper $fieldHelper,
) {}
public function getMetaTags(string $tagName, array $params = []): array
{
if (!isset($params[0])) {
throw new InvalidArgumentException(
'$params[0]',
'Field type handlers require at least a field identifier.',
);
}
$fieldIdentifiers = is_array($params[0]) ? $params[0] : [$params[0]];
$fieldValue = $this->getFallbackValue($tagName, $params);
foreach ($fieldIdentifiers as $fieldIdentifier) {
$field = $this->validateField($fieldIdentifier);
try {
$fieldValue = $this->getFieldValue($field, $tagName, $params);
break;
} catch (FieldEmptyException $e) {
// do nothing
}
}
return [
new Item(
$tagName,
$fieldValue,
),
];
}
/**
* Returns the field value, converted to string.
*
* @throws \Netgen\Bundle\OpenGraphBundle\Exception\FieldEmptyException If field is empty
*/
protected function getFieldValue(Field $field, string $tagName, array $params = []): string
{
if (!$this->fieldHelper->isFieldEmpty($this->content, $field->fieldDefIdentifier)) {
return (string) $field->value;
}
throw new FieldEmptyException($field->fieldDefIdentifier);
}
/**
* Returns fallback value.
*/
protected function getFallbackValue(string $tagName, array $params = []): string
{
if (!empty($params[1])) {
return (string) $params[1];
}
return '';
}
/**
* Validates field by field identifier.
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException If field does not exist, or the handler does not support it
*/
protected function validateField(string $fieldIdentifier): Field
{
$field = $this->content->getField($fieldIdentifier);
if (!$field instanceof Field) {
throw new InvalidArgumentException('$params[0]', 'Field \'' . $fieldIdentifier . '\' does not exist in content.');
}
if (!$this->supports($field)) {
throw new InvalidArgumentException(
'$params[0]',
static::class . ' field type handler does not support field with identifier \'' . $field->fieldDefIdentifier . '\'.',
);
}
return $field;
}
/**
* Returns if this field type handler supports current field.
*/
abstract protected function supports(Field $field): bool;
}