Row.php 7.93 KB
Newer Older
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
<?php

/**
 * @file
 * Contains \Drupal\migrate\Row.
 */

namespace Drupal\migrate;

use Drupal\Component\Utility\NestedArray;
use Drupal\migrate\Plugin\MigrateIdMapInterface;

/**
 * Stores a row.
 */
class Row {

  /**
   * The actual values of the source row.
   *
   * @var array
   */
  protected $source = array();

  /**
   * The source identifiers.
   *
   * @var array
   */
  protected $sourceIds = array();

  /**
   * The destination values.
   *
   * @var array
   */
  protected $destination = array();

39 40 41 42 43
  /**
   * Level separator of destination and source properties.
   */
  const PROPERTY_SEPARATOR = '/';

44 45 46 47 48 49 50 51
  /**
   * The mapping between source and destination identifiers.
   *
   * @var array
   */
  protected $idMap = array(
    'original_hash' => '',
    'hash' => '',
52
    'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
53 54 55 56 57 58 59 60 61 62 63
  );

  /**
   * Whether the source has been frozen already.
   *
   * Once frozen the source can not be changed any more.
   *
   * @var bool
   */
  protected $frozen = FALSE;

64 65 66 67 68 69 70 71 72 73 74 75
  /**
   * The raw destination properties.
   *
   * Unlike $destination which is set by using
   * \Drupal\Component\Utility\NestedArray::setValue() this array contains
   * the destination as setDestinationProperty was called.
   *
   * @var array
   *   The raw destination.
   *
   * @see getRawDestination()
   */
76
  protected $rawDestination = [];
77 78 79 80 81 82

  /**
   * TRUE when this row is a stub.
   *
   * @var bool
   */
83
  protected $isStub = FALSE;
84

85 86 87 88 89 90 91 92
  /**
   * Constructs a \Drupal\Migrate\Row object.
   *
   * @param array $values
   *   An array of values to add as properties on the object.
   * @param array $source_ids
   *   An array containing the IDs of the source using the keys as the field
   *   names.
93 94
   * @param bool $is_stub
   *   TRUE if the row being created is a stub.
95 96 97 98
   *
   * @throws \InvalidArgumentException
   *   Thrown when a source ID property does not exist.
   */
99
  public function __construct(array $values, array $source_ids, $is_stub = FALSE) {
100 101
    $this->source = $values;
    $this->sourceIds = $source_ids;
102
    $this->isStub = $is_stub;
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    foreach (array_keys($source_ids) as $id) {
      if (!$this->hasSourceProperty($id)) {
        throw new \InvalidArgumentException("$id has no value");
      }
    }
  }

  /**
   * Retrieves the values of the source identifiers.
   *
   * @return array
   *   An array containing the values of the source identifiers.
   */
  public function getSourceIdValues() {
    return array_intersect_key($this->source, $this->sourceIds);
  }

  /**
   * Determines whether a source has a property.
   *
   * @param string $property
   *   A property on the source.
   *
   * @return bool
   *   TRUE if the source has property; FALSE otherwise.
   */
  public function hasSourceProperty($property) {
130
    return NestedArray::keyExists($this->source, explode(static::PROPERTY_SEPARATOR, $property));
131 132 133 134 135 136 137 138 139 140 141 142
  }

  /**
   * Retrieves a source property.
   *
   * @param string $property
   *   A property on the source.
   *
   * @return mixed|null
   *   The found returned property or NULL if not found.
   */
  public function getSourceProperty($property) {
143
    $return = NestedArray::getValue($this->source, explode(static::PROPERTY_SEPARATOR, $property), $key_exists);
144 145
    if ($key_exists) {
      return $return;
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    }
  }

  /**
   * Returns the whole source array.
   *
   * @return array
   *   An array of source plugins.
   */
  public function getSource() {
    return $this->source;
  }

  /**
   * Sets a source property.
   *
   * This can only be called from the source plugin.
   *
   * @param string $property
   *   A property on the source.
   * @param mixed $data
   *   The property value to set on the source.
   *
   * @throws \Exception
   */
  public function setSourceProperty($property, $data) {
    if ($this->frozen) {
      throw new \Exception("The source is frozen and can't be changed any more");
    }
    else {
176
      NestedArray::setValue($this->source, explode(static::PROPERTY_SEPARATOR, $property), $data, TRUE);
177 178 179 180 181
    }
  }

  /**
   * Freezes the source.
182 183
   *
   * @return $this
184 185 186
   */
  public function freezeSource() {
    $this->frozen = TRUE;
187
    return $this;
188 189
  }

190 191 192 193 194 195 196 197 198
  /**
   * Clones the row with an empty set of destination values.
   *
   * @return static
   */
  public function cloneWithoutDestination() {
    return (new static($this->getSource(), $this->sourceIds, $this->isStub()))->freezeSource();
  }

199 200 201 202 203 204
  /**
   * Tests if destination property exists.
   *
   * @param array|string $property
   *   An array of properties on the destination.
   *
205
   * @return bool
206 207 208
   *   TRUE if the destination property exists.
   */
  public function hasDestinationProperty($property) {
209
    return NestedArray::keyExists($this->destination, explode(static::PROPERTY_SEPARATOR, $property));
210 211 212 213 214 215 216 217 218 219 220
  }

  /**
   * Sets destination properties.
   *
   * @param string $property
   *   The name of the destination property.
   * @param mixed $value
   *   The property value to set on the destination.
   */
  public function setDestinationProperty($property, $value) {
221
    $this->rawDestination[$property] = $value;
222
    NestedArray::setValue($this->destination, explode(static::PROPERTY_SEPARATOR, $property), $value, TRUE);
223 224
  }

225 226 227 228 229 230 231 232 233 234 235
  /**
   * Removes destination property.
   *
   * @param string $property
   *   The name of the destination property.
   */
  public function removeDestinationProperty($property) {
    unset($this->rawDestination[$property]);
    NestedArray::unsetValue($this->destination, explode(static::PROPERTY_SEPARATOR, $property));
  }

236 237 238 239 240 241 242 243 244 245
  /**
   * Returns the whole destination array.
   *
   * @return array
   *   An array of destination values.
   */
  public function getDestination() {
    return $this->destination;
  }

246 247 248
  /**
   * Returns the raw destination. Rarely necessary.
   *
249
   * For example calling setDestination('foo/bar', 'baz') results in
250 251
   * @code
   * $this->destination['foo']['bar'] = 'baz';
252
   * $this->rawDestination['foo/bar'] = 'baz';
253
   * @endcode
254 255 256 257 258 259 260 261
   *
   * @return array
   *   The raw destination values.
   */
  public function getRawDestination() {
    return $this->rawDestination;
  }

262 263 264
  /**
   * Returns the value of a destination property.
   *
265 266
   * @param string $property
   *   The name of a property on the destination.
267 268
   *
   * @return mixed
269
   *   The destination value.
270 271
   */
  public function getDestinationProperty($property) {
272
    return NestedArray::getValue($this->destination, explode(static::PROPERTY_SEPARATOR, $property));
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
  }

  /**
   * Sets the Migrate ID mappings.
   *
   * @param array $id_map
   *   An array of mappings between source ID and destination ID.
   */
  public function setIdMap(array $id_map) {
    $this->idMap = $id_map;
  }

  /**
   * Retrieves the Migrate ID mappings.
   *
   * @return array
   *   An array of mapping between source and destination identifiers.
   */
  public function getIdMap() {
    return $this->idMap;
  }

  /**
   * Recalculates the hash for the row.
   */
  public function rehash() {
    $this->idMap['original_hash'] = $this->idMap['hash'];
    $this->idMap['hash'] = hash('sha256', serialize($this->source));
  }

  /**
   * Checks whether the row has changed compared to the original ID map.
   *
   * @return bool
   *   TRUE if the row has changed, FALSE otherwise. If setIdMap() was not
   *   called, this always returns FALSE.
   */
  public function changed() {
    return $this->idMap['original_hash'] != $this->idMap['hash'];
  }

  /**
   * Returns if this row needs an update.
   *
   * @return bool
   *   TRUE if the row needs updating, FALSE otherwise.
   */
  public function needsUpdate() {
321
    return $this->idMap['source_row_status'] == MigrateIdMapInterface::STATUS_NEEDS_UPDATE;
322 323 324 325 326 327 328 329 330 331 332 333
  }

  /**
   * Returns the hash for the source values..
   *
   * @return mixed
   *   The hash of the source values.
   */
  public function getHash() {
    return $this->idMap['hash'];
  }

334
  /**
335
   * Reports whether this row is a stub.
336
   *
337
   * @return bool
338 339
   *   The current stub value.
   */
340 341
  public function isStub() {
    return $this->isStub;
342
  }
343
}