Completed
Push — master ( a7ba40...b3dcaf )
by Tomáš
06:44
created

ExportBase::getJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
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;
0 ignored issues
show
Documentation Bug introduced by
$object is of type TournamentGenerator\Interfaces\WithId, but the property $object was declared to be of type TournamentGenerator\HierarchyBase. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

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.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
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
}