Tidied up code comments pre-release.

This commit is contained in:
ekes 2019-05-12 15:37:07 +02:00
parent eab5592dd5
commit 65db102f6f
4 changed files with 224 additions and 81 deletions

View file

@ -1,11 +1,35 @@
<?php
/**
* Squat Radar Connector.
*
* Fetch data from Radar API.
*
* @package squat-radar
* @since 2.0.0
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
class Squat_Radar_Connector {
const BASE_URL = 'https://radar.squat.net';
const API_EVENTS = '/api/1.2/search/events.json';
/**
* Retrieve array of events from API based on a query.
*
* @see self::encode_api_query() for $query values.
*
* @param array $query
* Key value pairs for the API query.
*
* @return array
* Array of events.
*
* @throws Squat_Radar_Connector_Exception
* When events are not returned, but a timeout or API error.
*/
function get_events( $query ) {
$url = self::BASE_URL . self::API_EVENTS . '?' . build_query( $query );
@ -21,6 +45,15 @@ class Squat_Radar_Connector {
}
/**
* Turn a Radar frontend Search URL into facets key value and language values.
*
* @param string $url
* The https://radar.squat.net/events filtered URL.
*
* @return array
* [ 'language' => language code, 'facets' => [key => value] ].
*/
function decode_search_url( $url ) {
$matches = [];
$result = [];
@ -36,6 +69,21 @@ class Squat_Radar_Connector {
return $result;
}
/**
* Encode a query key value from facets, fields, language, limit.
*
* @param array $facets
* Optional. Facet key => filter value array.
* @param array $fields
* Optional. Index array of API field names to retrieve.
* @param string $language
* Optional. Language code.
* @param int $limit
* Optional. Maximum number to items to return.
*
* @return array
* Array for use in self::get_events().
*/
function encode_api_query( $facets = [], $fields = [], $language = '', $limit = 10 ) {
$query = [];
@ -57,6 +105,25 @@ class Squat_Radar_Connector {
return $query;
}
/**
* Return events meeting argument criteria. Either from cache, or retrieved from API.
*
* @param array $facets
* Facet name key => filter value.
* @param array $fields
* Optional. Array of key names.
* @param string $language
* Optional. Language code.
* @param int $limit
* Maximum number of items to return.
* @param int $expiration
* Seconds to cache results. 0 never expires.
* @param bool $reset
* Force a cache reset.
*
* @return array
* Array of event arrays, values keyed by field name.
*/
function events( $facets, $fields = [], $language = NULL, $limit = 10, $expiration = 10800, $reset = FALSE ) {
// Fields we often want to get data out of but not necessarily are chosen to be shown.
$fields = array_merge($fields, ['uuid', 'title', 'body:value', 'url', 'event_status']);