Skip to content

Commit

Permalink
create LitSchema and implement schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRDOrazio committed May 13, 2022
1 parent 3938a27 commit 85702b0
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 10 deletions.
66 changes: 56 additions & 10 deletions LitCalRegionalData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
ini_set( 'display_errors', 1 );

include_once( 'includes/enums/AcceptHeader.php' );
include_once( 'includes/enums/LitSchema.php' );
include_once( 'includes/enums/RequestMethod.php' );
include_once( 'includes/enums/RequestContentType.php' );
include_once( 'includes/enums/ReturnType.php' );
include_once( 'includes/APICore.php' );
include_once( 'vendor/autoload.php' );

use Swaggest\JsonSchema\Schema;

if( file_exists("allowedOrigins.php") ) {
include_once( 'allowedOrigins.php' );
Expand Down Expand Up @@ -156,10 +160,18 @@ private function writeRegionalCalendar() {
if( !file_exists( $path ) ) {
mkdir( $path, 0755, true );
}
$data = json_encode( $this->DATA, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE );
file_put_contents( $path . "/{$region}.json", $data . PHP_EOL );
header( $_SERVER[ "SERVER_PROTOCOL" ]." 201 Created", true, 201 );
die( '{"success":"National calendar created or updated for nation \"'. $this->DATA->Metadata->Region .'\""}' );

$test = $this->validateDataAgainstSchema( $this->DATA, LitSchema::NATIONAL );
if( $test === true ) {
$data = json_encode( $this->DATA, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE );
file_put_contents( $path . "/{$region}.json", $data . PHP_EOL );
header( $_SERVER[ "SERVER_PROTOCOL" ]." 201 Created", true, 201 );
die( '{"success":"National calendar created or updated for nation \"'. $this->DATA->Metadata->Region .'\""}' );
} else {
header( $_SERVER[ "SERVER_PROTOCOL" ]." 422 Unprocessable Entity", true, 422 );
die( json_encode( $test ) );
}

}
else if ( property_exists( $this->DATA, 'LitCal' ) && property_exists( $this->DATA, 'Metadata' ) && property_exists( $this->DATA, 'NationalCalendars' ) ) {
$this->DATA->Metadata->WiderRegion = ucfirst( strtolower( $this->DATA->Metadata->WiderRegion ) );
Expand All @@ -181,10 +193,18 @@ private function writeRegionalCalendar() {
}
}
}
$data = json_encode( $this->DATA, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE );
file_put_contents( "nations/{$this->DATA->Metadata->WiderRegion}.json", $data . PHP_EOL );
header( $_SERVER[ "SERVER_PROTOCOL" ]." 201 Created", true, 201 );
die( '{"success":"Wider region calendar created or updated for region \"'. $this->DATA->Metadata->WiderRegion .'\""}' );

$test = $this->validateDataAgainstSchema( $this->DATA, LitSchema::WIDERREGION );
if( $test === true ) {
$data = json_encode( $this->DATA, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE );
file_put_contents( "nations/{$this->DATA->Metadata->WiderRegion}.json", $data . PHP_EOL );
header( $_SERVER[ "SERVER_PROTOCOL" ]." 201 Created", true, 201 );
die( '{"success":"Wider region calendar created or updated for region \"'. $this->DATA->Metadata->WiderRegion .'\""}' );
} else {
header( $_SERVER[ "SERVER_PROTOCOL" ]." 422 Unprocessable Entity", true, 422 );
die( json_encode( $test ) );
}

}
else if ( property_exists( $this->DATA, 'LitCal' ) && property_exists( $this->DATA, 'Diocese' ) && property_exists( $this->DATA, 'Nation' ) ) {
$this->RESPONSE->Nation = strip_tags( $this->DATA->Nation );
Expand All @@ -206,7 +226,13 @@ private function writeRegionalCalendar() {
mkdir( $path, 0755, true );
}

file_put_contents( $path . "/{$this->RESPONSE->Diocese}.json", $this->RESPONSE->Calendar . PHP_EOL );
$test = $this->validateDataAgainstSchema( $CalData, LitSchema::DIOCESAN );
if( $test === true ) {
file_put_contents( $path . "/{$this->RESPONSE->Diocese}.json", $this->RESPONSE->Calendar . PHP_EOL );
} else {
header( $_SERVER[ "SERVER_PROTOCOL" ]." 422 Unprocessable Entity", true, 422 );
die( json_encode( $test ) );
}

$this->createOrUpdateIndex( $path );
header( $_SERVER[ "SERVER_PROTOCOL" ]." 201 Created", true, 201 );
Expand Down Expand Up @@ -266,7 +292,27 @@ private function createOrUpdateIndex( string $path, bool $delete = false ) {
}
}

file_put_contents( "nations/index.json", json_encode( $this->GeneralIndex, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE ) . PHP_EOL );
$test = $this->validateDataAgainstSchema( $this->GeneralIndex, LitSchema::INDEX );
if( $test === true ) {
file_put_contents( "nations/index.json", json_encode( $this->GeneralIndex, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE ) . PHP_EOL );
} else {
header( $_SERVER[ "SERVER_PROTOCOL" ]." 422 Unprocessable Entity", true, 422 );
die( json_encode( $test ) );
}

}

private function validateDataAgainstSchema( object $data, string $schemaUrl ) : bool {
$result = new stdClass();
$schema = Schema::import( $schemaUrl );
try {
$validation = $schema->in($data);
return true;
} catch (Exception $e) {
$result->error = LitSchema::ERROR_MESSAGES[ $schemaUrl ] . PHP_EOL . $e->getMessage();
header( $_SERVER[ "SERVER_PROTOCOL" ]." 422 Unprocessable Entity", true, 422 );
die( json_encode( $result ) );
}
}

public function Init() {
Expand Down
23 changes: 23 additions & 0 deletions includes/enums/LitSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

class LitSchema {

const INDEX = "https://litcal.org/api/dev/schemas/Index.json";
const DIOCESAN = "https://litcal.org/api/dev/schemas/DiocesanCalendar.json";
const NATIONAL = "https://litcal.org/api/dev/schemas/NationalCalendar.json";
const PROPRIUMDESANCTIS = "https://litcal.org/api/dev/schemas/PropriumDeSanctis.json";
const WIDERREGION = "https://litcal.org/api/dev/schemas/WiderRegionCalendar.json";
const DECREEMEMORIALS = "https://litcal.org/api/dev/schemas/MemorialsFromDecrees.json";
const I18N = "https://litcal.org/api/dev/schemas/LitCalTranslation.json";

const ERROR_MESSAGES = [
LitSchema::INDEX => "Schema validation error: Index not updated",
LitSchema::DIOCESAN => "Schema validation error: Diocesan Calendar not created / updated",
LitSchema::NATIONAL => "Schema validation error: National Calendar not created / updated",
LitSchema::PROPRIUMDESANCTIS => "Schema validation error: Proprium de Sanctis data not created / updated",
LitSchema::WIDERREGION => "Schema validation error: Wider Region data not created / updated",
LitSchema::DECREEMEMORIALS => "Schema validation error: Memorials from Decrees data not created / updated",
LitSchema::I18N => "Schema validation error: Translation data not created / updated"
];

}

0 comments on commit 85702b0

Please sign in to comment.