Completed
Pull Request — master (#16)
by Sergii
03:45
created

EntityDrupalWrapper::hasField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 2
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Utils;
6
7
final class EntityDrupalWrapper
8
{
9
    /**
10
     * Entity type.
11
     *
12
     * @var string
13
     */
14
    private $type = '';
15
    /**
16
     * Entity bundle.
17
     *
18
     * @var string
19
     */
20
    private $bundle = '';
21
    /**
22
     * Entity object.
23
     *
24
     * @var object
25
     */
26
    private $entity;
27
    /**
28
     * @var array
29
     */
30
    private $fields = [
31
        'locators' => [],
32
        'required' => [],
33
    ];
34
35
    /**
36
     * @param string $entityType
37
     * @param string $bundle
38
     */
39
    public function __construct($entityType, $bundle = '')
40
    {
41
        $this->type = $entityType;
42
43
        if (empty($bundle)) {
44
            $this->bundle = $this->type;
45
        }
46
47
        // The fields in "locators" array stored by machine name of a field and duplicated by field label.
48
        foreach (\DrupalKernelPlaceholder::getFieldDefinitions($this->type, $this->bundle) as $name => $definition) {
49
            $this->fields['locators'][$definition['label']] = $this->fields['locators'][$name] = $name;
50
51
            if ($definition['required']) {
52
                $this->fields['required'][$name] = $definition['label'];
53
            }
54
        }
55
    }
56
57
    public function load($id)
58
    {
59
        if (null === $this->entity) {
60
            $this->entity = \DrupalKernelPlaceholder::entityLoad($this->type, $id);
61
        }
62
63
        return $this->entity;
64
    }
65
66
    public function hasField($fieldName)
67
    {
68
        return \DrupalKernelPlaceholder::entityHasField(
69
            $this->getEntity(),
70
            $this->getFieldNameByLocator($fieldName)
71
        );
72
    }
73
74
    public function getFieldValue($fieldName)
75
    {
76
        return \DrupalKernelPlaceholder::entityFieldValue(
77
            $this->getEntity(),
78
            $this->getFieldNameByLocator($fieldName)
79
        );
80
    }
81
82
    /**
83
     * @param string $field_name
84
     *   Machine name or label of a field.
85
     *
86
     * @return string
87
     */
88
    public function getFieldNameByLocator($field_name)
89
    {
90
        return isset($this->fields['locators'][$field_name]) ? $this->fields['locators'][$field_name] : '';
91
    }
92
93
    /**
94
     * @return array[]
95
     */
96
    public function getRequiredFields()
97
    {
98
        return $this->fields['required'];
99
    }
100
101
    /**
102
     * @return object
103
     */
104
    protected function getEntity()
105
    {
106
        if (null === $this->entity) {
107
            throw new \RuntimeException('You have to load an entity before getting it.');
108
        }
109
110
        return $this->entity;
111
    }
112
}
113