diff --git a/composer.json b/composer.json index 302b9ca..16f54b1 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "require": { "php": ">= 7.4", "ext-dom": "*", - "ext-soap": "*" + "ext-soap": "*", + "justinrainbow/json-schema": "5.2.12" }, "require-dev": { "squizlabs/php_codesniffer": "^3.6.0" diff --git a/composer.lock b/composer.lock index 83cc9f6..fc09151 100644 --- a/composer.lock +++ b/composer.lock @@ -4,21 +4,92 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "b074362b32310c1261001900cd7dc81c", - "packages": [], + "content-hash": "a0a67ee09c999329c4395b2e94bc0d65", + "packages": [ + { + "name": "justinrainbow/json-schema", + "version": "5.2.12", + "source": { + "type": "git", + "url": "https://github.com/justinrainbow/json-schema.git", + "reference": "ad87d5a5ca981228e0e205c2bc7dfb8e24559b60" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/justinrainbow/json-schema/zipball/ad87d5a5ca981228e0e205c2bc7dfb8e24559b60", + "reference": "ad87d5a5ca981228e0e205c2bc7dfb8e24559b60", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "~2.2.20||~2.15.1", + "json-schema/json-schema-test-suite": "1.2.0", + "phpunit/phpunit": "^4.8.35" + }, + "bin": [ + "bin/validate-json" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "JsonSchema\\": "src/JsonSchema/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bruno Prieto Reis", + "email": "bruno.p.reis@gmail.com" + }, + { + "name": "Justin Rainbow", + "email": "justin.rainbow@gmail.com" + }, + { + "name": "Igor Wiedler", + "email": "igor@wiedler.ch" + }, + { + "name": "Robert Schönthal", + "email": "seroscho@googlemail.com" + } + ], + "description": "A library to validate a json schema.", + "homepage": "https://github.com/justinrainbow/json-schema", + "keywords": [ + "json", + "schema" + ], + "support": { + "issues": "https://github.com/justinrainbow/json-schema/issues", + "source": "https://github.com/justinrainbow/json-schema/tree/5.2.12" + }, + "time": "2022-04-13T08:02:27+00:00" + } + ], "packages-dev": [ { "name": "squizlabs/php_codesniffer", - "version": "3.6.2", + "version": "3.7.2", "source": { "type": "git", "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "5e4e71592f69da17871dba6e80dd51bce74a351a" + "reference": "ed8e00df0a83aa96acf703f8c2979ff33341f879" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/5e4e71592f69da17871dba6e80dd51bce74a351a", - "reference": "5e4e71592f69da17871dba6e80dd51bce74a351a", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/ed8e00df0a83aa96acf703f8c2979ff33341f879", + "reference": "ed8e00df0a83aa96acf703f8c2979ff33341f879", "shasum": "" }, "require": { @@ -54,14 +125,15 @@ "homepage": "https://github.com/squizlabs/PHP_CodeSniffer", "keywords": [ "phpcs", - "standards" + "standards", + "static analysis" ], "support": { "issues": "https://github.com/squizlabs/PHP_CodeSniffer/issues", "source": "https://github.com/squizlabs/PHP_CodeSniffer", "wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki" }, - "time": "2021-12-12T21:44:58+00:00" + "time": "2023-02-22T23:07:41+00:00" } ], "aliases": [], diff --git a/src/Downloader.php b/src/Downloader.php new file mode 100644 index 0000000..c9e1e8d --- /dev/null +++ b/src/Downloader.php @@ -0,0 +1,99 @@ +config = $config; + } + + /** + * @return mixed + * @throws DownloaderException + */ + public function download(string $app, string $source, array $arguments) + { + $config = $this->config[$app]; + $this->validateRequest($arguments, $app, $source); + + $options = [ + 'http' => [ + 'header' => "Content-type: application/x-www-form-urlencoded\r\n" . + "Authorization: Basic " . base64_encode($config['username'] . ':' . $config['password']), + 'method' => 'GET', + 'content' => http_build_query($arguments), + ], + ]; + $context = stream_context_create($options); + + $result = file_get_contents( + $this->config[$app]['url'] . $source + . '?' . http_build_query($arguments), + false, + $context + ); + if ($result === false) { + restore_error_handler(); + throw new DownloaderException(error_get_last()['message']); + } + $this->validateResponse(json_decode($result), $app, $source); + return $result; + } + + /** + * @throws DownloaderException + */ + private function validateRequest(array $arguments, string $app, string $source): void + { + $this->validate(__DIR__ . DIRECTORY_SEPARATOR . + $app . DIRECTORY_SEPARATOR . + $source . DIRECTORY_SEPARATOR . 'request.json', $arguments); + } + + /** + * @throws DownloaderException + */ + private function validate(string $schemaFile, array $arguments): void + { + try { + $validator = new Validator(); + $validator->validate($arguments, (object)[ + '$ref' => 'file://' . $schemaFile, + ], Constraint::CHECK_MODE_TYPE_CAST); + + if ($validator->isValid()) { + return; + } else { + $errors = ''; + foreach ($validator->getErrors() as $error) { + var_dump($error); + $errors .= join(', ', $error); + } + throw new DownloaderException($errors); + } + } catch (\Throwable$exception) { + throw new DownloaderException($exception->getMessage(), $exception->getCode(), $exception); + } + } + + /** + * @throws DownloaderException + */ + private function validateResponse(array $arguments, string $app, string $source): void + { + $this->validate(__DIR__ . DIRECTORY_SEPARATOR . + $app . DIRECTORY_SEPARATOR . + $source . DIRECTORY_SEPARATOR . 'response.json', $arguments); + } +} diff --git a/src/DownloaderException.php b/src/DownloaderException.php new file mode 100644 index 0000000..7a572cc --- /dev/null +++ b/src/DownloaderException.php @@ -0,0 +1,9 @@ + [ + 'url' => FKSDB_API, + 'username' => FKSDB_USER, + 'password' => FKSDB_PASS, + ], +]); +echo $newDownloader->download('fksdb', 'GetOrganizers', ['contest_id' => 1, 'year' => 37]); +return $newDownloader; +/* +return function (Request $request, bool $soap = true) use ($new): ?string { try { if ($soap) { header('Content-Type: text/xml'); @@ -23,4 +32,4 @@ var_dump($exception); } return null; -}; +};*/