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 * Constructs a \Drupal\Migrate\Row object.
83 * @param array $values
84 * An array of values to add as properties on the object.
85 * @param array $source_ids
86 * An array containing the IDs of the source using the keys as the field
88 * @param bool $is_stub
89 * TRUE if the row being created is a stub.
91 * @throws \InvalidArgumentException
92 * Thrown when a source ID property does not exist.
94 public function __construct(array $values = [], array $source_ids = [], $is_stub = FALSE) {
95 $this->source = $values;
96 $this->sourceIds = $source_ids;
97 $this->isStub = $is_stub;
98 foreach (array_keys($source_ids) as $id) {
99 if (!$this->hasSourceProperty($id)) {
100 throw new \InvalidArgumentException("$id has no value");
106 * Retrieves the values of the source identifiers.
109 * An array containing the values of the source identifiers. Returns values
110 * in the same order as defined in $this->sourceIds.
112 public function getSourceIdValues() {
113 return array_merge($this->sourceIds, array_intersect_key($this->source, $this->sourceIds));
117 * Determines whether a source has a property.
119 * @param string $property
120 * A property on the source.
123 * TRUE if the source has property; FALSE otherwise.
125 public function hasSourceProperty($property) {
126 return NestedArray::keyExists($this->source, explode(static::PROPERTY_SEPARATOR, $property));
130 * Retrieves a source property.
132 * @param string $property
133 * A property on the source.
136 * The found returned property or NULL if not found.
138 public function getSourceProperty($property) {
139 $return = NestedArray::getValue($this->source, explode(static::PROPERTY_SEPARATOR, $property), $key_exists);
146 * Returns the whole source array.
149 * An array of source plugins.
151 public function getSource() {
152 return $this->source;
156 * Sets a source property.
158 * This can only be called from the source plugin.
160 * @param string $property
161 * A property on the source.
163 * The property value to set on the source.
167 public function setSourceProperty($property, $data) {
169 throw new \Exception("The source is frozen and can't be changed any more");
172 NestedArray::setValue($this->source, explode(static::PROPERTY_SEPARATOR, $property), $data, TRUE);
177 * Freezes the source.
181 public function freezeSource() {
182 $this->frozen = TRUE;
187 * Clones the row with an empty set of destination values.
191 public function cloneWithoutDestination() {
192 return (new static($this->getSource(), $this->sourceIds, $this->isStub()))->freezeSource();
196 * Tests if destination property exists.
198 * @param array|string $property
199 * An array of properties on the destination.
202 * TRUE if the destination property exists.
204 public function hasDestinationProperty($property) {
205 return NestedArray::keyExists($this->destination, explode(static::PROPERTY_SEPARATOR, $property));
209 * Sets destination properties.
211 * @param string $property
212 * The name of the destination property.
213 * @param mixed $value
214 * The property value to set on the destination.
216 public function setDestinationProperty($property, $value) {
217 $this->rawDestination[$property] = $value;
218 NestedArray::setValue($this->destination, explode(static::PROPERTY_SEPARATOR, $property), $value, TRUE);
222 * Removes destination property.
224 * @param string $property
225 * The name of the destination property.
227 public function removeDestinationProperty($property) {
228 unset($this->rawDestination[$property]);
229 NestedArray::unsetValue($this->destination, explode(static::PROPERTY_SEPARATOR, $property));
233 * Returns the whole destination array.
236 * An array of destination values.
238 public function getDestination() {
239 return $this->destination;
243 * Returns the raw destination. Rarely necessary.
245 * For example calling setDestination('foo/bar', 'baz') results in
247 * $this->destination['foo']['bar'] = 'baz';
248 * $this->rawDestination['foo/bar'] = 'baz';
252 * The raw destination values.
254 public function getRawDestination() {
255 return $this->rawDestination;
259 * Returns the value of a destination property.
261 * @param string $property
262 * The name of a property on the destination.
265 * The destination value.
267 public function getDestinationProperty($property) {
268 return NestedArray::getValue($this->destination, explode(static::PROPERTY_SEPARATOR, $property));
272 * Sets the Migrate ID mappings.
274 * @param array $id_map
275 * An array of mappings between source ID and destination ID.
277 public function setIdMap(array $id_map) {
278 $this->idMap = $id_map;
282 * Retrieves the Migrate ID mappings.
285 * An array of mapping between source and destination identifiers.
287 public function getIdMap() {
292 * Recalculates the hash for the row.
294 public function rehash() {
295 $this->idMap['original_hash'] = $this->idMap['hash'];
296 $this->idMap['hash'] = hash('sha256', serialize($this->source));
300 * Checks whether the row has changed compared to the original ID map.
303 * TRUE if the row has changed, FALSE otherwise. If setIdMap() was not
304 * called, this always returns FALSE.
306 public function changed() {
307 return $this->idMap['original_hash'] != $this->idMap['hash'];
311 * Returns if this row needs an update.
314 * TRUE if the row needs updating, FALSE otherwise.
316 public function needsUpdate() {
317 return $this->idMap['source_row_status'] == MigrateIdMapInterface::STATUS_NEEDS_UPDATE;
321 * Returns the hash for the source values..
324 * The hash of the source values.
326 public function getHash() {
327 return $this->idMap['hash'];
331 * Reports whether this row is a stub.
334 * The current stub value.
336 public function isStub() {
337 return $this->isStub;