You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

51 lines
1.8 KiB

<?php
namespace app\traits;
use app\models\Parametro;
use Slim\Psr7\Request;
use Exception;
trait Validate
{
public function validateData(Request $request, $data = false)
{
$body = json_decode($request->getBody()->getContents(), true);
$parametro = Parametro::find(['org_id' => $body['org_id']], ['prm_max_dias_relatorio']);
$datas = [];
$filters = [
'id', 'uniqueid', 'origem', 'destino',
'fila', 'org_id', 'data_inicial',
'data_final', 'agente', 'apelido',
'nome', 'ramal', 'evento'
];
foreach ($body as $k => $v) {
if (!in_array($k, $filters)) {
throw new Exception("O parametro informado e invalido [$k]!");
}
}
if ($data) {
$datas['data_inicial'] = $body['data_inicial'] ?? date('Y-m-d H:i:s');
$datas['data_final'] = $body['data_final'] ?? date('Y-m-d H:i:s');
if (($body['data_inicial'] && $body['data_final']) && strtotime($body['data_inicial']) > strtotime($body['data_final'])) {
throw new Exception('A [data_inicial] nao pode ser maior que [data_final]!');
}
if ($body['data_final'] && strtotime($datas['data_inicial']) > strtotime($body['data_final'])) {
throw new Exception('Informe o parametro de [data_inicial]!');
}
if ($parametro->prm_max_dias_relatorio) {
if (strtotime($datas['data_inicial']) > strtotime($body['data_inicial'] . "+" . $parametro->prm_max_dias_relatorio . "days")) {
throw new Exception("O periodo nao pode ultrapassar " . $parametro->prm_max_dias_relatorio . " dias!");
}
}
}
return $datas;
}
}