-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDabbler.php
More file actions
81 lines (72 loc) · 1.86 KB
/
Dabbler.php
File metadata and controls
81 lines (72 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
class Dabbler
{
/**
* @var string (the URL to the dabble json data)
*/
private $_dataUrl;
/**
* @var stdClass (the decoded JSON tree)
*/
private $_data;
/**
* @var array (the parsed out data)
*/
private $_entries;
/**
* @param string $dataUrl the URL to the dabbel json data
*/
public function __construct($dataUrl)
{
if (!isset($dataUrl)) {
throw new Exception('Must have a schema in order to do anything useful');
} else {
$this->_dataUrl = $dataUrl;
}
if (!$this->_loadData()) {
throw new Exception('Unable to load JSON data from Dabble');
}
}
/**
* @return array
*/
public function getEntries()
{
return $this->_entries;
}
/**
* @param string $value the value to filter on
* @param string $key the column to filter on
*/
public function getEntry($value, $key)
{
foreach ($this->_entries as $entry) {
if ($entry[$key] == $value) {
return $entry;
}
}
return null;
}
/**
* @return bool
*/
private function _loadData()
{
$jsContent = file_get_contents($this->_dataUrl);
$objects = json_decode($jsContent);
if ($objects) {
$this->_data = $objects;
foreach ($objects->entries as $entry) {
$row = array();
foreach ($entry->fields as $field) {
$fieldId = $field->field;
$key = $objects->fields->$fieldId->name;
$row[$key] = $field->value;
}
$this->_entries[] = $row;
}
return true;
}
return false;
}
}