1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Catalog parser class for parsing catalog JSON |
7
|
|
|
* |
8
|
|
|
* @link https://tech.lds.org/wiki/Gospel_Library_Catalog_Web_Service |
9
|
|
|
* |
10
|
|
|
* @copyright Copyright (c) 2018 Jared Howland |
11
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
12
|
|
|
* @author Jared Howland <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Gospel\Parser; |
16
|
|
|
|
17
|
|
|
use Gospel\GospelException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Catalog parser class for parsing catalog JSON |
21
|
|
|
*/ |
22
|
|
|
class Catalog |
23
|
|
|
{ |
24
|
|
|
/** @var \stdClass Object containing JSON data from `catalog.query` API call */ |
25
|
|
|
private $object; |
26
|
|
|
|
27
|
|
|
/** @var \stdClass Catalog data */ |
28
|
|
|
private $catalogData; |
29
|
|
|
|
30
|
|
|
/** @var \stdClass Gospel library folders */ |
31
|
|
|
private $folders; |
32
|
|
|
|
33
|
|
|
/** @var \stdClass Gospel library books */ |
34
|
|
|
private $books; |
35
|
|
|
|
36
|
|
|
/** @var \stdClass Gospel library files */ |
37
|
|
|
private $files; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Construct Client class |
41
|
|
|
* |
42
|
|
|
* @param \stdClass $object Object to parse |
43
|
|
|
*/ |
44
|
|
|
public function __construct(\stdClass $object) |
45
|
|
|
{ |
46
|
|
|
$this->object = $object; |
47
|
|
|
$this->catalogData = $object->results->catalog->folders; |
48
|
|
|
$this->parseData(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Status of API call |
53
|
|
|
* |
54
|
|
|
* @param null |
55
|
|
|
* |
56
|
|
|
* @return bool `true` if successful, `false` otherwise |
57
|
|
|
*/ |
58
|
|
|
public function getSuccessStatus(): bool |
59
|
|
|
{ |
60
|
|
|
return $this->object->results->success; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Date catalog was last modified |
65
|
|
|
* |
66
|
|
|
* @param null |
67
|
|
|
* |
68
|
|
|
* @throws GospelException if date is invalid |
69
|
|
|
* |
70
|
|
|
* @return \DateTime object of the last time the catalog was modified |
71
|
|
|
*/ |
72
|
|
|
public function getModifiedDate(): \DateTime |
73
|
|
|
{ |
74
|
|
|
$date = $this->object->results->catalog->date_changed; |
75
|
|
|
$dateObject = \DateTime::createFromFormat('Y-m-d H:i:s', $date); |
76
|
|
|
$errors = \DateTime::getLastErrors(); |
77
|
|
|
if ($dateObject !== false && empty($errors['warning_count'])) { |
78
|
|
|
return $dateObject; |
79
|
|
|
} else { |
80
|
|
|
throw new GospelException('Invalid date used for `getModifiedDate` in class `' . __CLASS__ . '``: ' . $date); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Catalog name |
86
|
|
|
* |
87
|
|
|
* @param null |
88
|
|
|
* |
89
|
|
|
* @return string Name of the catalog |
90
|
|
|
*/ |
91
|
|
|
public function getCatalogName(): string |
92
|
|
|
{ |
93
|
|
|
return $this->object->results->catalog->name; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get all folders in an adjacency list model for hierarchical data |
98
|
|
|
* |
99
|
|
|
* @link http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ |
100
|
|
|
* |
101
|
|
|
* @param null |
102
|
|
|
* |
103
|
|
|
* @return \stdClass Object containing folder data in an adjacency list model for hierarchical data |
104
|
|
|
*/ |
105
|
|
|
public function getFolders(): \stdClass |
106
|
|
|
{ |
107
|
|
|
return (object)$this->folders; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get all books in an adjacency list model for hierarchical data |
112
|
|
|
* |
113
|
|
|
* @link http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ |
114
|
|
|
* |
115
|
|
|
* @param null |
116
|
|
|
* |
117
|
|
|
* @return \stdClass Object containing books data in an adjacency list model for hierarchical data |
118
|
|
|
*/ |
119
|
|
|
public function getBooks(): \stdClass |
120
|
|
|
{ |
121
|
|
|
return (object)$this->books; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get all files in an adjacency list model for hierarchical data |
126
|
|
|
* |
127
|
|
|
* @link http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ |
128
|
|
|
* |
129
|
|
|
* @param null |
130
|
|
|
* |
131
|
|
|
* @return \stdClass Object containing files data in an adjacency list model for hierarchical data |
132
|
|
|
*/ |
133
|
|
|
public function getFiles(): \stdClass |
134
|
|
|
{ |
135
|
|
|
return (object)$this->files; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Loop through the JSON object and parse the data |
140
|
|
|
* |
141
|
|
|
* @param \stdClass $object Object containing data from the `catalogQuery` API call. Default: `null` |
142
|
|
|
* @param int $parentFolderId ID for the parent folder. Default: `null` |
143
|
|
|
*/ |
144
|
|
|
private function parseData(\stdClass $object = null, int $parentFolderId = null): void |
145
|
|
|
{ |
146
|
|
|
$object = empty($object) ? $this->catalogData : $object; |
147
|
|
|
foreach ($object as $folder) { |
148
|
|
|
$this->folders[$folder->id] = (object)['parentFolderId' => $parentFolderId, 'languageId' => $this->object->args['languageid'], 'platformId' => $this->object->args['platformid'], 'name' => $folder->name, 'displayOrder' => $folder->display_order, 'englishName' => $folder->eng_name]; |
149
|
|
|
$this->parseBooks((object)$folder->books, $folder->id); |
150
|
|
|
if (!empty($folder->folders)) { |
151
|
|
|
$this->parseData((object)$folder->folders, $folder->id); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Loop through the array and parse the books data |
158
|
|
|
* |
159
|
|
|
* @param \stdClass $books Object containing books data. Default: `null` |
160
|
|
|
* @param int $folderId ID for the folder that contains the book. Default: `null` |
161
|
|
|
*/ |
162
|
|
|
private function parseBooks(\stdClass $books = null, int $folderId): void |
163
|
|
|
{ |
164
|
|
|
if (!empty($books)) { |
165
|
|
|
foreach ($books as $book) { |
166
|
|
|
$this->books[$book->id] = (object)['folderId' => $folderId, 'name' => $book->name, 'fullName' => $book->full_name, 'description' => $book->description, 'gospelLibraryUri' => $book->gl_uri, 'url' => $book->url, 'displayOrder' => $book->display_order, 'version' => $book->version, 'fileVersion' => $book->file_version, 'file' => $book->file, 'dateAdded' => $book->dateadded, 'dateModified' => $book->datemodified, 'cbId' => $book->cb_id, 'mediaAvailable' => $book->media_available, 'obsolete' => $book->obsolete, 'size' => $book->size, 'sizeIndex' => $book->size_index]; |
167
|
|
|
$this->parseFiles(@$book->files, $book->id); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Loop through the array and parse the files data |
174
|
|
|
* |
175
|
|
|
* @param \stdClass $files Object containing files data. Default: `null` |
176
|
|
|
* @param int $bookId ID for the book associated with the file. Default: `null` |
177
|
|
|
*/ |
178
|
|
|
private function parseFiles(\stdClass $files = null, int $bookId): void |
179
|
|
|
{ |
180
|
|
|
if (!empty($files)) { |
181
|
|
|
// Currently, Gospel Library only includes PDF files |
182
|
|
|
foreach ($files->PDF as $file) { |
183
|
|
|
$this->files[$file->id] = (object)['bookId' => $bookId, 'order' => $file->order, 'dateAdded' => $file->dateadded, 'dateModified' => $file->datemodified, 'version' => $file->version, 'name' => $file->name, 'title' => $file->title, 'url' => $file->url, 'size' => $file->size]; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|