Skip to content

Commit

Permalink
update schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRDOrazio committed May 21, 2022
1 parent a4d8bc4 commit 1257b9b
Show file tree
Hide file tree
Showing 13 changed files with 452 additions and 395 deletions.
120 changes: 99 additions & 21 deletions LitCalHealth.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
include_once( 'includes/APICore.php' );
include_once( 'vendor/autoload.php' );

use Swaggest\JsonSchema\InvalidValue;
use Swaggest\JsonSchema\Schema;

if( file_exists("allowedOrigins.php") ) {
Expand Down Expand Up @@ -48,10 +49,12 @@ class LitCalHealth {
"data/propriumdesanctis_ITALY_1983/propriumdesanctis_ITALY_1983.json" => LitSchema::PROPRIUMDESANCTIS,
"data/propriumdesanctis_USA_2011/propriumdesanctis_USA_2011.json" => LitSchema::PROPRIUMDESANCTIS,
"data/propriumdetempore.json" => LitSchema::PROPRIUMDETEMPORE,
"nations/index.json" => LitSchema::INDEX,
"https://litcal.johnromanodorazio.com/api/dev/LitCalMetadata.php" => LitSchema::METADATA
"nations/index.json" => LitSchema::INDEX,
"https://litcal.johnromanodorazio.com/api/dev/LitCalMetadata.php" => LitSchema::METADATA
];

const LitCalBaseUrl = "https://litcal.johnromanodorazio.com/api/dev/LitCalEngine.php";

public APICore $APICore;
//private array $MESSAGES = [];

Expand All @@ -62,53 +65,128 @@ public function __construct(){
}

private function executeValidations() {
$MetadataIsValid = false;
$Metadata = new stdClass();
$result = new stdClass();
$result->messages = [];
foreach( LitCalHealth::DataSchema as $dataPath => $schema ) {
$message = new stdClass();
$data = file_get_contents( $dataPath );
if( $data !== false ) {
$message = new stdClass();
$message->type = "success";
$message->text = "Data file $dataPath exists";
$result->messages = $message;
$message->text = "The Data file $dataPath exists";
$result->messages[] = $message;

$jsonData = json_decode( $data );
if( json_last_error() === JSON_ERROR_NONE ) {
$message = new stdClass();
$message->type = "success";
$message->text = "The Data file $dataPath was successfully decoded as JSON";
$result->messages = $message;

if( $this->validateDataAgainstSchema( $jsonData, $schema ) ) {
$result->messages[] = $message;

$validationResult = $this->validateDataAgainstSchema( $jsonData, $schema );
if( gettype( $validationResult ) === 'boolean' && $validationResult === true ) {
if( $schema === LitSchema::METADATA ) {
$MetadataIsValid = true;
$Metadata = $jsonData->LitCalMetadata;
}
$message = new stdClass();
$message->type = "success";
$message->text = "The Data file $dataPath was successfully validated against the Schema $schema";
$result->messages = $message;
$result->messages[] = $message;
}
else if( gettype( $validationResult === 'object' ) ) {
$result->messages[] = $validationResult;
}
} else {
$message = new stdClass();
$message->type = "error";
$message->text = "There was an error decoding the Data file $dataPath as JSON: " . json_last_error_msg();
$result->messages = $message;
$result->messages[] = $message;
}

} else {
$message = new stdClass();
$message->type = "error";
$message->text = "Data file $dataPath does not exist";
$result->messages = $message;
$result->messages[] = $message;
}
}
if( $MetadataIsValid ) {
$NationalCalendars = [];
$DiocesanCalendars = [];
$Years = [];
foreach( $Metadata->NationalCalendars as $key => $value ){
array_push( $NationalCalendars, $key );
array_push( $DiocesanCalendars, ...$value );
}
for( $i=10; $i>0; $i-- ) {
array_push( $Years, rand(1970,9999) );
}
}
$result = $this->validateCalendars( $NationalCalendars, $Years, 'nationalcalendar', $result );
$result = $this->validateCalendars( $DiocesanCalendars, $Years, 'diocesancalendar', $result );
die( json_encode( $result ) );
}

private function validateDataAgainstSchema( object|array $data, string $schemaUrl ) : bool {
$result = new stdClass();
$schema = Schema::import( $schemaUrl );
private function validateCalendars( array $Calendars, array $Years, string $type, object $result ) : object {
foreach( $Calendars as $Calendar ) {
foreach( $Years as $Year ) {
$req = "?$type=$Calendar&year=$Year";
$data = file_get_contents( self::LitCalBaseUrl . $req );
if( $data !== false ) {
$message = new stdClass();
$message->type = "success";
$message->text = "The $type of $Calendar exists";
$result->messages[] = $message;

$jsonData = json_decode( $data );
if( json_last_error() === JSON_ERROR_NONE ) {
$message = new stdClass();
$message->type = "success";
$message->text = "The $type of $Calendar was successfully decoded as JSON";
$result->messages[] = $message;

$validationResult = $this->validateDataAgainstSchema( $jsonData, LitSchema::LITCAL );
if( gettype( $validationResult ) === 'boolean' && $validationResult === true ) {
$message = new stdClass();
$message->type = "success";
$message->text = "The $type of $Calendar was successfully validated against the Schema " . LitSchema::LITCAL;
$result->messages[] = $message;
}
else if( gettype( $validationResult === 'object' ) ) {
$result->messages[] = $validationResult;
}
} else {
$message = new stdClass();
$message->type = "error";
$message->text = "There was an error decoding the $type of $Calendar from the URL " . self::LitCalBaseUrl . $req . " as JSON: " . json_last_error_msg();
$result->messages[] = $message;
}
} else {
$message = new stdClass();
$message->type = "error";
$message->text = "The $type of $Calendar does not exist at the URL " . self::LitCalBaseUrl . $req;
$result->messages[] = $message;
}
}
}
return $result;
}

private function validateDataAgainstSchema( object|array $data, string $schemaUrl ) : bool|object {
$res = false;
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 ) );
$schema = Schema::import( $schemaUrl );
$schema->in($data);
$res = true;
} catch (InvalidValue|Exception $e) {
$message = new stdClass();
$message->type = "error";
$message->text = LitSchema::ERROR_MESSAGES[ $schemaUrl ] . PHP_EOL . $e->getMessage();
return $message;
}
return $res;
}

private function handleRequestedMethod() {
Expand Down Expand Up @@ -149,4 +227,4 @@ public function Init() {
$this->handleRequestedMethod();
}

}
}
2 changes: 1 addition & 1 deletion LitCalMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"DiocesanGroups" => $diocesanGroups,
"WiderRegions" => $widerRegionsNames,
"RomanMissals" => RomanMissal::produceMetadata()
],
]
], JSON_PRETTY_PRINT );
$responseHash = md5( $response );
header("Etag: \"{$responseHash}\"");
Expand Down
11 changes: 7 additions & 4 deletions includes/Festivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
include_once( 'enums/LitCommon.php' );
include_once( 'enums/LitFeastType.php' );
include_once( 'enums/LitGrade.php' );
include_once( 'LitDateTime.php' );

class Festivity implements JsonSerializable
{
public static $eventIdx = 0;

public int $idx;
public string $name;
public DateTime $date;
public LitDateTime $date;
public array $color;
public string $type;
public int $grade;
Expand All @@ -27,7 +28,7 @@ class Festivity implements JsonSerializable
public ?bool $hasVesperII = null;
public ?int $psalterWeek = null;

function __construct(string $name, DateTime $date, string|array $color = [ '???' ], string $type = '???', int $grade = LitGrade::WEEKDAY, string|array $common = [ '???' ], string $displayGrade='')
function __construct(string $name, LitDateTime $date, string|array $color = [ '???' ], string $type = '???', int $grade = LitGrade::WEEKDAY, string|array $common = [ '???' ], string $displayGrade='')
{
$this->idx = self::$eventIdx++;
$this->name = $name;
Expand Down Expand Up @@ -61,10 +62,11 @@ function __construct(string $name, DateTime $date, string|array $color = [ '???'
}
}
}

/*
private static function debugWrite( string $string ) {
file_put_contents( "debug.log", $string . PHP_EOL, FILE_APPEND );
}
*/

/* * * * * * * * * * * * * * * * * * * * * * * * *
* Funzione statica di comparazione
Expand All @@ -87,7 +89,8 @@ public function jsonSerialize() : mixed {
$returnArr = [
'eventIdx' => $this->idx,
'name' => $this->name,
'date' => $this->date->format('U'), //serialize the DateTime object as a PHP timestamp
//serialize the DateTime object as a PHP timestamp (seconds since the Unix Epoch)
'date' => (int) $this->date->format('U'),
'color' => $this->color,
'type' => $this->type,
'grade' => $this->grade,
Expand Down
20 changes: 14 additions & 6 deletions includes/FestivityCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
include_once( 'includes/Festivity.php' );
include_once( 'includes/LitMessages.php' );
include_once( 'includes/LitSettings.php' );
include_once( 'includes/LitDateTime.php' );

class FestivityCollection {

Expand All @@ -12,10 +13,10 @@ class FestivityCollection {
private array $feasts = [];
private array $memorials = [];
private array $WeekdayAdventChristmasLent = [];
private array $WeekdaysEpiphany = [];
private array $SolemnitiesLordBVM = [];
private array $SundaysAdventLentEaster = [];
private array $T = [];
private array $WeekdaysEpiphany = [];
private array $SolemnitiesLordBVM = [];
private array $SundaysAdventLentEaster = [];
private array $T = [];
private IntlDateFormatter $dayOfTheWeek;
private LitSettings $LitSettings;
private LitGrade $LitGrade;
Expand All @@ -24,7 +25,14 @@ class FestivityCollection {

public function __construct( LitSettings $LitSettings ) {
$this->LitSettings = $LitSettings;
$this->dayOfTheWeek = IntlDateFormatter::create( strtolower( $this->LitSettings->Locale ), IntlDateFormatter::FULL, IntlDateFormatter::NONE, 'UTC', IntlDateFormatter::GREGORIAN, "EEEE" );
$this->dayOfTheWeek = IntlDateFormatter::create(
strtolower( $this->LitSettings->Locale ),
IntlDateFormatter::FULL,
IntlDateFormatter::NONE,
'UTC',
IntlDateFormatter::GREGORIAN,
"EEEE"
);
if( $this->LitSettings->Locale === LitLocale::LATIN ) {
$this->T = [
"YEAR" => "ANNUM",
Expand Down Expand Up @@ -241,7 +249,7 @@ private function handleGradeProperty( string $key, int $value, int $oldValue ) :
}

public function setProperty( string $key, string $property, string|int|bool $value ) : bool {
$reflect = new ReflectionClass( new Festivity("test", new DateTime('NOW')) );
$reflect = new ReflectionClass( new Festivity("test", new LitDateTime('NOW')) );
if( array_key_exists( $key, $this->festivities ) ) {
$oldValue = $this->festivities[ $key ]->{$property};
if( $reflect->hasProperty( $property ) ) {
Expand Down

0 comments on commit 1257b9b

Please sign in to comment.