TypedEntity.php 4.65 KB
Newer Older
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Contains \Drupal\typed_entity\TypedEntity\TypedEntity.
 */

namespace Drupal\typed_entity\TypedEntity;

10
11
use Drupal\typed_entity\Exception\TypedEntityException;

Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
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
class TypedEntity implements TypedEntityInterface {

  /**
   * The entity ID.
   *
   * @var int
   */
  protected $entityId;

  /**
   * The fully loaded entity.
   *
   * @var object
   */
  protected $entity;

  /**
   * The entity type.
   *
   * @var string
   */
  protected $entityType;

  /**
   * The entity bundle.
   *
   * @var string
   */
  protected $bundle;

  /**
   * The EMW
   *
   * @var \EntityDrupalWrapper
   */
  protected $wrapper;

  /**
   * Constructs a TypedEntity object.
   *
   * @param string $entity_type
   *   The type of the entity.
54
55
56
57
58
59
60
61
62
   * @param int $entity_id
   *   The entity ID.
   * @param object $entity
   *   The fully loaded entity.
   * @param string $bundle
   *   (Optional). Allow forcing a bundle. This is mainly for unit testing.
   *
   * @throws \Drupal\typed_entity\Exception\TypedEntityException
   * @throws \EntityMalformedException
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
63
   */
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
64
  public function __construct($entity_type, $entity_id = NULL, $entity = NULL, $bundle = NULL) {
65
66
    if (empty($entity_type)) {
      throw new TypedEntityException('You need to provide the entity type for the TypedEntity.');
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
67
    }
68
69
70
71
    if (empty($entity_id) && empty($entity)) {
      throw new TypedEntityException('You need to provide the fully loaded entity or the entity ID.');
    }
    $this->entityType = $entity_type;
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
72
73
    $this->entityId = $entity_id;
    $this->entity = $entity;
74
    $this->bundle = $bundle;
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
75
76
77
78
79
80
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityId() {
81
82
83
84
85
86
87
    if (isset($this->entityId)) {
      return $this->entityId;
    }
    if (empty($this->entity)) {
      // This means that somehow we do not have neither entity nor entity ID.
      throw new TypedEntityException('You need to provide the fully loaded entity or the entity ID.');
    }
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
88
89
90
    list($entity_id, , $bundle) = entity_extract_ids($this->getEntityType(), $this->entity);
    $this->entityId = $entity_id;
    $this->bundle = $bundle;
91

Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
92
93
94
95
96
97
98
99
100
101
    return $this->entityId;
  }

  /**
   * {@inheritdoc}
   */
  public function getEntity() {
    if (isset($this->entity)) {
      return $this->entity;
    }
102
103
    $entity_id = $this->getEntityId();
    if (empty($entity_id)) {
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
104
105
106
      // We do not have neither entity nor ID. We cannot load.
      return NULL;
    }
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
107
108
    $entities = entity_load($this->getEntityType(), array($this->getEntityId()));
    $this->entity = isset($entities[$this->getEntityId()]) ? $entities[$this->getEntityId()] : NULL;
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    return $this->entity;
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityType() {
    return $this->entityType;
  }

  /**
   * {@inheritdoc}
   */
  public function getBundle() {
    if (isset($this->bundle)) {
      return $this->bundle;
    }

Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
127
128
129
    list($entity_id, , $bundle) = entity_extract_ids($this->getEntityType(), $this->getEntity());
    $this->entityId = $entity_id;
    $this->bundle = $bundle;
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
130
131
132
133
134
135
136
137
138
139
    return $this->bundle;
  }

  /**
   * {@inheritdoc}
   */
  public function getWrapper() {
    if (isset($this->wrapper)) {
      return $this->wrapper;
    }
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
140
    $this->wrapper = entity_metadata_wrapper($this->getEntityType(), $this->getEntity());
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
141
    return $this->wrapper;
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
  }

  /**
   * {@inheritdoc}
   */
  public function save() {
    return $this->getWrapper()->save();
  }

  /**
   * {@inheritdoc}
   */
  public function access($op, $account = NULL) {
    return $this->getWrapper()->access($op, $account);
  }

  /**
   * Implements the magic methods to proxy property access to the entity.
   *
   * @param string $property_name
   *   The name of the property being accessed.
   *
   * @return mixed
   *   The property value.
   */
  public function __get($property_name) {
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
168
    $entity = $this->getEntity();
169
    return isset($entity->{$property_name}) ? $entity->{$property_name} : NULL;
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
  }

  /**
   * Implements the magic methods to proxy property access to the entity.
   *
   * @param string $property_name
   *   The name of the property being accessed.
   * @param mixed $value
   *   The property value.
   */
  public function __set($property_name, $value) {
    $this->getEntity()->{$property_name} = $value;
  }

Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
184
185
186
  /**
   * Implements the magic methods to proxy methods to the entity.
   *
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
187
188
189
190
191
   * @param string $name
   *   The method name.
   * @param array $arguments
   *   The arguments.
   *
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
192
193
194
   * @return mixed
   *   The value returned by the entity method.
   */
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
195
196
  public function __call($name, array $arguments) {
    $callable = array($this->getEntity(), $name);
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
197
    if (is_callable($callable)) {
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
198
      return call_user_func_array($callable, $arguments);
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
199
200
201
202
    }
    return NULL;
  }

Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
203
}