|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Del\Person\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Del\Person\Criteria\PersonCriteria; |
|
6
|
|
|
use Del\Person\Entity\Person; |
|
7
|
|
|
use Del\Person\Repository\PersonRepository; |
|
8
|
|
|
use Doctrine\ORM\EntityManager; |
|
9
|
|
|
use Barnacle\Container; |
|
10
|
|
|
|
|
11
|
|
|
class PersonService |
|
12
|
|
|
{ |
|
13
|
|
|
public function __construct( |
|
14
|
|
|
protected EntityManager $em |
|
15
|
|
|
) { |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function createFromArray(array $data): Person |
|
19
|
|
|
{ |
|
20
|
5 |
|
$person = new Person(); |
|
21
|
|
|
|
|
22
|
5 |
|
return $this->populateFromArray($person, $data); |
|
23
|
5 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function populateFromArray(Person $person, array $data): Person |
|
26
|
|
|
{ |
|
27
|
|
|
isset($data['id']) ? $person->setId($data['id']) : null; |
|
28
|
|
|
isset($data['firstname']) ? $person->setFirstname($data['firstname']) : null; |
|
29
|
4 |
|
isset($data['middlename']) ? $person->setMiddlename($data['middlename']) : null; |
|
30
|
|
|
isset($data['lastname']) ? $person->setLastname($data['lastname']) : null; |
|
31
|
4 |
|
isset($data['aka']) ? $person->setAka($data['aka']) : null; |
|
32
|
|
|
isset($data['dob']) ? $person->setDob($data['dob']) : null; |
|
33
|
4 |
|
isset($data['birthplace']) ? $person->setBirthplace($data['birthplace']) : null; |
|
34
|
|
|
isset($data['country']) ? $person->setCountry($data['country']) : null; |
|
35
|
|
|
isset($data['image']) ? $person->setImage($data['image']) : null; |
|
36
|
|
|
isset($data['backgroundImage']) ? $person->setImage($data['image']) : null; |
|
37
|
|
|
|
|
38
|
|
|
return $person; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
4 |
|
public function toArray(Person $person): array |
|
42
|
|
|
{ |
|
43
|
4 |
|
$data = [ |
|
44
|
4 |
|
'id' => $person->getId(), |
|
45
|
4 |
|
'firstname' => $person->getFirstname(), |
|
46
|
4 |
|
'middlename' => $person->getMiddlename(), |
|
47
|
4 |
|
'lastname' => $person->getLastname(), |
|
48
|
4 |
|
'aka' => $person->getAka(), |
|
49
|
4 |
|
'dob' => $person->getDob(), |
|
50
|
4 |
|
'birthplace' => $person->getBirthplace(), |
|
51
|
4 |
|
'country' => $person->getCountry(), |
|
52
|
|
|
'image' => $person->getImage(), |
|
53
|
4 |
|
'backgroundImage' => $person->getBackgroundImage(), |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
return $data; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function savePerson(Person $person): Person |
|
60
|
1 |
|
{ |
|
61
|
|
|
return $this->getRepository()->save($person); |
|
62
|
1 |
|
} |
|
63
|
1 |
|
|
|
64
|
1 |
|
public function deletePerson(Person $person): void |
|
65
|
1 |
|
{ |
|
66
|
1 |
|
$this->getRepository()->delete($person); |
|
67
|
1 |
|
} |
|
68
|
1 |
|
|
|
69
|
1 |
|
public function findByCriteria(PersonCriteria $criteria): array |
|
70
|
1 |
|
{ |
|
71
|
1 |
|
return $this->getRepository()->findByCriteria($criteria); |
|
72
|
|
|
} |
|
73
|
1 |
|
|
|
74
|
|
|
public function findOneByCriteria(PersonCriteria $criteria): ?Person |
|
75
|
|
|
{ |
|
76
|
|
|
$results = $this->findByCriteria($criteria); |
|
77
|
|
|
return (isset($results[0])) ? $results[0] : null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
protected function getRepository(): PersonRepository |
|
81
|
|
|
{ |
|
82
|
2 |
|
return $this->em->getRepository(Person::class); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|