Issue #737170 by mikeryan: Added support for field level callbacks
[project/migrate.git] / includes / field_mapping.inc
CommitLineData
177f7ae4 1<?php
177f7ae4
MR
2
3/**
4 * @file
5 * The MigrateFieldMapping class - tracking mappings between source and
6 * destination.
7 */
8
9class MigrateFieldMapping {
10 /**
11 * Destination field name for the mapping. If empty, the mapping is just a
12 * stub for annotating the source field.
13 *
14 * @var string
15 */
0869eff2
MR
16 protected $destinationField;
17 public function getDestinationField() {
18 return $this->destinationField;
19 }
177f7ae4
MR
20
21 /**
22 * Source field name for the mapping. If empty, the defaultValue will be
23 * applied.
24 *
25 * @var string
26 */
0869eff2
MR
27 protected $sourceField;
28 public function getSourceField() {
29 return $this->sourceField;
30 }
177f7ae4
MR
31
32 /**
33 * Default value for simple mappings, when there is no source mapping or the
34 * source field is empty. If both this and the sourceField are omitted, the
35 * mapping is just a stub for annotating the destination field.
36 *
37 * @var mixed
38 */
0869eff2
MR
39 protected $defaultValue;
40 public function getDefaultValue() {
41 return $this->defaultValue;
42 }
177f7ae4
MR
43
44 /**
45 * Separator string. If present, the destination field will be set up as an
46 * array of values exploded from the corresponding source field.
47 *
48 * @var string
49 */
0869eff2
MR
50 protected $separator;
51 public function getSeparator() {
52 return $this->separator;
53 }
177f7ae4
MR
54
55 /**
56 * Class name of source migration for a field. If present, the value in the
57 * source field is considered to be a source ID in the mapping table of this
58 * migration, and the corresponding destination ID will be retrieved.
59 *
60 * @var string
61 */
0869eff2
MR
62 protected $sourceMigration;
63 public function getSourceMigration() {
64 return $this->sourceMigration;
65 }
1e61b858
MR
66
67 /**
68 * Array of callbacks to be called on a source value.
69 *
70 * @var string
71 */
72 protected $callbacks = array();
73 public function getCallbacks() {
74 return $this->callbacks;
75 }
76
62903225 77 /**
78 * An associative array with keys:
79 * - table: The table for querying for a duplicate.
80 * - column: The column for querying for a duplicate.
81 *
82 * @todo: Let fields declare this data and a replacement pattern. Then
83 * developers won't have to specify this.
84 *
85 * @var string
86 */
87 protected $dedupe;
88 public function getDedupe() {
89 return $this->dedupe;
90 }
177f7ae4
MR
91
92 /**
93 * Argument overrides. If present this will be an array, keyed by
94 * a field API array key, with one or both of these entries:
95 * 'source_field' - Name of the source field in the incoming row containing the
96 * value to be assigned
97 * 'default_value' - A constant value to be assigned in the absence of source_field
98 *
99 * @var array
100 */
0869eff2
MR
101 protected $arguments;
102 public function getArguments() {
103 return $this->arguments;
104 }
177f7ae4 105
0869eff2
MR
106 protected $description = '';
107 public function getDescription() {
108 return $this->description;
109 }
110
111 protected $issueGroup;
112 public function getIssueGroup() {
113 return $this->issueGroup;
114 }
115
116 protected $issueNumber;
117 public function getIssueNumber() {
118 return $this->issueNumber;
119 }
120
121 protected $issuePriority = self::ISSUE_PRIORITY_OK;
122 public function getIssuePriority() {
123 return $this->issuePriority;
124 }
177f7ae4
MR
125
126 const ISSUE_PRIORITY_OK = 1;
127 const ISSUE_PRIORITY_LOW = 2;
128 const ISSUE_PRIORITY_MEDIUM = 3;
129 const ISSUE_PRIORITY_BLOCKER = 4;
130
131 public static $priorities = array();
132
133 public function __construct($destination_field, $source_field) {
134 // Must have one or the other
135 if (!$destination_field && !$source_field) {
136 throw new Exception('Field mappings must have a destination field or a source field');
137 }
138 $this->destinationField = $destination_field;
139 $this->sourceField = $source_field;
d6483a4f 140 $this->issueGroup = t('Done');
177f7ae4
MR
141 if (count(self::$priorities) == 0) {
142 self::$priorities[self::ISSUE_PRIORITY_OK] = t('OK');
143 self::$priorities[self::ISSUE_PRIORITY_LOW] = t('Low');
144 self::$priorities[self::ISSUE_PRIORITY_MEDIUM] = t('Medium');
145 self::$priorities[self::ISSUE_PRIORITY_BLOCKER] = t('Blocker');
146 }
147 }
148
149 public function defaultValue($default_value) {
150 $this->defaultValue = $default_value;
151 return $this;
152 }
153
154 public function separator($separator) {
155 $this->separator = $separator;
156 return $this;
157 }
158
159 public function sourceMigration($source_migration) {
160 $this->sourceMigration = $source_migration;
161 return $this;
162 }
1e61b858
MR
163
164 public function callbacks($callbacks) {
165 $this->callbacks = func_get_args();
166 return $this;
167 }
168
62903225 169 public function dedupe($table, $column) {
170 $this->dedupe = array('table' => $table, 'column' => $column);
171 return $this;
172 }
177f7ae4
MR
173
174 public function arguments($arguments) {
175 $this->arguments = $arguments;
176 return $this;
177 }
178
d6483a4f 179 public function description($text) {
0869eff2 180 $this->description = $text;
d6483a4f
MR
181 return $this;
182 }
183
184 public function issueGroup($group) {
177f7ae4
MR
185 if (!$group) {
186 $group = t('Done');
187 }
d6483a4f 188 $this->issueGroup = $group;
177f7ae4
MR
189 return $this;
190 }
191
192 public function issueNumber($number) {
193 $this->issueNumber = $number;
194 return $this;
195 }
196
197 public function issuePriority($priority) {
198 $this->issuePriority = $priority;
199 return $this;
200 }
201}