Completed
Push — master ( dca578...2fbbce )
by Tomáš
09:39
created

ExporterBase::applyModifiers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
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;
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 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
}