-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenLibraryBook.php
128 lines (115 loc) · 3.28 KB
/
OpenLibraryBook.php
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* Class to hold book data (optionally pulled from OpenLibrary API)
* File: OpenLibraryBook.php
*
*/
class OpenLibraryBook extends WireData {
const dateFormat = 'Y';
/**
* We keep a copy of the $page that owns this event so that we can follow
* its outputFormatting state and change our output per that state
*
*/
protected $page;
#public $isbn;
#public $title;
#public $author;
#public $callno;
#public $pubdate;
#public $cover;
#public $summary;
#public $rating;
public function __construct() {
#try {
$this->set('isbn', '');
$this->set('title', '');
$this->set('author', '');
$this->set('callno', '');
$this->set('pubdate', '');
$this->set('cover', '');
$this->set('summary', '');
$this->set('rating', 0);
#} catch (WireException $e) {
# throw new WireException('Error in setting the constructor values in '. __FILE__ .' near line ' . __LINE__);
#}
}
/**
* Set a value to the book: isbn, title, author, callno, pubdate, cover, summary, or rating
*
*/
public function set($key, $value) {
if ($key == 'page') {
$this->page = $value;
return $this;
} else if ($key == 'isbn') {
if (!is_numeric($value)) {
// Make the value an empty string if it isn't numeric
$value = '';
}
if (strlen($value) < 13) {
if (strlen($value) < 10) {
// Under 10 characters
$value = str_pad($value, 10, '0', STR_PAD_LEFT);
} else {
// 11 or 12 characters, needs to be 13
$value = str_pad($value, 13, '0', STR_PAD_LEFT);
}
} else if (strlen($value > 13)) {
// Too long, trim to 13 characters
$value = substr($value, 0, 13);
}
} else if ($key == 'pubdate') {
if (!is_numeric($value)) {
// Make the value an empty string if it isn't numeric
$value = '';
}
if ($value && strlen($value) != 4) {
// Make the value an empty string if it isn't correct
$value = '';
}
} else if ($key == 'cover') {
$value = wire('sanitizer')->url($value);
} else if ($key == 'summary') {
$value = wire('sanitizer')->textarea($value);
} else {
$value = wire('sanitizer')->text($value);
}
return parent::set($key, $value);
}
public function get($key) {
$value = parent::get($key);
// If the page's output formatting is on, then we'll return formatted values
if ($this->page && $this->page->of()) {
/*if ($key == 'date') {
// format a unix timestamp to a date string
$value = date(self::dateFormat, $value);
}*/
if (in_array($key, array('summary', 'title', 'author'))) {
// return entity encoded versions of strings
$value = $this->sanitizer->entities($value);
}
}
return $value;
}
/**
* Provide a default rendering for a book
*
*/
public function renderBook() {
// remember page's output formatting state
$of = $this->page->of();
// turn on output formatting for our rendering (if it's not already on)
if(!$of) $this->page->of(true);
$out = "<p><strong>$this->title</strong><br /><em>$this->author</em><br />$this->summary</p>";
if(!$of) $this->page->of(false);
return $out;
}
/**
* Return a string representing this book
*
*/
public function __toString() {
return $this->renderBook();
}
}