Skip to content

Commit

Permalink
fix areValid method for LitCommon
Browse files Browse the repository at this point in the history
take into account that commons will often contain a colon!
  • Loading branch information
JohnRDOrazio committed May 19, 2022
1 parent 5450010 commit e2c1f6c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
36 changes: 24 additions & 12 deletions includes/Festivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class Festivity implements JsonSerializable
public int $idx;
public string $name;
public DateTime $date;
public string|array $color;
public array $color;
public string $type;
public int $grade;
public string $displayGrade;
public string|array $common; //"Proper" or specified common(s) of saints...
public array $common; //"Proper" or specified common(s) of saints...

/** The following properties are not used in construction, they are only set externally */
public ?string $liturgicalYear = null;
Expand All @@ -27,14 +27,17 @@ 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, DateTime $date, string|array $color = [ '???' ], string $type = '???', int $grade = LitGrade::WEEKDAY, string|array $common = [ '???' ], string $displayGrade='')
{
$this->idx = self::$eventIdx++;
$this->name = $name;
$this->date = $date; //DateTime object
if( is_array( $color ) && LitColor::areValid( $color ) ) {
$this->color = $color;
} else {
if( is_array( $color ) ) {
if( LitColor::areValid( $color ) ) {
$this->color = $color;
}
}
else if ( is_string( $color ) ) {
$_color = strtolower( $color );
//the color string can contain multiple colors separated by a comma, when there are multiple commons to choose from for that festivity
$this->color = strpos( $_color, "," ) && LitColor::areValid( explode(",", $_color) ) ? explode(",", $_color) : ( LitColor::isValid( $_color ) ? [ $_color ] : [ '???' ] );
Expand All @@ -43,17 +46,26 @@ function __construct(string $name, DateTime $date, string|array $color = '???',
$this->type = LitFeastType::isValid( $_type ) ? $_type : '???';
$this->grade = $grade >= LitGrade::WEEKDAY && $grade <= LitGrade::HIGHER_SOLEMNITY ? $grade : -1;
$this->displayGrade = $displayGrade;
//Festivity::debugWrite( "*** Festivity.php *** common vartype = " . gettype( $common ) );
if( is_string( $common ) ) {
$this->common = LitCommon::areValid( explode(",", $common) ) ? explode(",", $common) : [ '???' ];
}
else if( is_array( $common ) && LitCommon::areValid( $common ) ) {
$this->common = $common;
//Festivity::debugWrite( "*** Festivity.php *** common vartype is string, value = $common" );
$this->common = LitCommon::areValid( explode(",", $common) ) ? explode(",", $common) : [];
}
else {
$this->common = [];
else if( is_array( $common ) ) {
//Festivity::debugWrite( "*** Festivity.php *** common vartype is array, value = " . implode( ', ', $common ) );
if( LitCommon::areValid( $common ) ) {
$this->common = $common;
} else {
//Festivity::debugWrite( "*** Festivity.php *** common values have not passed the validity test!" );
$this->common = [];
}
}
}

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

/* * * * * * * * * * * * * * * * * * * * * * * * *
* Funzione statica di comparazione
* in vista dell'ordinamento di un array di oggetti Festivity
Expand Down
9 changes: 8 additions & 1 deletion includes/LitCalAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public function __construct(){
$this->CacheDuration = "_" . CacheDuration::MONTH . date( "m" );
}

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

private function initParameterData() {
if ( $this->APICore->getRequestContentType() === RequestContentType::JSON ) {
$json = file_get_contents( 'php://input' );
Expand Down Expand Up @@ -509,7 +513,7 @@ private function calculateFixedSolemnities() : void {
foreach( $tempCalSolemnities as $row ) {
$currentFeastDate = DateTime::createFromFormat( '!j-n-Y', $row->DAY . '-' . $row->MONTH . '-' . $this->LitSettings->Year, new DateTimeZone( 'UTC' ) );
$tempFestivity = new Festivity( $row->NAME, $currentFeastDate, $row->COLOR, LitFeastType::FIXED, $row->GRADE, $row->COMMON );

//LitCalAPI::debugWrite( "adding new fixed solemnity '$row->NAME', common vartype = " . gettype( $row->COMMON ) . ", common = " . implode(', ', $row->COMMON) );
//A Solemnity impeded in any given year is transferred to the nearest day following designated in nn. 1-8 of the Tables given above ( LY 60 )
//However if a solemnity is impeded by a Sunday of Advent, Lent or Easter Time, the solemnity is transferred to the Monday following,
//or to the nearest free day, as laid down by the General Norms.
Expand Down Expand Up @@ -887,6 +891,9 @@ private function calculateMemorials( int $grade = LitGrade::MEMORIAL, string $mi
$row->DATE = DateTime::createFromFormat( '!j-n-Y', $row->DAY . '-' . $row->MONTH . '-' . $this->LitSettings->Year, new DateTimeZone( 'UTC' ) );
if ( self::DateIsNotSunday( $row->DATE ) && $this->Cal->notInSolemnitiesFeastsOrMemorials( $row->DATE ) ) {
$newFestivity = new Festivity( $row->NAME, $row->DATE, $row->COLOR, LitFeastType::FIXED, $row->GRADE, $row->COMMON );
//LitCalAPI::debugWrite( "adding new memorial '$row->NAME', common vartype = " . gettype( $row->COMMON ) . ", common = " . implode(', ', $row->COMMON) );
//LitCalAPI::debugWrite( ">>> added new memorial '$newFestivity->name', common vartype = " . gettype( $newFestivity->common ) . ", common = " . implode(', ', $newFestivity->common) );

$this->Cal->addFestivity( $row->TAG, $newFestivity );

$this->reduceMemorialsInAdventLentToCommemoration( $row->DATE, $row );
Expand Down
9 changes: 6 additions & 3 deletions includes/enums/LitCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function __construct( string $locale ) {
self::PRO_SANCTIS_MULIERIBUS => "Pro sanctis mulieribus"
];

public static function POSSESSIVE( string $value ) : string {
private static function POSSESSIVE( string $value ) : string {
switch( $value ) {
case "Blessed Virgin Mary":
/**translators: (singular feminine) glue between "From the Common" and the actual common. Latin: leave empty! */
Expand Down Expand Up @@ -269,6 +269,9 @@ public static function isValid( string $value ) {
}

public static function areValid( array $values ) {
$values = array_reduce($values, function( $carry, $key ){
return strpos($key, ':') ? array_merge( explode(':', $key), $carry ) : array_merge( [ $key ], $carry );
}, [] );
return empty( array_diff( $values, self::$values ) );
}

Expand All @@ -294,7 +297,7 @@ public static function AB( string|array $value ) : string {
}
*/

public function i18n( string|array $value ) : string|array {
private function i18n( string|array $value ) : string|array {
if( is_array( $value ) && self::areValid( $value ) ) {
return array_map( [$this, 'i18n'], $value );
}
Expand All @@ -308,7 +311,7 @@ public function i18n( string|array $value ) : string|array {
return $value;
}

public function getPossessive( string|array $value ) : string|array {
private function getPossessive( string|array $value ) : string|array {
if( is_array( $value ) ) {
return array_map( [$this, 'getPossessive'], $value );
}
Expand Down

0 comments on commit e2c1f6c

Please sign in to comment.