Completed
Push — master ( 428775...2db87f )
by Khoa
14:01
created

bbook_book::set_name()   A

Complexity

Conditions 3
Paths 3

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 21
Code Lines 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 21
rs 9.3142
c 1
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
/**
3
* This file is part of the VinaBB.vn package.
4
*
5
* @copyright (c) VinaBB <vinabb.vn>
6
* @license GNU General Public License, version 2 (GPL-2.0)
7
*/
8
9
namespace vinabb\web\entities;
10
11
use vinabb\web\entities\sub\book_desc;
12
use vinabb\web\includes\constants;
13
14
/**
15
* Entity for a single book
16
*/
17
class bbook_book extends book_desc implements bbook_book_interface
18
{
19
	/** @var \phpbb\db\driver\driver_interface $db */
20
	protected $db;
21
22
	/** @var \phpbb\extension\manager $ext_manager */
23
	protected $ext_manager;
24
25
	/** @var \vinabb\web\entities\helper\helper_interface $entity_helper */
26
	protected $entity_helper;
27
28
	/** @var \phpbb\path_helper $path_helper */
29
	protected $path_helper;
30
31
	/** @var string $table_name */
32
	protected $table_name;
33
34
	/** @var string $ext_root_path */
35
	protected $ext_root_path;
36
37
	/** @var string $ext_web_path */
38
	protected $ext_web_path;
39
40
	/** @var array $data */
41
	protected $data;
42
43
	/**
44
	* Constructor
45
	*
46
	* @param \phpbb\db\driver\driver_interface				$db				Database object
47
	* @param \phpbb\extension\manager						$ext_manager	Extension manager
48
	* @param \vinabb\web\entities\helper\helper_interface	$entity_helper	Entity helper
49
	* @param \phpbb\path_helper								$path_helper	Path helper
50
	* @param string											$table_name		Table name
51
	*/
52
	public function __construct(
53
		\phpbb\db\driver\driver_interface $db,
54
		\phpbb\extension\manager $ext_manager,
55
		\vinabb\web\entities\helper\helper_interface $entity_helper,
56
		\phpbb\path_helper $path_helper,
57
		$table_name
58
	)
59
	{
60
		$this->db = $db;
61
		$this->ext_manager = $ext_manager;
62
		$this->entity_helper = $entity_helper;
63
		$this->path_helper = $path_helper;
64
		$this->table_name = $table_name;
65
66
		$this->ext_root_path = $this->ext_manager->get_extension_path('vinabb/web', true);
67
		$this->ext_web_path = $this->path_helper->update_web_root_path($this->ext_root_path);
68
	}
69
70
	/**
71
	* Data for this entity
72
	*
73
	* @return array
74
	*/
75
	protected function prepare_data()
76
	{
77
		return [
78
			'book_id'		=> 'integer',
79
			'book_name'		=> 'string',
80
			'book_name_seo'	=> 'string',
81
			'book_lang'		=> 'string',
82
			'book_img'		=> 'string',
83
84
			// Entity: vinabb\web\entities\sub\book_desc
85
			'book_desc'				=> 'string',
86
			'book_desc_uid'			=> 'string',
87
			'book_desc_bitfield'	=> 'string',
88
			'book_desc_options'		=> 'integer'
89
		];
90
	}
91
92
	/**
93
	* Load the data from the database for an entity
94
	*
95
	* @param int			$id		Book ID
96
	* @return bbook_book	$this	Object for chaining calls: load()->set()->save()
97
	* @throws \vinabb\web\exceptions\out_of_bounds
98
	*/
99
	public function load($id)
100
	{
101
		$sql = 'SELECT *
102
			FROM ' . $this->table_name . '
103
			WHERE book_id = ' . (int) $id;
104
		$result = $this->db->sql_query($sql);
105
		$this->data = $this->db->sql_fetchrow($result);
106
		$this->db->sql_freeresult($result);
107
108
		// The entity does not exist
109
		if ($this->data === false)
110
		{
111
			throw new \vinabb\web\exceptions\out_of_bounds('book_id');
112
		}
113
114
		return $this;
115
	}
116
117
	/**
118
	* Import data for an entity
119
	*
120
	* Used when the data is already loaded externally.
121
	* Any existing data on this entity is over-written.
122
	* All data is validated and an exception is thrown if any data is invalid.
123
	*
124
	* @param array			$data	Data array from the database
125
	* @return bbook_book	$this	Object for chaining calls: load()->set()->save()
126
	* @throws \vinabb\web\exceptions\invalid_argument
127
	*/
128
	public function import($data)
129
	{
130
		// Clear out any saved data
131
		$this->data = [];
132
133
		// Go through the basic fields and set them to our data array
134
		foreach ($this->prepare_data() as $field => $type)
135
		{
136
			// The data wasn't sent to us
137
			if (!isset($data[$field]))
138
			{
139
				throw new \vinabb\web\exceptions\invalid_argument([$field, 'EMPTY']);
140
			}
141
142
			// settype() passes values by reference
143
			$value = $data[$field];
144
145
			// We're using settype() to enforce data types
146
			settype($value, $type);
147
148
			$this->data[$field] = $value;
149
		}
150
151
		return $this;
152
	}
153
154
	/**
155
	* Insert the entity for the first time
156
	*
157
	* Will throw an exception if the entity was already inserted (call save() instead)
158
	*
159
	* @return bbook_book $this Object for chaining calls: load()->set()->save()
160
	* @throws \vinabb\web\exceptions\out_of_bounds
161
	*/
162
	public function insert()
163
	{
164
		// The entity already exists
165
		if (!empty($this->data['book_id']))
166
		{
167
			throw new \vinabb\web\exceptions\out_of_bounds('book_id');
168
		}
169
170
		// Make extra sure there is no ID set
171
		unset($this->data['book_id']);
172
173
		$sql = 'INSERT INTO ' . $this->table_name . ' ' . $this->db->sql_build_array('INSERT', $this->data);
174
		$this->db->sql_query($sql);
175
176
		// Set the ID using the ID created by the SQL INSERT
177
		$this->data['book_id'] = (int) $this->db->sql_nextid();
178
179
		return $this;
180
	}
181
182
	/**
183
	* Save the current settings to the database
184
	*
185
	* This must be called before closing or any changes will not be saved!
186
	* If adding an entity (saving for the first time), you must call insert() or an exception will be thrown
187
	*
188
	* @return bbook_book $this Object for chaining calls: load()->set()->save()
189
	* @throws \vinabb\web\exceptions\out_of_bounds
190
	*/
191
	public function save()
192
	{
193
		// The entity does not exist
194
		if (empty($this->data['book_id']))
195
		{
196
			throw new \vinabb\web\exceptions\out_of_bounds('book_id');
197
		}
198
199
		// Copy the data array, filtering out the ID
200
		// so we do not attempt to update the row's identity column.
201
		$sql_array = array_diff_key($this->data, ['book_id' => null]);
202
203
		$sql = 'UPDATE ' . $this->table_name . '
204
			SET ' . $this->db->sql_build_array('UPDATE', $sql_array) . '
205
			WHERE book_id = ' . $this->get_id();
206
		$this->db->sql_query($sql);
207
208
		return $this;
209
	}
210
211
	/**
212
	* Get the book ID
213
	*
214
	* @return int
215
	*/
216
	public function get_id()
217
	{
218
		return isset($this->data['book_id']) ? (int) $this->data['book_id'] : 0;
219
	}
220
221
	/**
222
	* Get the book title
223
	*
224
	* @return string
225
	*/
226
	public function get_name()
227
	{
228
		return isset($this->data['book_name']) ? (string) $this->data['book_name'] : '';
229
	}
230
231
	/**
232
	* Set the book title
233
	*
234
	* @param string			$text	Book title
235
	* @return bbook_book	$this	Object for chaining calls: load()->set()->save()
236
	* @throws \vinabb\web\exceptions\unexpected_value
237
	*/
238
	public function set_name($text)
239
	{
240
		$text = (string) $text;
241
242
		// This is a required field
243
		if ($text == '')
244
		{
245
			throw new \vinabb\web\exceptions\unexpected_value(['book_name', 'EMPTY']);
246
		}
247
248
		// Check the max length
249
		if (utf8_strlen($text) > constants::MAX_CONFIG_NAME)
250
		{
251
			throw new \vinabb\web\exceptions\unexpected_value(['book_name', 'TOO_LONG']);
252
		}
253
254
		// Set the value on our data array
255
		$this->data['book_name'] = $text;
256
257
		return $this;
258
	}
259
260
	/**
261
	* Get the book SEO title
262
	*
263
	* @return string
264
	*/
265
	public function get_name_seo()
266
	{
267
		return isset($this->data['book_name_seo']) ? (string) $this->data['book_name_seo'] : '';
268
	}
269
270
	/**
271
	* Set the book SEO title
272
	*
273
	* @param string			$text	Book SEO title
274
	* @return bbook_book	$this	Object for chaining calls: load()->set()->save()
275
	* @throws \vinabb\web\exceptions\unexpected_value
276
	*/
277
	public function set_name_seo($text)
278
	{
279
		$text = (string) $text;
280
281
		// Check invalid characters
282
		if (!preg_match(constants::REGEX_SEO, $text))
283
		{
284
			throw new \vinabb\web\exceptions\unexpected_value(['book_name_seo', 'INVALID']);
285
		}
286
287
		// Set the value on our data array
288
		$this->data['book_name_seo'] = $text;
289
290
		return $this;
291
	}
292
293
	/**
294
	* Get the book language
295
	*
296
	* @return string
297
	*/
298
	public function get_lang()
299
	{
300
		return isset($this->data['book_lang']) ? (string) $this->data['book_lang'] : '';
301
	}
302
303
	/**
304
	* Set the book language
305
	*
306
	* @param string			$text	2-letter language ISO code
307
	* @return bbook_book	$this	Object for chaining calls: load()->set()->save()
308
	* @throws \vinabb\web\exceptions\unexpected_value
309
	*/
310
	public function set_lang($text)
311
	{
312
		$text = (string) $text;
313
314
		// This is a required field
315
		if ($text == '')
316
		{
317
			throw new \vinabb\web\exceptions\unexpected_value(['lang', 'EMPTY']);
318
		}
319
		else if (!$this->entity_helper->check_lang_iso($text))
320
		{
321
			throw new \vinabb\web\exceptions\unexpected_value(['lang', 'NOT_EXISTS']);
322
		}
323
324
		// Set the value on our data array
325
		$this->data['book_lang'] = $text;
326
327
		return $this;
328
	}
329
330
	/**
331
	* Get the book cover
332
	*
333
	* @param bool	$real_path	True to return the path on filesystem, false to return the web access path
334
	* @param bool	$full_path	True to return the path + filename, false to return only filename
335
	* @return string
336
	*/
337
	public function get_img($real_path = false, $full_path = true)
338
	{
339
		$path = $full_path ? ($real_path ? $this->ext_root_path : $this->ext_web_path) . constants::DIR_BBOOK_FILES . 'cover_images/' : '';
340
341
		return !empty($this->data['book_img']) ? (string) $path . $this->data['book_img'] : '';
342
	}
343
344
	/**
345
	* Set the book cover
346
	*
347
	* @param string			$text	Book cover image
348
	* @return bbook_book	$this	Object for chaining calls: load()->set()->save()
349
	* @throws \vinabb\web\exceptions\unexpected_value
350
	*/
351
	public function set_img($text)
352
	{
353
		$text = (string) $text;
354
355
		// Check the max length
356
		if (utf8_strlen($text) > constants::MAX_CONFIG_NAME)
357
		{
358
			throw new \vinabb\web\exceptions\unexpected_value(['book_img', 'TOO_LONG']);
359
		}
360
361
		// Set the value on our data array
362
		$this->data['book_img'] = $text;
363
364
		return $this;
365
	}
366
}
367