/** * is the given filename pointing at valid JSON? * * @param mixed $path * the filename to inspect * @return boolean * TRUE if the file is valid JSON * FALSE otherwise */ public static function check($path) { // defensive programming!! RequireStringy::check($path, E4xx_UnsupportedType::class); if (!IsReadableFile::check($path)) { return false; } $contents = file_get_contents($path); if (empty($contents)) { return false; } $payload = json_decode($contents); if ($payload === null) { return false; } return true; }
/** * is $path a file, and can we run it? * * @param string $path * the file to check * @return void * * @throws E4xx_InvalidPath * if $path is not a file, or otherwise does not exist * @throws E4xx_FileIsNotExecutable * if $path is a file, but we do not have permissions to run it */ public static function check($path) { // robustness RequireStringy::check($path); // do we have a file? if (!IsFile::check($path)) { throw new E4xx_InvalidPath($path); } // can we read it? if (!IsReadableFile::check($path)) { throw new E4xx_FileIsNotExecutable($path); } }