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 ExportBase implements Export, JsonSerializable |
24
|
|
|
{ |
25
|
|
|
|
26
|
17 |
|
/** @var HierarchyBase Hierarchy object to export */ |
27
|
17 |
|
protected WithId $object; |
28
|
17 |
|
/** @var Modifier[] Modifiers to apply to exported data */ |
29
|
|
|
protected array $modifiers = []; |
30
|
|
|
|
31
|
|
|
public function __construct(WithId $object) { |
32
|
|
|
$this->object = $object; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
14 |
|
/** |
36
|
14 |
|
* Return result as json |
37
|
14 |
|
* |
38
|
14 |
|
* @return string |
39
|
14 |
|
* @throws JsonException |
40
|
14 |
|
* @see ExportBase::jsonSerialize() |
41
|
14 |
|
* @see ExportBase::get() |
42
|
|
|
* |
43
|
|
|
*/ |
44
|
|
|
public function getJson() : string { |
45
|
|
|
return $this->jsonSerialize(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
16 |
|
* Serialize exported data as JSON |
50
|
16 |
|
* |
51
|
5 |
|
* @return string |
52
|
|
|
* @throws JsonException |
53
|
16 |
|
*/ |
54
|
1 |
|
public function jsonSerialize() : string { |
55
|
|
|
return json_encode($this->get(), JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Finish the export query -> get the result |
60
|
|
|
* |
61
|
|
|
* @return array The query result |
62
|
|
|
*/ |
63
|
|
|
public function get() : array { |
64
|
|
|
$data = $this->getBasic(); |
65
|
|
|
$this->applyModifiers($data); |
66
|
|
|
return array_map(static function(object $object) { |
67
|
|
|
unset($object->object); |
68
|
|
|
return $object; |
69
|
|
|
}, $data); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Apply set modifiers to data array |
74
|
|
|
* |
75
|
|
|
* @param array $data |
76
|
|
|
*/ |
77
|
|
|
protected function applyModifiers(array &$data) : void { |
78
|
|
|
foreach ($this->modifiers as $modifier) { |
79
|
|
|
$modifier::process($data); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
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.