diff --git a/api-payroll/src/application/EmployeeApplication.php b/api-payroll/src/application/EmployeeApplication.php index 1b5483a..3909eee 100644 --- a/api-payroll/src/application/EmployeeApplication.php +++ b/api-payroll/src/application/EmployeeApplication.php @@ -505,6 +505,9 @@ class EmployeeApplication{ } /** + * Uses an already existing method to create and array containing the details of + * all currently active employees + * * @return array */ function listAllActiveEmployees(){ @@ -513,10 +516,40 @@ class EmployeeApplication{ $result = array(); foreach($ids as $row){ - $result[] = $this->proxyGetEmployeeDataById($row['id']); + $currentEmployee = $this->proxyGetEmployeeDataById($row['id']); + + $result[] = array( + 'idPerson' => $currentEmployee['idPerson'], + 'fullName' => $currentEmployee['firstName']." ". + $currentEmployee['middleName']." ". + $currentEmployee['lastName'], + 'email' => $currentEmployee['email'], + 'phone' => $currentEmployee['phone'], + 'code' => $currentEmployee['code'], + 'contractType' => $currentEmployee['contractType'] + ); } return $result; } + + /** + * Takes an array of all active employees and filters them by a string, returning + * all sub arrays that contain such string + * + * @param $partialName string + * @return array + */ + function findEmployeeByFullName($partialName){ + $fullList = $this->listAllActiveEmployees(); + + $pattern = '/'.$partialName.'/'; + + $matches = array_filter($fullList, function($a) use($pattern) { + return preg_grep($pattern, $a); + }); + + return $matches; + } } ?> \ No newline at end of file diff --git a/api-payroll/src/routes.php b/api-payroll/src/routes.php index 1bef8c0..6a2fc5b 100644 --- a/api-payroll/src/routes.php +++ b/api-payroll/src/routes.php @@ -41,10 +41,12 @@ $app->get('/api/employee/types', function (Request $request, Response $response, ->write(json_encode($this->employeeApplication->listEmployeeTypes())); }); -$app->get('/api/employee/all', function (Request $request, Response $response, array $args) { +$app->get('/api/employee/find/{partialName}', function (Request $request, Response $response, array $args) { + $partialName = $args['partialName']; + return $response->withStatus(200) ->withHeader('Content-Type', 'application/json') - ->write(json_encode($this->employeeApplication->listAllActiveEmployees())); + ->write(json_encode($this->employeeApplication->findEmployeeByFullName($partialName))); }); $app->post('/api/employee', function ($request, $response) {