terrarium-web/src/Validator/ApiValidator.php

48 lines
847 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;
}
return true;
}
}