comparison core/modules/migrate/src/Row.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
83 * @var array 83 * @var array
84 */ 84 */
85 protected $emptyDestinationProperties = []; 85 protected $emptyDestinationProperties = [];
86 86
87 /** 87 /**
88 * Constructs a \Drupal\Migrate\Row object. 88 * Constructs a \Drupal\migrate\Row object.
89 * 89 *
90 * @param array $values 90 * @param array $values
91 * An array of values to add as properties on the object. 91 * An array of values to add as properties on the object.
92 * @param array $source_ids 92 * @param array $source_ids
93 * An array containing the IDs of the source using the keys as the field 93 * An array containing the IDs of the source using the keys as the field
134 } 134 }
135 135
136 /** 136 /**
137 * Retrieves a source property. 137 * Retrieves a source property.
138 * 138 *
139 * This function directly retrieves a source property. It does not unescape
140 * '@' symbols. This is most useful in source plugins when you don't want to
141 * worry about escaping '@' symbols. If using this in a process plugin to
142 * retrieve a source property based on a configuration value, consider if the
143 * ::get() function might be more appropriate, to allow the migration to
144 * potentially specify a destination key as well.
145 *
139 * @param string $property 146 * @param string $property
140 * A property on the source. 147 * A property on the source.
141 * 148 *
142 * @return mixed|null 149 * @return mixed|null
143 * The found returned property or NULL if not found. 150 * The found returned property or NULL if not found.
283 } 290 }
284 291
285 /** 292 /**
286 * Returns the value of a destination property. 293 * Returns the value of a destination property.
287 * 294 *
295 * This function directly returns a destination property. The property name
296 * should not begin with an @ symbol. This is most useful in a destination
297 * plugin.
298 *
288 * @param string $property 299 * @param string $property
289 * The name of a property on the destination. 300 * The name of a property on the destination.
290 * 301 *
291 * @return mixed 302 * @return mixed
292 * The destination value. 303 * The destination value.
293 */ 304 */
294 public function getDestinationProperty($property) { 305 public function getDestinationProperty($property) {
295 return NestedArray::getValue($this->destination, explode(static::PROPERTY_SEPARATOR, $property)); 306 return NestedArray::getValue($this->destination, explode(static::PROPERTY_SEPARATOR, $property));
307 }
308
309 /**
310 * Retrieve a source or destination property.
311 *
312 * If the property key begins with '@' return a destination property,
313 * otherwise return a source property. the '@' symbol itself can be escaped
314 * as '@@'. Returns NULL if property is not found. Useful in process plugins
315 * to retrieve a row property specified in a configuration key which may be
316 * either a source or destination property prefixed with an '@'.
317 *
318 * @param string $property
319 * The property to get.
320 *
321 * @return mixed|null
322 * The requested property.
323 */
324 public function get($property) {
325 $values = $this->getMultiple([$property]);
326 return reset($values);
327 }
328
329 /**
330 * Retrieve multiple source and destination properties at once.
331 *
332 * @param string[] $properties
333 * An array of values to retrieve, with destination values prefixed with @.
334 *
335 * @return array
336 * An array of property values, keyed by property name.
337 */
338 public function getMultiple(array $properties) {
339 $return = [];
340 foreach ($properties as $orig_property) {
341 $property = $orig_property;
342 $is_source = TRUE;
343 if ($property[0] == '@') {
344 $property = preg_replace_callback('/^(@?)((?:@@)*)([^@]|$)/', function ($matches) use (&$is_source) {
345 // If there are an odd number of @ in the beginning, it's a
346 // destination.
347 $is_source = empty($matches[1]);
348 // Remove the possible escaping and do not lose the terminating
349 // non-@ either.
350 return str_replace('@@', '@', $matches[2]) . $matches[3];
351 }, $property);
352 }
353 if ($is_source) {
354 $return[$orig_property] = $this->getSourceProperty($property);
355 }
356 else {
357 $return[$orig_property] = $this->getDestinationProperty($property);
358 }
359 }
360 return $return;
296 } 361 }
297 362
298 /** 363 /**
299 * Sets the Migrate ID mappings. 364 * Sets the Migrate ID mappings.
300 * 365 *