-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.php
More file actions
195 lines (168 loc) · 3.91 KB
/
Copy pathcode.php
File metadata and controls
195 lines (168 loc) · 3.91 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* @package Joomla.Component.Builder
*
* @created 4th September, 2022
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VDM\Joomla\Import;
use VDM\Joomla\Interfaces\TableInterface as Table;
use VDM\Joomla\Interfaces\Import\MapperInterface;
/**
* Import Mapper Class
*
* @since 4.0.3
*/
final class Mapper implements MapperInterface
{
/**
* The Table Class.
*
* @var Table
* @since 4.0.3
*/
protected Table $table;
/**
* The current parent table map.
*
* @var array
* @since 4.0.3
*/
private array $parent = [];
/**
* The current join tables map.
*
* @var array
* @since 4.0.3
*/
private array $join = [];
/**
* Constructor.
*
* @param Table $table The Table Class.
*
* @since 4.0.3
*/
public function __construct(Table $table)
{
$this->table = $table;
}
/**
* Build the table-to-field mapping for import processing.
*
* Parent table fields are stored directly, while foreign table fields
* are grouped under their respective join table names.
*
* @param object $map The import file map.
* @param string $parentTable The parent table name.
*
* @return void
* @since 4.0.3
*/
public function set(object $map, string $parentTable): void
{
// Always reset state
$this->parent = [];
$this->join = [];
foreach ($map as $row)
{
if (empty($row->target) || empty($row->column))
{
continue;
}
$mapping = $this->getTableField($row->target);
if ($mapping === null)
{
continue;
}
$field = $this->table->get($mapping->table, $mapping->field);
if (!empty($mapping->column_value))
{
$field['subform_2'] = $mapping;
}
if ($mapping->table === $parentTable)
{
$this->parent[$row->column] = $field;
continue;
}
$this->join[$mapping->table][$row->column] = $field;
}
}
/**
* Get the parent table keys
*
* @return array
* @since 4.0.3
*/
public function getParent(): array
{
return $this->parent;
}
/**
* Get the join tables keys
*
* @return array
* @since 4.0.3
*/
public function getJoin(): array
{
return $this->join;
}
/**
* Get the table and field name from an import key.
*
* Expected format:
* table.field
* table.field.subfield.another
*
* Only the first dot separates table and field.
*
* @param string $key The import file key.
*
* @return object|null Object with table and field properties, or null if invalid.
* @since 4.0.3
*/
private function getTableField(string $key): ?object
{
// Split only on the first dot
$parts = explode('.', $key, 2);
// Must contain a table and a field
if (count($parts) !== 2 || $parts[0] === '' || $parts[1] === '')
{
return null;
}
[$table, $field] = $parts;
// Sub-from support (ONLY two values in subform)
if (strpos($field, '|') !== false)
{
$parts_ = array_map('trim', explode('|', $field));
if (count($parts_) === 4)
{
[$field, $value, $column, $columnValue] = $parts_;
}
}
// Validate table/field existence
if (!$this->table->exist($table, $field))
{
return null;
}
// ONLY two value sub-from mapping supported [table.field|value|column|column_value]
if (!empty($value) && !empty($column) && !empty($columnValue))
{
return (object) [
'table' => $table,
'field' => $field,
'value' => $value,
'column' => $column,
'column_value' => $columnValue
];
}
return (object) [
'table' => $table,
'field' => $field,
];
}
}