|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace TournamentGenerator\Export; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use JsonException; |
|
8
|
|
|
use JsonSerializable; |
|
9
|
|
|
use TournamentGenerator\Export\Modifiers\Modifier; |
|
10
|
|
|
use TournamentGenerator\HierarchyBase; |
|
11
|
|
|
use TournamentGenerator\Interfaces\WithId; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Base class for exporters |
|
15
|
|
|
* |
|
16
|
|
|
* Exporters operate on some HierarchyBase class. They extract data and/or settings from these classes in a form of PHP array. |
|
17
|
|
|
* Exporters also allow of adding modifiers to the exported query - adding more data. These modifiers are added via specific methods (usually starting with "with" keyword). |
|
18
|
|
|
* |
|
19
|
|
|
* @package TournamentGenerator\Export |
|
20
|
|
|
* @author Tomáš Vojík <[email protected]> |
|
21
|
|
|
* @since 0.5 |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class ExporterBase implements Exporter, JsonSerializable |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** @var HierarchyBase Hierarchy object to export */ |
|
27
|
|
|
protected WithId $object; |
|
28
|
|
|
/** @var Modifier[] Modifiers to apply to exported data */ |
|
29
|
|
|
protected array $modifiers = []; |
|
30
|
|
|
|
|
31
|
17 |
|
public function __construct(WithId $object) { |
|
32
|
17 |
|
$this->object = $object; |
|
|
|
|
|
|
33
|
17 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Return result as json |
|
37
|
|
|
* |
|
38
|
|
|
* @return string |
|
39
|
|
|
* @throws JsonException |
|
40
|
|
|
* @see ExporterBase::jsonSerialize() |
|
41
|
|
|
* @see ExporterBase::get() |
|
42
|
|
|
* |
|
43
|
|
|
*/ |
|
44
|
7 |
|
public function getJson() : string { |
|
45
|
7 |
|
return json_encode($this->get(), JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Finish the export query -> get the result |
|
50
|
|
|
* |
|
51
|
|
|
* @return array The query result |
|
52
|
|
|
*/ |
|
53
|
14 |
|
public function get() : array { |
|
54
|
14 |
|
$data = $this->getBasic(); |
|
55
|
14 |
|
$this->applyModifiers($data); |
|
56
|
14 |
|
return array_map(static function(object $object) { |
|
57
|
14 |
|
unset($object->object); |
|
58
|
14 |
|
return $object; |
|
59
|
14 |
|
}, $data); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Apply set modifiers to data array |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $data |
|
66
|
|
|
*/ |
|
67
|
26 |
|
protected function applyModifiers(array &$data) : void { |
|
68
|
26 |
|
foreach ($this->modifiers as $modifier) { |
|
69
|
8 |
|
$modifier::process($data); |
|
70
|
|
|
} |
|
71
|
26 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Serialize exported data as JSON |
|
75
|
|
|
* |
|
76
|
|
|
* @return array |
|
77
|
|
|
* @see json_encode() |
|
78
|
|
|
* |
|
79
|
|
|
*/ |
|
80
|
7 |
|
public function jsonSerialize() : array { |
|
81
|
7 |
|
return $this->get(); |
|
82
|
|
|
} |
|
83
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.