Completed
Push — master ( 47b0f8...cbbce4 )
by Khoa
09:57
created

bbook_word::prepare_data()   A

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 15
Code Lines 10

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
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\word_desc;
12
use vinabb\web\includes\constants;
13
14
/**
15
* Entity for a single book
16
*/
17
class bbook_word extends word_desc implements bbook_word_interface
18
{
19
	/** @var \phpbb\db\driver\driver_interface $db */
20
	protected $db;
21
22
	/** @var \vinabb\web\entities\helper\helper_interface $entity_helper */
23
	protected $entity_helper;
24
25
	/** @var string $table_name */
26
	protected $table_name;
27
28
	/** @var array $data */
29
	protected $data;
30
31
	/**
32
	* Constructor
33
	*
34
	* @param \phpbb\db\driver\driver_interface				$db				Database object
35
	* @param \vinabb\web\entities\helper\helper_interface	$entity_helper	Entity helper
36
	* @param string											$table_name		Table name
37
	*/
38
	public function __construct(\phpbb\db\driver\driver_interface $db, \vinabb\web\entities\helper\helper_interface $entity_helper, $table_name)
39
	{
40
		$this->db = $db;
41
		$this->entity_helper = $entity_helper;
42
		$this->table_name = $table_name;
43
	}
44
45
	/**
46
	* Data for this entity
47
	*
48
	* @return array
49
	*/
50
	protected function prepare_data()
51
	{
52
		return [
53
			'word_id'	=> 'integer',
54
			'word'		=> 'string',
55
			'word_lang'	=> 'string',
56
			'word_url'	=> 'string',
57
58
			// Entity: vinabb\web\entities\sub\word_desc
59
			'word_desc'				=> 'string',
60
			'word_desc_uid'			=> 'string',
61
			'word_desc_bitfield'	=> 'string',
62
			'word_desc_options'		=> 'integer'
63
		];
64
	}
65
66
	/**
67
	* Load the data from the database for an entity
68
	*
69
	* @param int					$id		Word ID
70
	* @return bbook_word_interface	$this	Object for chaining calls: load()->set()->save()
71
	* @throws \vinabb\web\exceptions\out_of_bounds
72
	*/
73
	public function load($id)
74
	{
75
		$sql = 'SELECT *
76
			FROM ' . $this->table_name . '
77
			WHERE word_id = ' . (int) $id;
78
		$result = $this->db->sql_query($sql);
79
		$this->data = $this->db->sql_fetchrow($result);
80
		$this->db->sql_freeresult($result);
81
82
		// The entity does not exist
83
		if ($this->data === false)
84
		{
85
			throw new \vinabb\web\exceptions\out_of_bounds('word_id');
86
		}
87
88
		return $this;
89
	}
90
91
	/**
92
	* Import data for an entity
93
	*
94
	* Used when the data is already loaded externally.
95
	* Any existing data on this entity is over-written.
96
	* All data is validated and an exception is thrown if any data is invalid.
97
	*
98
	* @param array					$data	Data array from the database
99
	* @return bbook_word_interface	$this	Object for chaining calls: load()->set()->save()
100
	* @throws \vinabb\web\exceptions\invalid_argument
101
	*/
102
	public function import($data)
103
	{
104
		// Clear out any saved data
105
		$this->data = [];
106
107
		// Go through the basic fields and set them to our data array
108
		foreach ($this->prepare_data() as $field => $type)
109
		{
110
			// The data wasn't sent to us
111
			if (!isset($data[$field]))
112
			{
113
				throw new \vinabb\web\exceptions\invalid_argument([$field, 'EMPTY']);
114
			}
115
116
			// settype() passes values by reference
117
			$value = $data[$field];
118
119
			// We're using settype() to enforce data types
120
			settype($value, $type);
121
122
			$this->data[$field] = $value;
123
		}
124
125
		return $this;
126
	}
127
128
	/**
129
	* Insert the entity for the first time
130
	*
131
	* Will throw an exception if the entity was already inserted (call save() instead)
132
	*
133
	* @return bbook_word_interface $this Object for chaining calls: load()->set()->save()
134
	* @throws \vinabb\web\exceptions\out_of_bounds
135
	*/
136
	public function insert()
137
	{
138
		// The entity already exists
139
		if (!empty($this->data['word_id']))
140
		{
141
			throw new \vinabb\web\exceptions\out_of_bounds('word_id');
142
		}
143
144
		// Make extra sure there is no ID set
145
		unset($this->data['word_id']);
146
147
		$sql = 'INSERT INTO ' . $this->table_name . ' ' . $this->db->sql_build_array('INSERT', $this->data);
148
		$this->db->sql_query($sql);
149
150
		// Set the ID using the ID created by the SQL INSERT
151
		$this->data['word_id'] = (int) $this->db->sql_nextid();
152
153
		return $this;
154
	}
155
156
	/**
157
	* Save the current settings to the database
158
	*
159
	* This must be called before closing or any changes will not be saved!
160
	* If adding an entity (saving for the first time), you must call insert() or an exception will be thrown
161
	*
162
	* @return bbook_word_interface $this Object for chaining calls: load()->set()->save()
163
	* @throws \vinabb\web\exceptions\out_of_bounds
164
	*/
165
	public function save()
166
	{
167
		// The entity does not exist
168
		if (empty($this->data['word_id']))
169
		{
170
			throw new \vinabb\web\exceptions\out_of_bounds('word_id');
171
		}
172
173
		// Copy the data array, filtering out the ID
174
		// so we do not attempt to update the row's identity column.
175
		$sql_array = array_diff_key($this->data, ['word_id' => null]);
176
177
		$sql = 'UPDATE ' . $this->table_name . '
178
			SET ' . $this->db->sql_build_array('UPDATE', $sql_array) . '
179
			WHERE word_id = ' . $this->get_id();
180
		$this->db->sql_query($sql);
181
182
		return $this;
183
	}
184
185
	/**
186
	* Get the word ID
187
	*
188
	* @return int
189
	*/
190
	public function get_id()
191
	{
192
		return isset($this->data['word_id']) ? (int) $this->data['word_id'] : 0;
193
	}
194
195
	/**
196
	* Get the word
197
	*
198
	* @return string
199
	*/
200
	public function get_word()
201
	{
202
		return isset($this->data['word']) ? (string) $this->data['word'] : '';
203
	}
204
205
	/**
206
	* Set the word
207
	*
208
	* @param string					$text	Word
209
	* @return bbook_word_interface	$this	Object for chaining calls: load()->set()->save()
210
	* @throws \vinabb\web\exceptions\unexpected_value
211
	*/
212
	public function set_word($text)
213
	{
214
		$text = (string) $text;
215
216
		// This is a required field
217
		if ($text == '')
218
		{
219
			throw new \vinabb\web\exceptions\unexpected_value(['word', 'EMPTY']);
220
		}
221
222
		// Check the max length
223
		if (utf8_strlen($text) > constants::MAX_BBOOK_WORD)
224
		{
225
			throw new \vinabb\web\exceptions\unexpected_value(['word', 'TOO_LONG']);
226
		}
227
228
		// Set the value on our data array
229
		$this->data['word'] = $text;
230
231
		return $this;
232
	}
233
234
	/**
235
	* Get the word language
236
	*
237
	* @return string
238
	*/
239
	public function get_lang()
240
	{
241
		return isset($this->data['word_lang']) ? (string) $this->data['word_lang'] : '';
242
	}
243
244
	/**
245
	* Set the word language
246
	*
247
	* @param string					$text	2-letter language ISO code
248
	* @return bbook_word_interface	$this	Object for chaining calls: load()->set()->save()
249
	* @throws \vinabb\web\exceptions\unexpected_value
250
	*/
251
	public function set_lang($text)
252
	{
253
		$text = (string) $text;
254
255
		// This is a required field
256
		if ($text == '')
257
		{
258
			throw new \vinabb\web\exceptions\unexpected_value(['lang', 'EMPTY']);
259
		}
260
		else if (!$this->entity_helper->check_lang_iso($text))
261
		{
262
			throw new \vinabb\web\exceptions\unexpected_value(['lang', 'NOT_EXISTS']);
263
		}
264
265
		// Set the value on our data array
266
		$this->data['word_lang'] = $text;
267
268
		return $this;
269
	}
270
271
	/**
272
	* Get the word link
273
	*
274
	* @return string
275
	*/
276
	public function get_url()
277
	{
278
		return isset($this->data['word_url']) ? (string) htmlspecialchars_decode($this->data['word_url']) : '';
279
	}
280
281
	/**
282
	* Set the word link
283
	*
284
	* @param string					$text	Word URL
285
	* @return bbook_word_interface	$this	Object for chaining calls: load()->set()->save()
286
	* @throws \vinabb\web\exceptions\unexpected_value
287
	*/
288
	public function set_url($text)
289
	{
290
		$text = strtolower($text);
291
292
		// Checking for valid URL
293
		if ($text != '' && filter_var($text, FILTER_VALIDATE_URL) === false)
294
		{
295
			throw new \vinabb\web\exceptions\unexpected_value(['word_url', 'INVALID_URL']);
296
		}
297
298
		// Set the value on our data array
299
		$this->data['word_url'] = $text;
300
	}
301
}
302