52 lines
926 B
PHP
52 lines
926 B
PHP
<?php
|
|
|
|
namespace App\Validator;
|
|
|
|
/**
|
|
* class ApiValidator.
|
|
*
|
|
* @author Simon Vieille <simon@deblan.fr>
|
|
*/
|
|
class ApiValidator
|
|
{
|
|
/**
|
|
* isValidContent
|
|
*
|
|
* @param string|null $content
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isValidContent(? string $content): bool
|
|
{
|
|
$json = json_decode($content, true);
|
|
|
|
if (!is_array($json)) {
|
|
return false;
|
|
}
|
|
|
|
if (count($json) !== 2) {
|
|
return false;
|
|
}
|
|
|
|
if (!isset($json['date'], $json['value'])) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
new \DateTime($json['date']);
|
|
} catch (\Exception $e) {
|
|
return false;
|
|
}
|
|
|
|
if (filter_var($json['value'], FILTER_VALIDATE_FLOAT) === null) {
|
|
return false;
|
|
}
|
|
|
|
if ((float) $json['value'] < 1) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|