Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / ArrayBuild.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\process;
4
5 use Drupal\migrate\MigrateException;
6 use Drupal\migrate\MigrateExecutableInterface;
7 use Drupal\migrate\ProcessPluginBase;
8 use Drupal\migrate\Row;
9
10 /**
11  * Builds an array based on the key and value configuration.
12  *
13  * The array_build plugin builds a single associative array by extracting keys
14  * and values from each array in the input value, which is expected to be an
15  * array of arrays. The keys of the returned array will be determined by the
16  * 'key' configuration option, and the values will be determined by the 'value'
17  * option.
18  *
19  * Available configuration keys
20  *   - key: The key used to lookup a value in the source arrays to be used as
21  *     a key in the destination array.
22  *   - value: The key used to lookup a value in the source arrays to be used as
23  *     a value in the destination array.
24  *
25  * Example:
26  *
27  * Consider the migration of language negotiation by domain.
28  * The source is an array of all the languages:
29  *
30  * @code
31  * languages: Array
32  * (
33  *   [0] => Array
34  *     (
35  *       [language] => en
36  * ...
37  *       [domain] => http://example.com
38  *     )
39  *   [1] => Array
40  *     (
41  *       [language] => fr
42  * ...
43  *       [domain] => http://fr.example.com
44  *     )
45  * ...
46  * @endcode
47  *
48  * The destination should be an array of all the domains keyed by their
49  * language code:
50  *
51  * @code
52  * domains: Array
53  * (
54  *   [en] => http://example.com
55  *   [fr] => http://fr.example.com
56  * ...
57  * @endcode
58  *
59  * The array_build process plugin would be used like this:
60  *
61  * @code
62  * process:
63  *   domains:
64  *     plugin: array_build
65  *     key: language
66  *     value: domain
67  *     source: languages
68  * @endcode
69  *
70  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
71  *
72  * @MigrateProcessPlugin(
73  *   id = "array_build",
74  *   handle_multiples = TRUE
75  * )
76  */
77 class ArrayBuild extends ProcessPluginBase {
78
79   /**
80    * {@inheritdoc}
81    */
82   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
83     $new_value = [];
84
85     foreach ((array) $value as $old_key => $old_value) {
86       // Checks that $old_value is an array.
87       if (!is_array($old_value)) {
88         throw new MigrateException("The input should be an array of arrays");
89       }
90
91       // Checks that the key exists.
92       if (!array_key_exists($this->configuration['key'], $old_value)) {
93         throw new MigrateException("The key '" . $this->configuration['key'] . "' does not exist");
94       }
95
96       // Checks that the value exists.
97       if (!array_key_exists($this->configuration['value'], $old_value)) {
98         throw new MigrateException("The key '" . $this->configuration['value'] . "' does not exist");
99       }
100
101       $new_value[$old_value[$this->configuration['key']]] = $old_value[$this->configuration['value']];
102     }
103
104     return $new_value;
105   }
106
107 }