Completed
Pull Request — master (#16)
by Sergii
04:26
created

EntityDrupalWrapper::getFieldInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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