PHP code example of liturgical-calendar / components
1. Go to this page and download the library: Download liturgical-calendar/components library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
liturgical-calendar / components example snippets
LiturgicalCalendar\Components\ApiClient;
use LiturgicalCalendar\Components\WebCalendar;
// 1. Initialize ApiClient once at application bootstrap
$apiClient = ApiClient::getInstance([
'apiUrl' => 'https://litcal.johnromanodorazio.com/api/dev'
]);
// 2. Fetch calendar data using the factory method
$calendarData = $apiClient->calendar()
->nation('IT')
->year(2024)
->locale('it')
->get();
// 3. Display the calendar
$webCalendar = new WebCalendar($calendarData);
echo $webCalendar->buildTable();
LiturgicalCalendar\Components\ApiClient;
use LiturgicalCalendar\Components\CalendarSelect;
use LiturgicalCalendar\Components\ApiOptions;
// Initialize ApiClient
$apiClient = ApiClient::getInstance([
'apiUrl' => 'https://litcal.johnromanodorazio.com/api/dev'
]);
// Create calendar dropdown (uses ApiClient configuration automatically)
$calendarSelect = new CalendarSelect();
echo $calendarSelect->getSelect();
// Create API parameter form inputs
$apiOptions = new ApiOptions();
echo $apiOptions->getForm();
LiturgicalCalendar\Components\ApiClient;
use LiturgicalCalendar\Components\Http\HttpClientFactory;
use LiturgicalCalendar\Components\Cache\ArrayCache;
// 1. Setup cache (optional but recommended)
$cache = new ArrayCache();
// 2. Create production-ready HTTP client with caching, retry, and circuit breaker
$httpClient = HttpClientFactory::createProductionClient(
cache: $cache,
cacheTtl: 3600 * 24, // Cache for 24 hours
maxRetries: 3, // Retry up to 3 times
failureThreshold: 5 // Circuit breaker threshold
);
// 3. Initialize ApiClient with production client
$apiClient = ApiClient::getInstance([
'apiUrl' => 'https://litcal.johnromanodorazio.com/api/dev',
'httpClient' => $httpClient
]);
// 4. Fetch calendar data with automatic caching and retry
$calendar = $apiClient->calendar()
->nation('US')
->year(2024)
->locale('en')
->get();
// 5. Access metadata via ApiClient
$metadata = $apiClient->metadata()->getMetadata();
use LiturgicalCalendar\Components\CalendarSelect;
use LiturgicalCalendar\Components\Metadata\MetadataProvider;
use LiturgicalCalendar\Components\Http\HttpClientFactory;
use LiturgicalCalendar\Components\Cache\ArrayCache;
// Create a production-ready HTTP client with all features
$cache = new ArrayCache();
$httpClient = HttpClientFactory::createProductionClient(
cache: $cache,
cacheTtl: 3600 * 24, // Cache for 24 hours
maxRetries: 3, // Retry up to 3 times
failureThreshold: 5 // Circuit breaker threshold
);
// Initialize MetadataProvider with the already-decorated production client
// Note: Don't pass cache/logger again - they're already in the production client
MetadataProvider::getInstance(
apiUrl: 'https://litcal.johnromanodorazio.com/api/dev',
httpClient: $httpClient
);
// Create components - they automatically use the configured MetadataProvider
$calendar = new CalendarSelect();
use LiturgicalCalendar\Components\Metadata\MetadataProvider;
use LiturgicalCalendar\Components\CalendarSelect;
// 1. Initialize MetadataProvider ONCE at application bootstrap
MetadataProvider::getInstance(
apiUrl: 'https://litcal.johnromanodorazio.com/api/dev',
httpClient: $httpClient,
cache: $cache,
logger: $logger,
cacheTtl: 86400 // 24 hours
);
// 2. Create components - they automatically use the configured singleton
$calendarSelect = new CalendarSelect();
// 3. Use static validation methods
$isValid = MetadataProvider::isValidDioceseForNation('boston_us', 'US');
// Check if diocese belongs to nation
$isValid = MetadataProvider::isValidDioceseForNation('boston_us', 'US');
// Also available via CalendarSelect
$isValid = CalendarSelect::isValidDioceseForNation('boston_us', 'US');
// Get configured API URL
$apiUrl = MetadataProvider::getApiUrl();
// Get metadata endpoint URL (API URL + /calendars)
$metadataUrl = MetadataProvider::getMetadataUrl();
// Check if metadata is cached
$isCached = MetadataProvider::isCached();
use LiturgicalCalendar\Components\Http\HttpClientFactory;
use LiturgicalCalendar\Components\Metadata\MetadataProvider;
use LiturgicalCalendar\Components\CalendarSelect;
use LiturgicalCalendar\Components\Cache\ArrayCache;
// Create production-ready HTTP client (already ger again - they're already in the production client
MetadataProvider::getInstance(
apiUrl: 'https://litcal.johnromanodorazio.com/api/dev',
httpClient: $httpClient
);
// All components use this configuration automatically
$calendar = new CalendarSelect();
iturgicalCalendar\Components\CalendarSelect;
$options = [
'locale' => 'it', // set the locale to Italian
'class' => 'form-select',
'id' => 'calendarSelect',
'label' => true,
'labelText' => _("Select a calendar")
];
$CalendarSelect = new CalendarSelect($options);
echo $CalendarSelect->getSelect();
include_once 'vendor/autoload.php';
use LiturgicalCalendar\Components\CalendarSelect;
use LiturgicalCalendar\Components\CalendarSelect\OptionsType;
$CalendarSelect = new CalendarSelect();
$CalendarSelect->nationFilter('NL')
->setOptions(OptionsType::DIOCESES_FOR_NATION)
->locale('it')
->class('form-select')
->id('diocesan_calendar')
->name('diocesan_calendar')
->label(true)
->labelText('diocese')
->data(['
iturgicalCalendar\Components\ApiOptions;
$apiOptions = new ApiOptions();
echo $apiOptions->getForm();
iturgicalCalendar\Components\ApiOptions;
use LiturgicalCalendar\Components\ApiOptions\PathType;
$apiOptions = new ApiOptions();
echo $apiOptions->getForm(PathType::BASE_PATH);
echo '<br>';
echo $apiOptions->getForm(PathType::ALL_PATHS);
iturgicalCalendar\Components\ApiOptions;
use LiturgicalCalendar\Components\ApiOptions\PathType;
$options = [
'locale' => 'it-IT'
];
$apiOptions = new ApiOptions($options);
echo $apiOptions->getForm(PathType::BASE_PATH);
echo '<br>';
echo $apiOptions->getForm(PathType::ALL_PATHS);
iturgicalCalendar\Components\ApiOptions;
$options = [
'locale' => 'it-IT',
'wrapper' => 'div', //we can set a string representing the 'as' html element
'formLabel' => 'h5' //we can set a string representing the 'as' html element
];
$apiOptions = new ApiOptions($options);
$apiOptions->wrapper->class('calendarOptions')->id('calendarOptions');
$apiOptions->formLabel->text('Liturgical Calendar API Request Options');
echo $apiOptions->getForm();
iturgicalCalendar\Components\ApiOptions;
use LiturgicalCalendar\Components\ApiOptions\Input;
$options = [
"locale" => "it-IT",
"wrapper" => true, //we can simply set a boolean, then set the 'as' html element afterwards by using the ->as() method
"formLabel" => true //we can simply set a boolean, then set the 'as' html element afterwards by using the ->as() method
];
$apiOptions = new ApiOptions($options);
$apiOptions->wrapper->as('div')->class('calendarOptions')->id('calendarOptions');
$apiOptions->formLabel->as('h5')->text('Liturgical Calendar API Request Options');
Input::setGlobalWrapper('div');
Input::setGlobalWrapperClass('form-group');
Input::setGlobalInputClass('form-select');
echo $apiOptions->getForm();
// set up our cURL request to the calendar endpoint...
$response = curl_exec($ch);
curl_close($ch);
if ($response) {
$LiturgicalCalendar = json_decode($response);
if (JSON_ERROR_NONE === json_last_error()) {
$apiOptions->localeInput->selectedValue($LiturgicalCalendar->settings->locale);
}
}
$selectedDiocese = (isset($_POST['diocesan_calendar']) && !empty($_POST['diocesan_calendar']))
? htmlspecialchars($_POST['diocesan_calendar'], ENT_QUOTES, 'UTF-8')
: false;
$selectedNation = (isset($_POST['national_calendar']) && !empty($_POST['national_calendar']))
? htmlspecialchars($_POST['national_calendar'], ENT_QUOTES, 'UTF-8')
: false;
if ($selectedDiocese || $selectedNation) {
$apiOptions->epiphanyInput->disabled();
$apiOptions->ascensionInput->disabled();
$apiOptions->corpusChristiInput->disabled();
$apiOptions->eternalHighPriestInput->disabled();
}
// set up our cURL request to the calendar endpoint...
$response = curl_exec($ch);
curl_close($ch);
if ($response) {
$LiturgicalCalendar = json_decode($response);
if (JSON_ERROR_NONE === json_last_error()) {
$apiOptions->epiphanyInput->selectedValue($LiturgicalCalendar->settings->epiphany);
$apiOptions->ascensionInput->selectedValue($LiturgicalCalendar->settings->ascension);
$apiOptions->corpusChristiInput->selectedValue($LiturgicalCalendar->settings->corpus_christi);
$apiOptions->eternalHighPriestInput->selectedValue($LiturgicalCalendar->settings->eternal_high_priest ? 'true' : 'false');
$apiOptions->localeInput->selectedValue($LiturgicalCalendar->settings->locale);
}
}
LiturgicalCalendar\Components\WebCalendar;
// build your request here
// $response = curl_exec($ch);
// Get an object from the response
$LiturgicalCalendar = json_decode($response);
// If we have successfully obtained an object, pass it into the WebCalendar constructor
if (JSON_ERROR_NONE === json_last_error()) {
$webCalendar = new WebCalendar($LiturgicalCalendar);
$table = $webCalendar->buildTable();
echo $table;
} else {
echo '<div class="col-12">JSON error: ' . json_last_error_msg() . '</div>';
}
use LiturgicalCalendar\Components\WebCalendar;
use LiturgicalCalendar\Components\WebCalendar\Grouping;
use LiturgicalCalendar\Components\WebCalendar\ColorAs;
use LiturgicalCalendar\Components\WebCalendar\Column;
use LiturgicalCalendar\Components\WebCalendar\ColumnOrder;
use LiturgicalCalendar\Components\WebCalendar\DateFormat;
use LiturgicalCalendar\Components\WebCalendar\GradeDisplay;
// make your request and get an object from the response...
$webCalendar = new WebCalendar($LiturgicalCalendar);
$webCalendar->id('LitCalTable')
->class('.liturgicalCalendar')
->firstColumnGrouping(Grouping::BY_LITURGICAL_SEASON)
->psalterWeekGrouping()
->removeHeaderRow()
->removeCaption()
->monthHeader()
->seasonColor(ColorAs::CSS_CLASS)
->seasonColorColumns(Column::LITURGICAL_SEASON)
->eventColor(ColorAs::INDICATOR)
->eventColorColumns(Column::EVENT)
->columnOrder(ColumnOrder::GRADE_FIRST)
->dateFormat(DateFormat::DAY_ONLY)
->gradeDisplay(GradeDisplay::ABBREVIATED);
bash
git clone https://github.com/Liturgical-Calendar/liturgy-components-php.git
cd liturgy-components-php
composer install