Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Adds GovCmsCkanRecord class #73

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/GovCmsCkanDatasetParser.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Handles CKAN Dataset Parsing.
*/

require_once __DIR__ . '/GovCmsCkanRecord.inc';

/**
* Defines the GovCmsCkanDatasetParser class.
*/
Expand Down Expand Up @@ -284,7 +286,7 @@ class GovCmsCkanDatasetParser {
$labels = array();
if (!empty($this->records) && is_array($this->records) && !empty($key)) {
foreach ($this->records as $record) {
if (isset($record->{$key})) {
if ($record->{$key}) {
$labels[$record->{$key}] = $record->{$key};
}
}
Expand Down Expand Up @@ -463,6 +465,7 @@ class GovCmsCkanDatasetParser {
*/
private function parseCallbackNone($records) {
$table = array();
$records = $this->buildRecords($records);

// Table header is simply the keys.
$table['header'] = $this->keys;
Expand All @@ -484,6 +487,7 @@ class GovCmsCkanDatasetParser {
*/
private function parseCallbackKeys($records) {
$table = array();
$records = $this->buildRecords($records);

// We need to add the label field back to the keys for this formatter.
$keys = $this->keys;
Expand Down Expand Up @@ -523,6 +527,7 @@ class GovCmsCkanDatasetParser {
*/
private function parseCallbackValues($records) {
$table = array();
$records = $this->buildRecords($records);

// TODO: Should use an x axis label for this value.
$table['header'] = array($this->createHeaderCell('col'));
Expand Down Expand Up @@ -556,4 +561,20 @@ class GovCmsCkanDatasetParser {
return $table;
}

/**
* Build record objects from a given dataset.
*
* @param array $records
* The resultant dataset.
*
* @return array
* An array of GovCmsCkanRecords.
*/
public function buildRecords(array $records = array()) {
foreach ($records as &$record) {
$record = get_class($record) === 'GovCmsCkanRecord' ? $record : new GovCmsCkanRecord($record);
}
return $records;
}

}
51 changes: 51 additions & 0 deletions src/GovCmsCkanRecord.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* @file
* Standard way for accessing a CKAN record.
*/

/**
* Class GovCmsCkanRecord
*
* @group govcms_ckan
*/
class GovCmsCkanRecord {

/**
* GovCmsCkanRecord constructor.
*
* Builds a CKAN response record from a CKAN request object.
*
* @param array $properties
* A list $key => $value pairs.
*/
public function __construct($properties = []) {
$properties = (array) $properties;
foreach ($properties as $prop => $value) {
$this->$prop = $value;
}
}

/**
* Provide access to properties on the object.
*
* @param string $property
* A property to access.
* @param mixed $default
* A default value to provide if $property is undefined.
*
* @return mixed
* $property or $default.
*/
public function get($property = '', $default = NULL) {
return is_null($this->$property) ? $default : $this->$property;
}

/**
* {@inheritdoc}
*/
public function __get($property) {
return isset($this->$property) ? $this->$property : NULL;
}

}