1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Donor entity repository class file |
4
|
|
|
* |
5
|
|
|
* @package EBloodBank |
6
|
|
|
* @subpackage Models |
7
|
|
|
* @since 1.0 |
8
|
|
|
*/ |
9
|
|
|
namespace EBloodBank\Models; |
10
|
|
|
|
11
|
|
|
use EBloodBank as EBB; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Donor entity repository class |
15
|
|
|
* |
16
|
|
|
* @since 1.0 |
17
|
|
|
*/ |
18
|
|
|
class DonorRepository extends EntityRepository |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Finds entities by a set of criteria. |
22
|
|
|
* |
23
|
|
|
* @param array $criteria |
24
|
|
|
* @param array|null $orderBy |
25
|
|
|
* @param int|null $limit |
26
|
|
|
* @param int|null $offset |
27
|
|
|
* |
28
|
|
|
* @return array The objects. |
29
|
|
|
*/ |
30
|
|
|
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
31
|
|
|
{ |
32
|
|
|
$criteria = $this->parseCriteria($criteria); |
33
|
|
|
return parent::findBy($criteria, $orderBy, $limit, $offset); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Finds a single entity by a set of criteria. |
38
|
|
|
* |
39
|
|
|
* @param array $criteria |
40
|
|
|
* @param array|null $orderBy |
41
|
|
|
* |
42
|
|
|
* @return object|null The entity instance or NULL if the entity can not be found. |
43
|
|
|
*/ |
44
|
|
|
public function findOneBy(array $criteria, array $orderBy = null) |
45
|
|
|
{ |
46
|
|
|
$criteria = $this->parseCriteria($criteria); |
47
|
|
|
return parent::findOneBy($criteria, $orderBy); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array |
52
|
|
|
* @since 1.0 |
53
|
|
|
*/ |
54
|
|
|
protected function parseCriteria(array $criteria) |
55
|
|
|
{ |
56
|
|
|
if (isset($criteria['blood_group_alternatives']) && $criteria['blood_group_alternatives']) { |
57
|
|
|
if (isset($criteria['blood_group']) && is_string($criteria['blood_group'])) { |
58
|
|
|
$criteria['blood_group'] = $this->findCompatibleRedBloodCellGroup($criteria['blood_group']); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
unset($criteria['blood_group_alternatives']); // Remove the alternative blood-groups criteria. |
63
|
|
|
|
64
|
|
|
if (isset($criteria['blood_group']) && 'any' === $criteria['blood_group']) { |
65
|
|
|
unset($criteria['blood_group']); // Remove the blood-group criteria. |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (isset($criteria['district']) && -1 == $criteria['district']) { |
69
|
|
|
unset($criteria['district']); // Remove the district criteria. |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (isset($criteria['city']) && empty($criteria['district'])) { |
73
|
|
|
$districts = []; |
74
|
|
|
|
75
|
|
|
if ($criteria['city'] instanceof City) { |
76
|
|
|
$districts = $criteria['city']->get('districts'); |
77
|
|
|
} elseif (EBB\isValidID($criteria['city'])) { |
|
|
|
|
78
|
|
|
$em = $this->getEntityManager(); |
79
|
|
|
$city = $em->find('Entities:City', $criteria['city']); |
80
|
|
|
if (! empty($city)) { |
81
|
|
|
$districts = $city->get('districts'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (! empty($districts)) { |
86
|
|
|
$criteria['district'] = []; |
87
|
|
|
|
88
|
|
|
foreach ($districts as $district) { |
89
|
|
|
$criteria['district'][] = (int) $district->get('id'); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
unset($criteria['city']); // Remove the city criteria in any condition. |
95
|
|
|
|
96
|
|
|
if (isset($criteria['status']) && 'any' === $criteria['status']) { |
97
|
|
|
unset($criteria['status']); // Remove the status criteria. |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $criteria; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return array |
105
|
|
|
* @since 1.6 |
106
|
|
|
*/ |
107
|
|
|
protected function findCompatibleRedBloodCellGroup(string $bloodGroup) |
108
|
|
|
{ |
109
|
|
|
$compatibleGroups = []; |
110
|
|
|
|
111
|
|
|
switch ($bloodGroup) { |
112
|
|
|
case 'A+': |
113
|
|
|
$compatibleGroups = ['A+', 'A-', 'O+', 'O-']; |
114
|
|
|
break; |
115
|
|
|
case 'A-': |
116
|
|
|
$compatibleGroups = ['A-', 'O-']; |
117
|
|
|
break; |
118
|
|
|
case 'B+': |
119
|
|
|
$compatibleGroups = ['B+', 'B-', 'O+', 'O-']; |
120
|
|
|
break; |
121
|
|
|
case 'B+': |
122
|
|
|
$compatibleGroups = ['B+', 'B-', 'O+', 'O-']; |
123
|
|
|
break; |
124
|
|
|
case 'B-': |
125
|
|
|
$compatibleGroups = ['B-', 'O-']; |
126
|
|
|
break; |
127
|
|
|
case 'O+': |
128
|
|
|
$compatibleGroups = ['O+', 'O-']; |
129
|
|
|
break; |
130
|
|
|
case 'O-': |
131
|
|
|
$compatibleGroups = ['O-']; |
132
|
|
|
break; |
133
|
|
|
case 'AB+': |
134
|
|
|
$compatibleGroups = ['any']; |
135
|
|
|
break; |
136
|
|
|
case 'AB-': |
137
|
|
|
$compatibleGroups = ['A-', 'B-', 'O-', 'AB-']; |
138
|
|
|
break; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $compatibleGroups; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|