1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BZIon\Form\Transformer; |
4
|
|
|
|
5
|
|
|
class MultipleAdvancedModelTransformer extends AdvancedModelTransformer |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* A list of Models to include to the results, even if they are not |
9
|
|
|
* specified by the user |
10
|
|
|
* |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
private $included = array(); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Transforms data to an object |
17
|
|
|
* |
18
|
|
|
* @param string $data |
19
|
|
|
* @return Model[] |
20
|
|
|
*/ |
21
|
1 |
|
public function reverseTransform($data) |
22
|
|
|
{ |
23
|
|
|
// Handle the data provided by Javascript, if any |
24
|
1 |
|
$transformed = parent::transformJSON($data, $this->included); |
|
|
|
|
25
|
|
|
|
26
|
1 |
|
if ($transformed !== false) { |
27
|
|
|
return $transformed; |
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
$models = array(); |
31
|
|
|
|
32
|
1 |
|
foreach ($this->types as $type) { |
33
|
1 |
|
if (trim($data[$type]) === '') { |
34
|
1 |
|
continue; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// Array to store IDs for quick access so we can be sure that no |
38
|
|
|
// duplicates are saved |
39
|
1 |
|
$ids = array(); |
40
|
|
|
|
41
|
|
|
// Add force-included models |
42
|
1 |
|
if (isset($this->included[$type])) { |
43
|
1 |
|
foreach ($this->included[$type] as $model) { |
44
|
1 |
|
$ids[$model->getID()] = true; // Prevent duplication |
45
|
1 |
|
$models[] = $model; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
$input = explode(',', $data[$type]); |
50
|
1 |
|
$input = array_unique(array_map('trim', $input)); |
51
|
|
|
|
52
|
1 |
|
foreach ($input as $name) { |
53
|
1 |
|
if ($name === '') { |
54
|
1 |
|
continue; |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
$model = $this->getModelFromName($name, $type); |
|
|
|
|
58
|
|
|
|
59
|
1 |
|
if (!$model) { |
60
|
|
|
// No model was found matching that name |
61
|
|
|
$models[] = $this->invalidModel($type); |
62
|
1 |
View Code Duplication |
} elseif (!$model->isValid() || !isset($ids[$model->getID()])) { |
|
|
|
|
63
|
|
|
// We can proceed, since we're not storing a duplicate |
64
|
|
|
// (We don't check invalid models, since they might be |
65
|
|
|
// corresponding to different names) |
66
|
1 |
|
$models[] = $model; |
67
|
1 |
|
$ids[$model->getID()] = true; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
return $models; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Add a model to the list of models that should be included in all cases |
77
|
|
|
* |
78
|
|
|
* @param \Model $model The model to include |
79
|
|
|
* @throws \Exception When a model is of an unsupported type |
80
|
|
|
*/ |
81
|
1 |
|
public function addInclude(\Model $model) |
82
|
|
|
{ |
83
|
1 |
|
$type = strtolower($model->getType()); |
84
|
|
|
|
85
|
1 |
|
if (!in_array($type, $this->types)) { |
86
|
|
|
throw new \Exception( |
87
|
|
|
"Objects of type \"{$model->getTypeForHumans()}\" are not supported" |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
$this->included[$type][] = $model; |
92
|
1 |
|
} |
93
|
|
|
} |
94
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.