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

WithScoresModifier::processArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
rs 10
c 1
b 0
f 0
1
<?php
2
3
4
namespace TournamentGenerator\Export\Modifiers;
5
6
7
use Exception;
8
use TournamentGenerator\Team;
9
10
class WithScoresModifier implements Modifier
11
{
12
13
	/**
14
	 * @inheritDoc
15
	 * @throws Exception
16
	 */
17
	public static function process(array &$data) : array {
18
		// Check for "single" export
19
		if (isset($data['object'])) {
20
			if (!$data['object'] instanceof Team) {
21
				throw new \InvalidArgumentException('WithScores modifier works only with Team data.');
22
			}
23
			return self::processArray($data);
24
		}
25
26
		foreach ($data as $object) {
27
			if (!$object->object instanceof Team) {
28
				throw new \InvalidArgumentException('WithScores modifier works only with Team data.');
29
			}
30
			self::processObject($object);
31
		}
32
		return $data;
33
	}
34
35
	/**
36
	 * Modify a "Single" data
37
	 *
38
	 * @param array $data
39
	 *
40
	 * @return array
41
	 * @throws Exception
42
	 */
43
	protected static function processArray(array &$data) : array {
44
		/** @var Team $team */
45
		$team = $data['object'];
46
		$data['scores'] = array_map(static function(array $group) {
47
			unset($group['group']); // Get rid of the Group object reference
48
			return $group;
49
		}, $team->getGroupResults());
50
		return $data;
51
	}
52
53
	/**
54
	 * Modify data
55
	 *
56
	 * @param object $object
57
	 *
58
	 * @return object
59
	 * @throws Exception
60
	 */
61
	protected static function processObject(object $object) : object {
62
		/** @var Team $team */
63
		$team = $object->object;
64
		$object->scores = array_map(static function(array $group) {
65
			unset($group['group']); // Get rid of the Group object reference
66
			return $group;
67
		}, $team->getGroupResults());
68
		return $object;
69
	}
70
}