1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DavidePastore\CodiceFiscaleRest\Controller; |
4
|
|
|
|
5
|
|
|
use DavidePastore\CodiceFiscaleRest\Entity\Subject; |
6
|
|
|
use CodiceFiscale\Calculator; |
7
|
|
|
use CodiceFiscale\Checker; |
8
|
|
|
use CodiceFiscale\Subject as CodiceFiscaleSubject; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
10
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
13
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
14
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class CrudController |
18
|
|
|
* @Route("/api/codiceFiscale") |
19
|
|
|
*/ |
20
|
|
|
class CodiceFiscaleController extends AbstractController |
21
|
|
|
{ |
22
|
|
|
public function __construct(ValidatorInterface $validator) |
23
|
|
|
{ |
24
|
|
|
$this->validator = $validator; |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Calculate the codice fiscale by the given parameters. |
29
|
|
|
* @Route("/calculate", name="apiCalculate") |
30
|
|
|
*/ |
31
|
|
|
public function calculate(Request $request): Response |
32
|
|
|
{ |
33
|
|
|
$data = array( |
34
|
|
|
'name' => $request->get('name', ''), |
35
|
|
|
'surname' => $request->get('surname', ''), |
36
|
|
|
'gender' => $request->get('gender', ''), |
37
|
|
|
'birthDate' => $request->get('birthDate', ''), |
38
|
|
|
'belfioreCode' => $request->get('belfioreCode', ''), |
39
|
|
|
'omocodiaLevel' => $request->get('omocodiaLevel', 0), |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
$subject = new Subject($data); |
43
|
|
|
|
44
|
|
|
$errors = $this->validator->validate($subject, null, 'calculate'); |
45
|
|
|
$response = new \stdClass(); |
46
|
|
|
|
47
|
|
View Code Duplication |
if (count($errors) > 0) { |
|
|
|
|
48
|
|
|
$response = $this->generateResponseFromErrors($errors); |
49
|
|
|
} else { |
50
|
|
|
$subject = new CodiceFiscaleSubject($data); |
51
|
|
|
|
52
|
|
|
$calculator = new Calculator($subject, array( |
53
|
|
|
'omocodiaLevel' => $data['omocodiaLevel'], |
54
|
|
|
)); |
55
|
|
|
$codiceFiscale = $calculator->calculate(); |
56
|
|
|
|
57
|
|
|
$response->status = true; |
58
|
|
|
$response->codiceFiscale = $codiceFiscale; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return new JsonResponse($response); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Calculate all the codice fiscale by the given parameters. |
66
|
|
|
* @Route("/calculateAll", name="apiCalculateAll") |
67
|
|
|
*/ |
68
|
|
|
public function calculateAll(Request $request): Response |
69
|
|
|
{ |
70
|
|
|
$data = array( |
71
|
|
|
'name' => $request->get('name', ''), |
72
|
|
|
'surname' => $request->get('surname', ''), |
73
|
|
|
'gender' => $request->get('gender', ''), |
74
|
|
|
'birthDate' => $request->get('birthDate', ''), |
75
|
|
|
'belfioreCode' => $request->get('belfioreCode', ''), |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
$subject = new Subject($data); |
79
|
|
|
|
80
|
|
|
$errors = $this->validator->validate($subject, null, 'calculateAll'); |
81
|
|
|
$response = new \stdClass(); |
82
|
|
|
|
83
|
|
View Code Duplication |
if (count($errors) > 0) { |
|
|
|
|
84
|
|
|
$response = $this->generateResponseFromErrors($errors); |
85
|
|
|
} else { |
86
|
|
|
$subject = new CodiceFiscaleSubject($data); |
87
|
|
|
|
88
|
|
|
$calculator = new Calculator($subject); |
89
|
|
|
$codiciFiscali = $calculator->calculateAllPossibilities(); |
90
|
|
|
|
91
|
|
|
$response->status = true; |
92
|
|
|
$response->codiciFiscali = $codiciFiscali; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return new JsonResponse($response); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check if the given parameters are ok for the given parameters. |
100
|
|
|
* @Route("/check", name="apiCheck") |
101
|
|
|
*/ |
102
|
|
|
public function check(Request $request): Response |
103
|
|
|
{ |
104
|
|
|
$data = array( |
105
|
|
|
'name' => $request->get('name', ''), |
106
|
|
|
'surname' => $request->get('surname', ''), |
107
|
|
|
'gender' => $request->get('gender', ''), |
108
|
|
|
'birthDate' => $request->get('birthDate', ''), |
109
|
|
|
'belfioreCode' => $request->get('belfioreCode', ''), |
110
|
|
|
'codiceFiscale' => $request->get('codiceFiscale', ''), |
111
|
|
|
'omocodiaLevel' => $request->get('omocodiaLevel', Checker::ALL_OMOCODIA_LEVELS), |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$subject = new Subject($data); |
115
|
|
|
|
116
|
|
|
$errors = $this->validator->validate($subject, null, 'check'); |
117
|
|
|
$response = new \stdClass(); |
118
|
|
|
|
119
|
|
|
if (count($errors) > 0) { |
120
|
|
|
$response = $this->generateResponseFromErrors($errors); |
121
|
|
|
} else { |
122
|
|
|
$subject = new CodiceFiscaleSubject($data); |
123
|
|
|
|
124
|
|
|
$checker = new Checker($subject, array( |
125
|
|
|
'codiceFiscaleToCheck' => $data['codiceFiscale'], |
126
|
|
|
'omocodiaLevel' => $data['omocodiaLevel'], |
127
|
|
|
)); |
128
|
|
|
|
129
|
|
|
if ($checker->check()) { |
130
|
|
|
$response->status = true; |
131
|
|
|
$response->message = 'Valid codice fiscale'; |
132
|
|
|
} else { |
133
|
|
|
$response->status = false; |
134
|
|
|
$response->message = 'Invalid codice fiscale'; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return new JsonResponse($response); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Generate response from the given ConstraintViolationList. |
143
|
|
|
* |
144
|
|
|
* @param $errors The ConstraintViolationList instance. |
145
|
|
|
* |
146
|
|
|
* @return Returns the response from the given errors. |
147
|
|
|
*/ |
148
|
|
|
private function generateResponseFromErrors(\Symfony\Component\Validator\ConstraintViolationList $errors) |
149
|
|
|
{ |
150
|
|
|
$response = new \stdclass(); |
151
|
|
|
$response->status = false; |
152
|
|
|
$responseErrors = array(); |
153
|
|
|
|
154
|
|
|
foreach ($errors as $error) { |
155
|
|
|
$responseErrors[$error->getPropertyPath()] = $error->getMessage(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$response->errors = $responseErrors; |
159
|
|
|
|
160
|
|
|
return $response; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: