From 4f58038b1bbc0ea9cc70848927804e581b199312 Mon Sep 17 00:00:00 2001 From: "John R. D'Orazio" Date: Sat, 22 Jun 2024 20:29:07 +0200 Subject: [PATCH] create composer package --- .gitignore | 2 + calculator.php | 8 - composer.json | 23 ++ composer.lock | 99 +++++++ includes/AnniversaryCalculator.php | 251 ----------------- includes/enums/AnnivType.php | 43 --- includes/enums/AreaInterest.php | 46 ---- includes/enums/LitCalendar.php | 36 --- includes/pgettext.php | 13 - index.php | 8 + phpcs.xml | 12 + src/AnniversaryCalculator.php | 260 ++++++++++++++++++ src/AnniversaryCalculator/Enums/AnnivType.php | 49 ++++ .../Enums/AreaInterest.php | 52 ++++ .../Enums/LitCalendar.php | 41 +++ .../AnniversaryCalculator}/LitEvent.php | 48 ++-- src/pgettext.php | 16 ++ 17 files changed, 588 insertions(+), 419 deletions(-) delete mode 100644 calculator.php create mode 100644 composer.json create mode 100644 composer.lock delete mode 100644 includes/AnniversaryCalculator.php delete mode 100644 includes/enums/AnnivType.php delete mode 100644 includes/enums/AreaInterest.php delete mode 100644 includes/enums/LitCalendar.php delete mode 100644 includes/pgettext.php create mode 100644 index.php create mode 100644 phpcs.xml create mode 100644 src/AnniversaryCalculator.php create mode 100644 src/AnniversaryCalculator/Enums/AnnivType.php create mode 100644 src/AnniversaryCalculator/Enums/AreaInterest.php create mode 100644 src/AnniversaryCalculator/Enums/LitCalendar.php rename {includes => src/AnniversaryCalculator}/LitEvent.php (77%) create mode 100644 src/pgettext.php diff --git a/.gitignore b/.gitignore index b6491501..6d0a5a6e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ dbcredentials.php + +/vendor/ diff --git a/calculator.php b/calculator.php deleted file mode 100644 index a5e2446f..00000000 --- a/calculator.php +++ /dev/null @@ -1,8 +0,0 @@ -Init(); - -?> diff --git a/composer.json b/composer.json new file mode 100644 index 00000000..f523ee6c --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "litcal/anniversarycalculator", + "description": "an API that calculates anniversaries of liturgical recurrences from the General Roman Calendar", + "type": "library", + "require-dev": { + "squizlabs/php_codesniffer": "^3.10" + }, + "license": "MIT", + "autoload": { + "psr-4": { + "LitCal\\": "src/" + }, + "files": ["src/pgettext.php"] + }, + "authors": [ + { + "name": "John R. D'Orazio", + "email": "priest@johnromanodorazio.com" + } + ], + "minimum-stability": "stable", + "require": {} +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 00000000..89abbd19 --- /dev/null +++ b/composer.lock @@ -0,0 +1,99 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "4d33a036a62cbdacd754741d9fe993fe", + "packages": [], + "packages-dev": [ + { + "name": "squizlabs/php_codesniffer", + "version": "3.10.1", + "source": { + "type": "git", + "url": "https://github.com/PHPCSStandards/PHP_CodeSniffer.git", + "reference": "8f90f7a53ce271935282967f53d0894f8f1ff877" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPCSStandards/PHP_CodeSniffer/zipball/8f90f7a53ce271935282967f53d0894f8f1ff877", + "reference": "8f90f7a53ce271935282967f53d0894f8f1ff877", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.3.4" + }, + "bin": [ + "bin/phpcbf", + "bin/phpcs" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "Former lead" + }, + { + "name": "Juliette Reinders Folmer", + "role": "Current lead" + }, + { + "name": "Contributors", + "homepage": "https://github.com/PHPCSStandards/PHP_CodeSniffer/graphs/contributors" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "https://github.com/PHPCSStandards/PHP_CodeSniffer", + "keywords": [ + "phpcs", + "standards", + "static analysis" + ], + "support": { + "issues": "https://github.com/PHPCSStandards/PHP_CodeSniffer/issues", + "security": "https://github.com/PHPCSStandards/PHP_CodeSniffer/security/policy", + "source": "https://github.com/PHPCSStandards/PHP_CodeSniffer", + "wiki": "https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki" + }, + "funding": [ + { + "url": "https://github.com/PHPCSStandards", + "type": "github" + }, + { + "url": "https://github.com/jrfnl", + "type": "github" + }, + { + "url": "https://opencollective.com/php_codesniffer", + "type": "open_collective" + } + ], + "time": "2024-05-22T21:24:41+00:00" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.6.0" +} diff --git a/includes/AnniversaryCalculator.php b/includes/AnniversaryCalculator.php deleted file mode 100644 index dd9795ee..00000000 --- a/includes/AnniversaryCalculator.php +++ /dev/null @@ -1,251 +0,0 @@ -requestHeaders = getallheaders(); - //$this->jsonEncodedRequestHeaders = json_encode( $this->requestHeaders ); - $this->acceptHeader = isset( $this->requestHeaders["Accept"] ) && in_array( $this->requestHeaders["Accept"], self::ALLOWED_ACCEPT_HEADERS ) ? ( string ) $this->requestHeaders["Accept"] : ""; - $this->RESPONSE = new stdClass(); - $this->RESPONSE->LitEvents = []; - $this->RESPONSE->Messages = [ "Anniversary Calculator instantiated" ]; - } - - public function Init() { - self::allowFromAnyOrigin(); - self::setAccessControlAllowMethods(); - self::validateRequestContentType(); - - $this->initParameterData(); - $this->prepareL10N(); - $this->setReponseContentTypeHeader(); - $this->readData(); - $this->outputResults(); - } - - private static function allowFromAnyOrigin() { - if ( isset( $_SERVER['HTTP_ORIGIN'] ) ) { - header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" ); - header( 'Access-Control-Allow-Credentials: true' ); - header( 'Access-Control-Max-Age: 86400' ); // cache for 1 day - } - } - - private static function setAccessControlAllowMethods() { - if ( isset( $_SERVER['REQUEST_METHOD'] ) ) { - if ( isset( $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] ) ) - header( "Access-Control-Allow-Methods: GET, POST" ); - if ( isset( $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'] ) ) - header( "Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}" ); - } - } - - private static function validateRequestContentType() { - if( isset( $_SERVER['CONTENT_TYPE'] ) && $_SERVER['CONTENT_TYPE'] !== '' && !in_array( $_SERVER['CONTENT_TYPE'], self::ALLOWED_CONTENT_TYPES ) ){ - header( $_SERVER["SERVER_PROTOCOL"]." 415 Unsupported Media Type", true, 415 ); - die( '{"error":"You seem to be forming a strange kind of request? Allowed Content Types are '.implode( ' and ',self::ALLOWED_CONTENT_TYPES ).', but your Content Type was '.$_SERVER['CONTENT_TYPE'].'"}' ); - } - } - - private function initParameterData() { - if ( isset( $_SERVER['CONTENT_TYPE'] ) && $_SERVER['CONTENT_TYPE'] === 'application/json' ) { - $json = file_get_contents( 'php://input' ); - $data = json_decode( $json,true ); - if( NULL === $json || "" === $json ){ - header( $_SERVER["SERVER_PROTOCOL"]." 400 Bad Request", true, 400 ); - die( '{"error":"No JSON data received in the request: <' . $json . '>"' ); - } else if ( json_last_error() !== JSON_ERROR_NONE ) { - header( $_SERVER["SERVER_PROTOCOL"]." 400 Bad Request", true, 400 ); - die( '{"error":"Malformed JSON data received in the request: <' . $json . '>, ' . json_last_error_msg() . '"}' ); - } else { - $this->parameterData = $data; - } - } else { - switch( strtoupper( $_SERVER["REQUEST_METHOD"] ) ) { - case 'POST': - $this->parameterData = $_POST; - break; - case 'GET': - $this->parameterData = $_GET; - break; - default: - header( $_SERVER["SERVER_PROTOCOL"]." 405 Method Not Allowed", true, 405 ); - $errorMessage = '{"error":"You seem to be forming a strange kind of request? Allowed Request Methods are '; - $errorMessage .= implode( ' and ', self::ALLOWED_REQUEST_METHODS ); - $errorMessage .= ', but your Request Method was ' . strtoupper( $_SERVER['REQUEST_METHOD'] ) . '"}'; - die( $errorMessage ); - } - } - - if( !isset( $this->parameterData["YEAR"] ) || $this->parameterData["YEAR"] === "" ) { - die( '{"error":"Parametro YEAR non impostato o non valido"}' ); - } - - if( isset( $this->parameterData["LOCALE"] ) && in_array( strtolower( $this->parameterData["LOCALE"] ), self::ALLOWED_LOCALES ) ) { - $this->parameterData["LOCALE"] = strtolower( $this->parameterData["LOCALE"] ); - } else { - $this->parameterData["LOCALE"] = "en"; - } - - $this->responseContentType = ( isset( $this->parameterData["return"] ) && in_array( strtolower( $this->parameterData["return"] ), self::ALLOWED_RETURN_TYPES ) ) ? strtolower( $this->parameterData["return"] ) : ( $this->acceptHeader !== "" ? ( string ) self::ALLOWED_RETURN_TYPES[array_search( $this->requestHeaders["Accept"], self::ALLOWED_ACCEPT_HEADERS )] : ( string ) self::ALLOWED_RETURN_TYPES[0] ); - $this->RESPONSE->Messages[] = "parameter data initialized"; - } - - private function prepareL10N() : void { - $localeArray = [ - $this->parameterData["LOCALE"] . '_' . strtoupper( $this->parameterData["LOCALE"] ) . '.utf8', - $this->parameterData["LOCALE"] . '_' . strtoupper( $this->parameterData["LOCALE"] ) . '.UTF-8', - $this->parameterData["LOCALE"] . '_' . strtoupper( $this->parameterData["LOCALE"] ), - $this->parameterData["LOCALE"] - ]; - setlocale( LC_ALL, $localeArray ); - bindtextdomain("litcal", "i18n"); - textdomain("litcal"); - } - - private function setReponseContentTypeHeader() { - switch( $this->responseContentType ){ - case "xml": - header( 'Content-Type: application/xml; charset=utf-8' ); - break; - case "json": - header( 'Content-Type: application/json; charset=utf-8' ); - break; - case "html": - header( 'Content-Type: text/html; charset=utf-8' ); - break; - default: - header( 'Content-Type: application/json; charset=utf-8' ); - } - $this->RESPONSE->Messages[] = "Response Content-Type header set"; - } - - private function readData() { - if( file_exists( "./data/LITURGY__anniversari.json" ) ) { - if( file_exists( "./data/i18n/{$this->parameterData["LOCALE"]}.json" ) ) { - $lclData = json_decode( file_get_contents( "./data/i18n/{$this->parameterData["LOCALE"]}.json" ) ); - $results = json_decode( file_get_contents( "./data/LITURGY__anniversari.json" ) ); - $this->RESPONSE->Messages[] = "localized data events loaded: " . count(get_object_vars( $lclData )); - $this->RESPONSE->Messages[] = "base data events loaded: " . count( $results ); - foreach( $lclData as $label => $lclRow ) { - list( $TAG, $IDX ) = explode( "_", $label ); - foreach( $results as $idx => $obj ) { - if( $obj->TAG === $TAG && $obj->IDX === intval( $IDX ) ) { - $litEvent = new LitEvent( array_merge( (array) $results[$idx], (array) $lclRow ), $this->parameterData["LOCALE"] ); - if( $litEvent->year !== null && $this->isAnniversary( $litEvent ) ) { - $this->RESPONSE->LitEvents[] = $litEvent; - } - } - } - - $props = [ - "month" => 2, - "day" => 1 - ]; - usort( $this->RESPONSE->LitEvents, function($a, $b) use ($props){ - foreach( $props as $key => $val ){ - if( $a->$key == $b->$key ) continue; - return $a->$key > $b->$key ? $val : -($val); - } - return 0; - }); - - } - $this->RESPONSE->Messages[] = count($this->RESPONSE->LitEvents) . " data rows calculated"; - } else { - $this->RESPONSE->Messages[] = "missing file ./data/i18n/{$this->parameterData["LOCALE"]}.json"; - } - } else { - $this->RESPONSE->Messages[] = "missing file ./data/LITURGY__anniversari.json"; - } - } - - - private function isAnniversary( LitEvent $litEvent ) : bool { - - $yearDiff = $this->parameterData["YEAR"] - $litEvent->anno; - $litEvent->setYearDiff( $yearDiff ); - - foreach( LitEvent::ANNIVERSARY as $key => $value ) { - - if ( in_array( $key, self::RECURRING ) ) { - - if( $key === "CENTENARY" ) { - - if( $yearDiff % LitEvent::ANNIVERSARY["CENTENARY"] === 0 ) { - $litEvent->setAnniversary( LitEvent::ANNIVERSARY["CENTENARY"] ); - return true; - } - - } - - $lastTwoDigits = substr((string)$yearDiff, -2); - if( $key === array_search( (int)$lastTwoDigits, LitEvent::ANNIVERSARY ) ){ - $litEvent->setAnniversary( LitEvent::ANNIVERSARY[$key] ); - return true; - } - } else { - $arraySearch = array_search( $yearDiff, LitEvent::ANNIVERSARY ); - if( $arraySearch !== false ) { - $litEvent->setAnniversary( $yearDiff ); - return true; - } - - } - - } - - return false; - - } - - private function outputResults() { - - echo json_encode( $this->RESPONSE, JSON_UNESCAPED_UNICODE ); - exit( 0 ); - - } - -} diff --git a/includes/enums/AnnivType.php b/includes/enums/AnnivType.php deleted file mode 100644 index e91a500c..00000000 --- a/includes/enums/AnnivType.php +++ /dev/null @@ -1,43 +0,0 @@ -locale = strtoupper( $locale ); - $this->GTXT = [ - self::BIRTH => strtoupper( _( "birth" ) ), - self::DEATH => strtoupper( _( "death" ) ), - self::CANONIZATION => strtoupper( _( "canonization" ) ), - self::DOCTOR => strtoupper( _( "doctor" ) ), - self::DEDICATION => strtoupper( _( "dedication" ) ), - /**translators: term "translation" refers to the transferral of the relics of a saint */ - self::TRANSLATION => strtoupper( _( "translation" ) ), - self::OTHER => strtoupper( _( "other" ) ) - ]; - } - - public static function isValid( string $value ) { - return in_array( $value, self::$values ); - } - - public function i18n( string $value ) : string { - if( self::isValid( $value ) ) { - return $this->GTXT[ $value ]; - } - return $value; - } - -} diff --git a/includes/enums/AreaInterest.php b/includes/enums/AreaInterest.php deleted file mode 100644 index 5a1a27ab..00000000 --- a/includes/enums/AreaInterest.php +++ /dev/null @@ -1,46 +0,0 @@ -locale = strtoupper( $locale ); - $this->GTXT = [ - self::ROME => strtoupper( _( "rome" ) ), - self::ITALY => strtoupper( _( "italy" ) ), - self::WORLD => strtoupper( _( "world" ) ), - self::BIBLICAL => strtoupper( _( "biblical" ) ), - self::MARIAN => strtoupper( _( "marian" ) ) - ]; - } - - public static function isValid( string $value ) { - return in_array( $value, self::$values ); - } - - public static function areValid( array $values ) { - return empty( array_diff( $values, self::$values ) ); - } - - public function i18n( string|array $value ) : string|array { - if( is_array( $value ) && self::areValid( $value ) ) { - return array_map( array( $this, 'i18n' ), $value ); - } else { - if( self::isValid( $value ) ) { - return $this->GTXT[ $value ]; - } - } - return $value; - } - -} diff --git a/includes/enums/LitCalendar.php b/includes/enums/LitCalendar.php deleted file mode 100644 index 55177bb5..00000000 --- a/includes/enums/LitCalendar.php +++ /dev/null @@ -1,36 +0,0 @@ -locale = strtoupper( $locale ); - $this->GTXT = [ - self::UNIVERSAL => strtoupper( _( "universal" ) ), - self::NATIONAL => strtoupper( _( "national" ) ), - self::DIOCESAN => strtoupper( _( "diocesan" ) ), - self::WIDE_AREA => strtoupper( _( "wide_area" ) ) - ]; - } - - public static function isValid( string $value ) { - return in_array( $value, self::$values ); - } - - public function i18n( string $value ) : string { - if( self::isValid( $value ) ) { - return $this->GTXT[ $value ]; - } - return $value; - } - -} diff --git a/includes/pgettext.php b/includes/pgettext.php deleted file mode 100644 index da60ca46..00000000 --- a/includes/pgettext.php +++ /dev/null @@ -1,13 +0,0 @@ -init(); diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 00000000..dcafce17 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,12 @@ + + + + + + ./ + /vendor/ + *Test.php + allowedOrigins.php + + + diff --git a/src/AnniversaryCalculator.php b/src/AnniversaryCalculator.php new file mode 100644 index 00000000..0f42f2ad --- /dev/null +++ b/src/AnniversaryCalculator.php @@ -0,0 +1,260 @@ +requestHeaders = getallheaders(); + //$this->jsonEncodedRequestHeaders = json_encode( $this->requestHeaders ); + $this->acceptHeader = isset($this->requestHeaders["Accept"]) && in_array($this->requestHeaders["Accept"], self::ALLOWED_ACCEPT_HEADERS) ? (string) $this->requestHeaders["Accept"] : ""; + $this->RESPONSE = new \stdClass(); + $this->RESPONSE->LitEvents = []; + $this->RESPONSE->Messages = [ "Anniversary Calculator instantiated" ]; + } + + public function init() + { + self::allowFromAnyOrigin(); + self::setAccessControlAllowMethods(); + self::validateRequestContentType(); + + $this->initParameterData(); + $this->prepareL10N(); + $this->setReponseContentTypeHeader(); + $this->readData(); + $this->outputResults(); + } + + private static function allowFromAnyOrigin() + { + if (isset($_SERVER['HTTP_ORIGIN'])) { + header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}"); + header('Access-Control-Allow-Credentials: true'); + header('Access-Control-Max-Age: 86400'); // cache for 1 day + } + } + + private static function setAccessControlAllowMethods() + { + if (isset($_SERVER['REQUEST_METHOD'])) { + if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) { + header("Access-Control-Allow-Methods: GET, POST"); + } + if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) { + header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"); + } + } + } + + private static function validateRequestContentType() + { + if (isset($_SERVER['CONTENT_TYPE']) && $_SERVER['CONTENT_TYPE'] !== '' && !in_array($_SERVER['CONTENT_TYPE'], self::ALLOWED_CONTENT_TYPES)) { + header($_SERVER["SERVER_PROTOCOL"] . " 415 Unsupported Media Type", true, 415); + die('{"error":"You seem to be forming a strange kind of request? Allowed Content Types are ' . implode(' and ', self::ALLOWED_CONTENT_TYPES) . ', but your Content Type was ' . $_SERVER['CONTENT_TYPE'] . '"}'); + } + } + + private function initParameterData() + { + if (isset($_SERVER['CONTENT_TYPE']) && $_SERVER['CONTENT_TYPE'] === 'application/json') { + $json = file_get_contents('php://input'); + $data = json_decode($json, true); + if (null === $json || "" === $json) { + header($_SERVER["SERVER_PROTOCOL"] . " 400 Bad Request", true, 400); + die('{"error":"No JSON data received in the request: <' . $json . '>"'); + } else if (json_last_error() !== JSON_ERROR_NONE) { + header($_SERVER["SERVER_PROTOCOL"] . " 400 Bad Request", true, 400); + die('{"error":"Malformed JSON data received in the request: <' . $json . '>, ' . json_last_error_msg() . '"}'); + } else { + $this->parameterData = $data; + } + } else { + switch (strtoupper($_SERVER["REQUEST_METHOD"])) { + case 'POST': + $this->parameterData = $_POST; + break; + case 'GET': + $this->parameterData = $_GET; + break; + default: + header($_SERVER["SERVER_PROTOCOL"] . " 405 Method Not Allowed", true, 405); + $errorMessage = '{"error":"You seem to be forming a strange kind of request? Allowed Request Methods are '; + $errorMessage .= implode(' and ', self::ALLOWED_REQUEST_METHODS); + $errorMessage .= ', but your Request Method was ' . strtoupper($_SERVER['REQUEST_METHOD']) . '"}'; + die($errorMessage); + } + } + + if (!isset($this->parameterData["YEAR"]) || $this->parameterData["YEAR"] === "") { + $this->parameterData["YEAR"] = (int)date("Y"); + //die( '{"error":"Parametro YEAR non impostato o non valido"}' ); + } + + if (isset($this->parameterData["LOCALE"]) && in_array(strtolower($this->parameterData["LOCALE"]), self::ALLOWED_LOCALES)) { + $this->parameterData["LOCALE"] = strtolower($this->parameterData["LOCALE"]); + } else { + $this->parameterData["LOCALE"] = "en"; + } + + $this->responseContentType = ( isset($this->parameterData["return"]) && in_array(strtolower($this->parameterData["return"]), self::ALLOWED_RETURN_TYPES) ) ? strtolower($this->parameterData["return"]) : ( $this->acceptHeader !== "" ? (string) self::ALLOWED_RETURN_TYPES[array_search($this->requestHeaders["Accept"], self::ALLOWED_ACCEPT_HEADERS)] : (string) self::ALLOWED_RETURN_TYPES[0] ); + $this->RESPONSE->Messages[] = "parameter data initialized"; + } + + private function prepareL10N(): void + { + $localeArray = [ + $this->parameterData["LOCALE"] . '_' . strtoupper($this->parameterData["LOCALE"]) . '.utf8', + $this->parameterData["LOCALE"] . '_' . strtoupper($this->parameterData["LOCALE"]) . '.UTF-8', + $this->parameterData["LOCALE"] . '_' . strtoupper($this->parameterData["LOCALE"]), + $this->parameterData["LOCALE"] + ]; + setlocale(LC_ALL, $localeArray); + bindtextdomain("litcal", "i18n"); + textdomain("litcal"); + } + + private function setReponseContentTypeHeader() + { + switch ($this->responseContentType) { + case "xml": + header('Content-Type: application/xml; charset=utf-8'); + break; + case "json": + header('Content-Type: application/json; charset=utf-8'); + break; + case "html": + header('Content-Type: text/html; charset=utf-8'); + break; + default: + header('Content-Type: application/json; charset=utf-8'); + } + $this->RESPONSE->Messages[] = "Response Content-Type header set"; + } + + private function readData() + { + if (file_exists("./data/LITURGY__anniversari.json")) { + if (file_exists("./data/i18n/{$this->parameterData["LOCALE"]}.json")) { + $lclData = json_decode(file_get_contents("./data/i18n/{$this->parameterData["LOCALE"]}.json")); + $results = json_decode(file_get_contents("./data/LITURGY__anniversari.json")); + $this->RESPONSE->Messages[] = "localized data events loaded: " . count(get_object_vars($lclData)); + $this->RESPONSE->Messages[] = "base data events loaded: " . count($results); + foreach ($lclData as $label => $lclRow) { + list( $TAG, $IDX ) = explode("_", $label); + foreach ($results as $idx => $obj) { + if ($obj->TAG === $TAG && $obj->IDX === intval($IDX)) { + $litEvent = new LitEvent(array_merge((array) $results[$idx], (array) $lclRow), $this->parameterData["LOCALE"]); + if ($litEvent->year !== null && $this->isAnniversary($litEvent)) { + $this->RESPONSE->LitEvents[] = $litEvent; + } + } + } + + $props = [ + "month" => 2, + "day" => 1 + ]; + usort($this->RESPONSE->LitEvents, function ($a, $b) use ($props) { + foreach ($props as $key => $val) { + if ($a->$key == $b->$key) { + continue; + } + return $a->$key > $b->$key ? $val : -($val); + } + return 0; + }); + } + $this->RESPONSE->Messages[] = count($this->RESPONSE->LitEvents) . " data rows calculated"; + } else { + $this->RESPONSE->Messages[] = "missing file ./data/i18n/{$this->parameterData["LOCALE"]}.json"; + } + } else { + $this->RESPONSE->Messages[] = "missing file ./data/LITURGY__anniversari.json"; + } + } + + + private function isAnniversary(LitEvent $litEvent): bool + { + + $yearDiff = $this->parameterData["YEAR"] - $litEvent->anno; + $litEvent->setYearDiff($yearDiff); + + foreach (LitEvent::ANNIVERSARY as $key => $value) { + if (in_array($key, self::RECURRING)) { + if ($key === "CENTENARY") { + if ($yearDiff % LitEvent::ANNIVERSARY["CENTENARY"] === 0) { + $litEvent->setAnniversary(LitEvent::ANNIVERSARY["CENTENARY"]); + return true; + } + } + + $lastTwoDigits = substr((string)$yearDiff, -2); + if ($key === array_search((int)$lastTwoDigits, LitEvent::ANNIVERSARY)) { + $litEvent->setAnniversary(LitEvent::ANNIVERSARY[$key]); + return true; + } + } else { + $arraySearch = array_search($yearDiff, LitEvent::ANNIVERSARY); + if ($arraySearch !== false) { + $litEvent->setAnniversary($yearDiff); + return true; + } + } + } + + return false; + } + + private function outputResults() + { + + echo json_encode($this->RESPONSE, JSON_UNESCAPED_UNICODE); + exit(0); + } +} diff --git a/src/AnniversaryCalculator/Enums/AnnivType.php b/src/AnniversaryCalculator/Enums/AnnivType.php new file mode 100644 index 00000000..d98b22a2 --- /dev/null +++ b/src/AnniversaryCalculator/Enums/AnnivType.php @@ -0,0 +1,49 @@ +locale = strtoupper( $locale ); + $this->GTXT = [ + self::BIRTH => strtoupper(_("birth")), + self::DEATH => strtoupper(_("death")), + self::CANONIZATION => strtoupper(_("canonization")), + self::DOCTOR => strtoupper(_("doctor")), + self::DEDICATION => strtoupper(_("dedication")), + /**translators: term "translation" refers to the transferral of the relics of a saint */ + self::TRANSLATION => strtoupper(_("translation")), + self::OTHER => strtoupper(_("other")) + ]; + } + + public static function isValid(string $value) + { + return in_array($value, self::$values); + } + + public function i18n(string $value): string + { + if (self::isValid($value)) { + return $this->GTXT[ $value ]; + } + return $value; + } +} diff --git a/src/AnniversaryCalculator/Enums/AreaInterest.php b/src/AnniversaryCalculator/Enums/AreaInterest.php new file mode 100644 index 00000000..1e918b37 --- /dev/null +++ b/src/AnniversaryCalculator/Enums/AreaInterest.php @@ -0,0 +1,52 @@ +locale = strtoupper( $locale ); + $this->GTXT = [ + self::ROME => strtoupper(_("rome")), + self::ITALY => strtoupper(_("italy")), + self::WORLD => strtoupper(_("world")), + self::BIBLICAL => strtoupper(_("biblical")), + self::MARIAN => strtoupper(_("marian")) + ]; + } + + public static function isValid(string $value) + { + return in_array($value, self::$values); + } + + public static function areValid(array $values) + { + return empty(array_diff($values, self::$values)); + } + + public function i18n(string|array $value): string|array + { + if (is_array($value) && self::areValid($value)) { + return array_map(array( $this, 'i18n' ), $value); + } else { + if (self::isValid($value)) { + return $this->GTXT[ $value ]; + } + } + return $value; + } +} diff --git a/src/AnniversaryCalculator/Enums/LitCalendar.php b/src/AnniversaryCalculator/Enums/LitCalendar.php new file mode 100644 index 00000000..9ad2c7df --- /dev/null +++ b/src/AnniversaryCalculator/Enums/LitCalendar.php @@ -0,0 +1,41 @@ +locale = strtoupper( $locale ); + $this->GTXT = [ + self::UNIVERSAL => strtoupper(_("universal")), + self::NATIONAL => strtoupper(_("national")), + self::DIOCESAN => strtoupper(_("diocesan")), + self::WIDE_AREA => strtoupper(_("wide_area")) + ]; + } + + public static function isValid(string $value) + { + return in_array($value, self::$values); + } + + public function i18n(string $value): string + { + if (self::isValid($value)) { + return $this->GTXT[ $value ]; + } + return $value; + } +} diff --git a/includes/LitEvent.php b/src/AnniversaryCalculator/LitEvent.php similarity index 77% rename from includes/LitEvent.php rename to src/AnniversaryCalculator/LitEvent.php index 6a26ccb1..af400e32 100644 --- a/includes/LitEvent.php +++ b/src/AnniversaryCalculator/LitEvent.php @@ -1,11 +1,14 @@ 100, "ONICE" => 95, "GRANITO" => 90, @@ -29,7 +32,7 @@ class LitEvent { "CARTA" => 1 ]; - const ANNIVERSARY = [ + public const ANNIVERSARY = [ "CENTENARY" => 100, "ONYX" => 95, "GRANITE" => 90, @@ -91,53 +94,54 @@ class LitEvent { public string|null $anniversary; public string|null $patronage; - function __construct( array $rowData, string $locale ){ - $AnnivType = new AnnivType( $locale ); - $AreaInterest = new AreaInterest( $locale ); - $LitCalendar = new LitCalendar( $locale ); + public function __construct(array $rowData, string $locale) + { + $AnnivType = new AnnivType($locale); + $AreaInterest = new AreaInterest($locale); + $LitCalendar = new LitCalendar($locale); $this->idx = $rowData["IDX"]; $this->tag = $rowData["TAG"]; $this->soggetto = $rowData["SUBJECT"]; - $this->tipoRicorrenza = $AnnivType->i18n( $rowData["ANNIVERSARY"] ); + $this->tipoRicorrenza = $AnnivType->i18n($rowData["ANNIVERSARY"]); $this->anno = $rowData["YEAR"]; $this->mese = $rowData["MONTH"]; $this->giorno = $rowData["DAY"]; - $this->calendario = $LitCalendar->i18n( $rowData["CALENDAR"] ); + $this->calendario = $LitCalendar->i18n($rowData["CALENDAR"]); $this->luogoNascita = $rowData["PLACE_OF_BIRTH"]; $this->luogoMorte = $rowData["PLACE_OF_DEATH"]; $this->luogoSepoltura = $rowData["PLACE_OF_BURIAL"]; $this->santuarioPrincipale = $rowData["MAIN_SHRINE"]; $this->luoghi = $rowData["PLACES"]; - $this->ambitoDiInteresse = $rowData["AREA"] ? $AreaInterest->i18n( explode( ",", $rowData["AREA"] ) ) : []; + $this->ambitoDiInteresse = $rowData["AREA"] ? $AreaInterest->i18n(explode(",", $rowData["AREA"])) : []; $this->note = $rowData["NOTES"]; $this->patronato = $rowData["PATRONAGE"]; $this->subject = $rowData["SUBJECT"]; - $this->anniversaryType = $AnnivType->i18n( $rowData["ANNIVERSARY"] ); + $this->anniversaryType = $AnnivType->i18n($rowData["ANNIVERSARY"]); $this->year = $rowData["YEAR"]; $this->month = $rowData["MONTH"]; $this->day = $rowData["DAY"]; - $this->calendar = $LitCalendar->i18n( $rowData["CALENDAR"] ); + $this->calendar = $LitCalendar->i18n($rowData["CALENDAR"]); $this->placeOfBirth = $rowData["PLACE_OF_BIRTH"]; $this->placeOfDeath = $rowData["PLACE_OF_DEATH"]; $this->placeOfBurial = $rowData["PLACE_OF_BURIAL"]; $this->mainShrine = $rowData["MAIN_SHRINE"]; $this->places = $rowData["PLACES"]; - $this->areaOfInterest = $rowData["AREA"] ? $AreaInterest->i18n( explode( ",", $rowData["AREA"] ) ) : []; + $this->areaOfInterest = $rowData["AREA"] ? $AreaInterest->i18n(explode(",", $rowData["AREA"])) : []; $this->notes = $rowData["NOTES"]; $this->patronage = $rowData["PATRONAGE"]; - } - public function setAnniversary( int $anniv ) { - $this->anniversario = array_search( $anniv, self::ANNIVERSARY ); - $this->anniversary = array_search( $anniv, self::ANNIVERSARY ); + public function setAnniversary(int $anniv) + { + $this->anniversario = array_search($anniv, self::ANNIVERSARY); + $this->anniversary = array_search($anniv, self::ANNIVERSARY); } - public function setYearDiff( int $yearDiff ) { + public function setYearDiff(int $yearDiff) + { $this->yearDiff = $yearDiff; } - } diff --git a/src/pgettext.php b/src/pgettext.php new file mode 100644 index 00000000..82efcb78 --- /dev/null +++ b/src/pgettext.php @@ -0,0 +1,16 @@ +