Skip to content

Commit

Permalink
add JSON schema
Browse files Browse the repository at this point in the history
  • Loading branch information
cevro committed Sep 22, 2023
1 parent 7969030 commit 37022e5
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 13 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
88 changes: 80 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 99 additions & 0 deletions src/Downloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

declare(strict_types=1);

namespace Fykosak\FKSDBDownloaderCore;

use JsonSchema\Constraints\Constraint;
use JsonSchema\Validator;

require __DIR__ . '/../vendor/autoload.php';

class Downloader
{
private array $config;

public function __construct(array $config)
{
$this->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);
}
}
9 changes: 9 additions & 0 deletions src/DownloaderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Fykosak\FKSDBDownloaderCore;

class DownloaderException extends \Exception
{
}
15 changes: 15 additions & 0 deletions src/fksdb/GetOrganizers/request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": [
"contest_id"
],
"properties": {
"contest_id": {
"type": "integer"
},
"year": {
"type": "integer"
}
}
}
78 changes: 78 additions & 0 deletions src/fksdb/GetOrganizers/response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"personId": {
"type": "integer",
"min": 0
},
"email": {
"type": [
"null",
"string"
]
},
"academicDegreePrefix": {
"type": [
"string",
"null"
]
},
"academicDegreeSuffix": {
"type": [
"string",
"null"
]
},
"career": {
"type": [
"string",
"null"
]
},
"contribution": {
"type": [
"string",
"null"
]
},
"order": {
"type": "integer",
"min": 0,
"max": 10
},
"role": {
"type": [
"string",
"null"
]
},
"since": {
"type": "integer"
},
"until": {
"type": [
"integer",
"null"
]
},
"texSignature": {
"type": [
"string",
"null"
]
},
"domainAlias": {
"type": [
"string",
"null"
]
}
}
}
}
17 changes: 13 additions & 4 deletions tools/downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@

declare(strict_types=1);

use Fykosak\FKSDBDownloaderCore\Downloader;
use Fykosak\FKSDBDownloaderCore\FKSDBDownloader;
use Fykosak\FKSDBDownloaderCore\Requests\Request;

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/pass.php';

$downloader = new FKSDBDownloader(FKSDB_WSDL, FKSDB_USER, FKSDB_PASS, FKSDB_API);

return function (Request $request, bool $soap = true) use ($downloader): ?string {
$newDownloader = new Downloader([
'fksdb' => [
'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');
Expand All @@ -23,4 +32,4 @@
var_dump($exception);
}
return null;
};
};*/

0 comments on commit 37022e5

Please sign in to comment.