1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @file |
4
|
|
|
* Contains \TheSportsDb\Entity\Sport. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace TheSportsDb\Entity; |
8
|
|
|
|
9
|
|
|
use TheSportsDb\Entity\EntityManagerInterface; |
10
|
|
|
use TheSportsDb\PropertyMapper\PropertyDefinition; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A fully loaded sport object. |
14
|
|
|
* |
15
|
|
|
* @author Jelle Sebreghts |
16
|
|
|
*/ |
17
|
|
|
class Sport extends Entity implements SportInterface { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
protected static $propertyMapDefinition; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The primary identifier. |
26
|
|
|
* |
27
|
|
|
* @var mixed |
28
|
|
|
*/ |
29
|
|
|
protected $id; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The name. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $name; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The leagues of this sport. |
40
|
|
|
* |
41
|
|
|
* @var \TheSportsDb\Entity\LeagueInterface[] |
42
|
|
|
*/ |
43
|
|
|
protected $leagues = array(); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
1 |
|
public function getId() { |
49
|
1 |
|
return $this->id; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
1 |
|
public function getName() { |
56
|
1 |
|
return $this->name; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
1 |
|
public function getLeagues() { |
63
|
1 |
|
return $this->leagues; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
1 |
|
public function addLeague(LeagueInterface $league) { |
70
|
1 |
|
if (!isset($this->leagues[$league->getId()])) { |
71
|
1 |
|
$this->leagues[$league->getId()] = $league; |
72
|
|
|
} |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Transforms the leagues property to league entities. |
77
|
|
|
* |
78
|
|
|
* @param array $values |
79
|
|
|
* The source value of the leagues property. |
80
|
|
|
* @param \stdClass $context |
81
|
|
|
* The source object representing this sport. |
82
|
|
|
* @param EntityManagerInterface $entityManager |
83
|
|
|
* The entity manager. |
84
|
|
|
* |
85
|
|
|
* @return \TheSportsDb\Entity\LeagueInterface[] |
86
|
|
|
* The league entities. |
87
|
|
|
*/ |
88
|
|
|
public static function transformLeagues($values, $context, EntityManagerInterface $entityManager) { |
89
|
|
|
$mappedLeagues = array(); |
90
|
|
|
foreach ($values as $leagueData) { |
91
|
|
|
$mappedLeagues[] = $entityManager->repository('league')->byId($leagueData->idLeague); |
92
|
|
|
} |
93
|
|
|
return $mappedLeagues; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
1 |
|
protected static function initPropertyMapDefinition() { |
100
|
1 |
|
static::$propertyMapDefinition |
101
|
1 |
|
->addPropertyMap( |
102
|
1 |
|
new PropertyDefinition('strSport'), |
103
|
1 |
|
new PropertyDefinition('id') |
104
|
|
|
) |
105
|
1 |
|
->addPropertyMap( |
106
|
1 |
|
new PropertyDefinition('strSport'), |
107
|
1 |
|
new PropertyDefinition('name') |
108
|
|
|
) |
109
|
1 |
|
->addPropertyMap( |
110
|
1 |
|
new PropertyDefinition('leagues'), |
111
|
1 |
|
new PropertyDefinition('leagues', 'league', TRUE), |
112
|
1 |
|
[self::class, 'transformLeagues'], |
113
|
1 |
|
[League::class, 'reverseArray'] |
114
|
|
|
); |
115
|
1 |
|
} |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|