3 namespace Drupal\migrate;
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\migrate\Plugin\MigrateIdMapInterface;
14 * The actual values of the source row.
18 protected $source = [];
21 * The source identifiers.
25 protected $sourceIds = [];
28 * The destination values.
32 protected $destination = [];
35 * Level separator of destination and source properties.
37 const PROPERTY_SEPARATOR = '/';
40 * The mapping between source and destination identifiers.
45 'original_hash' => '',
47 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
51 * Whether the source has been frozen already.
53 * Once frozen the source can not be changed any more.
57 protected $frozen = FALSE;
60 * The raw destination properties.
62 * Unlike $destination which is set by using
63 * \Drupal\Component\Utility\NestedArray::setValue() this array contains
64 * the destination as setDestinationProperty was called.
67 * The raw destination.
69 * @see getRawDestination()
71 protected $rawDestination = [];
74 * TRUE when this row is a stub.
78 protected $isStub = FALSE;
81 * The empty destination properties.
85 protected $emptyDestinationProperties = [];
88 * Constructs a \Drupal\Migrate\Row object.
90 * @param array $values
91 * An array of values to add as properties on the object.
92 * @param array $source_ids
93 * An array containing the IDs of the source using the keys as the field
95 * @param bool $is_stub
96 * TRUE if the row being created is a stub.
98 * @throws \InvalidArgumentException
99 * Thrown when a source ID property does not exist.
101 public function __construct(array $values = [], array $source_ids = [], $is_stub = FALSE) {
102 $this->source = $values;
103 $this->sourceIds = $source_ids;
104 $this->isStub = $is_stub;
105 foreach (array_keys($source_ids) as $id) {
106 if (!$this->hasSourceProperty($id)) {
107 throw new \InvalidArgumentException("$id is defined as a source ID but has no value.");
113 * Retrieves the values of the source identifiers.
116 * An array containing the values of the source identifiers. Returns values
117 * in the same order as defined in $this->sourceIds.
119 public function getSourceIdValues() {
120 return array_merge($this->sourceIds, array_intersect_key($this->source, $this->sourceIds));
124 * Determines whether a source has a property.
126 * @param string $property
127 * A property on the source.
130 * TRUE if the source has property; FALSE otherwise.
132 public function hasSourceProperty($property) {
133 return NestedArray::keyExists($this->source, explode(static::PROPERTY_SEPARATOR, $property));
137 * Retrieves a source property.
139 * @param string $property
140 * A property on the source.
143 * The found returned property or NULL if not found.
145 public function getSourceProperty($property) {
146 $return = NestedArray::getValue($this->source, explode(static::PROPERTY_SEPARATOR, $property), $key_exists);
153 * Returns the whole source array.
156 * An array of source plugins.
158 public function getSource() {
159 return $this->source;
163 * Sets a source property.
165 * This can only be called from the source plugin.
167 * @param string $property
168 * A property on the source.
170 * The property value to set on the source.
174 public function setSourceProperty($property, $data) {
176 throw new \Exception("The source is frozen and can't be changed any more");
179 NestedArray::setValue($this->source, explode(static::PROPERTY_SEPARATOR, $property), $data, TRUE);
184 * Freezes the source.
188 public function freezeSource() {
189 $this->frozen = TRUE;
194 * Clones the row with an empty set of destination values.
198 public function cloneWithoutDestination() {
199 return (new static($this->getSource(), $this->sourceIds, $this->isStub()))->freezeSource();
203 * Tests if destination property exists.
205 * @param array|string $property
206 * An array of properties on the destination.
209 * TRUE if the destination property exists.
211 public function hasDestinationProperty($property) {
212 return NestedArray::keyExists($this->destination, explode(static::PROPERTY_SEPARATOR, $property));
216 * Sets destination properties.
218 * @param string $property
219 * The name of the destination property.
220 * @param mixed $value
221 * The property value to set on the destination.
223 public function setDestinationProperty($property, $value) {
224 $this->rawDestination[$property] = $value;
225 NestedArray::setValue($this->destination, explode(static::PROPERTY_SEPARATOR, $property), $value, TRUE);
229 * Removes destination property.
231 * @param string $property
232 * The name of the destination property.
234 public function removeDestinationProperty($property) {
235 unset($this->rawDestination[$property]);
236 NestedArray::unsetValue($this->destination, explode(static::PROPERTY_SEPARATOR, $property));
240 * Sets a destination to be empty.
242 * @param string $property
243 * The destination property.
245 public function setEmptyDestinationProperty($property) {
246 $this->emptyDestinationProperties[] = $property;
250 * Gets the empty destination properties.
253 * An array of destination properties.
255 public function getEmptyDestinationProperties() {
256 return $this->emptyDestinationProperties;
260 * Returns the whole destination array.
263 * An array of destination values.
265 public function getDestination() {
266 return $this->destination;
270 * Returns the raw destination. Rarely necessary.
272 * For example calling setDestination('foo/bar', 'baz') results in
274 * $this->destination['foo']['bar'] = 'baz';
275 * $this->rawDestination['foo/bar'] = 'baz';
279 * The raw destination values.
281 public function getRawDestination() {
282 return $this->rawDestination;
286 * Returns the value of a destination property.
288 * @param string $property
289 * The name of a property on the destination.
292 * The destination value.
294 public function getDestinationProperty($property) {
295 return NestedArray::getValue($this->destination, explode(static::PROPERTY_SEPARATOR, $property));
299 * Sets the Migrate ID mappings.
301 * @param array $id_map
302 * An array of mappings between source ID and destination ID.
304 public function setIdMap(array $id_map) {
305 $this->idMap = $id_map;
309 * Retrieves the Migrate ID mappings.
312 * An array of mapping between source and destination identifiers.
314 public function getIdMap() {
319 * Recalculates the hash for the row.
321 public function rehash() {
322 $this->idMap['original_hash'] = $this->idMap['hash'];
323 $this->idMap['hash'] = hash('sha256', serialize($this->source));
327 * Checks whether the row has changed compared to the original ID map.
330 * TRUE if the row has changed, FALSE otherwise. If setIdMap() was not
331 * called, this always returns FALSE.
333 public function changed() {
334 return $this->idMap['original_hash'] != $this->idMap['hash'];
338 * Returns if this row needs an update.
341 * TRUE if the row needs updating, FALSE otherwise.
343 public function needsUpdate() {
344 return $this->idMap['source_row_status'] == MigrateIdMapInterface::STATUS_NEEDS_UPDATE;
348 * Returns the hash for the source values..
351 * The hash of the source values.
353 public function getHash() {
354 return $this->idMap['hash'];
358 * Reports whether this row is a stub.
361 * The current stub value.
363 public function isStub() {
364 return $this->isStub;