Passed
Push — master ( d26eb3...883d7f )
by Fabio
06:35
created

TDbParameterModule::loadDbParameters()   B

Complexity

Conditions 9
Paths 16

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 20
c 1
b 0
f 0
nc 16
nop 0
dl 0
loc 30
rs 8.0555
1
<?php
2
/**
3
 * TDbParameterModule class
4
 *
5
 * @author Brad Anderson <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 * @package Prado\Util
9
 */
10
11
namespace Prado\Util;
12
13
use Exception;
14
use PDO;
15
use Prado\Data\TDataSourceConfig;
16
use Prado\Data\TDbConnection;
17
use Prado\Exceptions\TConfigurationException;
18
use Prado\Exceptions\TInvalidDataTypeException;
19
use Prado\Exceptions\TInvalidOperationException;
20
use Prado\TModule;
21
use Prado\TPropertyValue;
22
use Prado\Util\Behaviors\TMapLazyLoadBehavior;
23
use Prado\Util\Behaviors\TMapRouteBehavior;
24
25
/**
26
 * TDbParameterModule class
27
 *
28
 * This loads application parameters from a database.  It adds the
29
 * {@link TMapLazyLoadBehavior} to Application Parameters when the
30
 * {@link setAutoLoadField} is set.  The key and name fields, table,
31
 * autoload field, and autoload values (both true and false values)
32
 * are parameterized.  Set them to your application specific values.
33
 *
34
 * The following will load the options from a WordPress Database:
35
 * <code>
36
 *		<module id="dbparams" class="Prado\Util\TDbParameterModule"
37
 * ConnectionID="DB" KeyField="option_name" ValueField="option_value" TableName="wp_options" Serializer="php"
38
 * autoLoadField="autoload" autoLoadValue="'yes'" autoLoadValueFalse="'no'"/>
39
 * </code>
40
 *
41
 * This allows for setting and removal of application parameters
42
 * into and from the database through {@link set} and
43
 * {@link remove}, respectively. Arrays and Objects are
44
 * serialized.  The specific serializer can be chose to be 'php',
45
 * 'json', or provide your own function or callable.  Default to 'php'.
46
 *
47
 * setting {@link setSerializer} to your own function that has the
48
 * following format:
49
 * <code>
50
 *		function mySerializerFunction($data, $encode) {...}
51
 * </code>
52
 * If $encode is true, then encode, otherwise decode, to text.
53
 *
54
 * When {@link getCaptureParameterChanges} is true, the default,
55
 * then this will route any changes to the Application Parameters
56
 * after TPageService::onPreRunPage back to the TDbParameterModule
57
 * and be saved to the database.  This captures any changes when
58
 * done by the page or user.  These changes are restored when
59
 * this module is loaded again.
60
 *
61
 * @author Brad Anderson <[email protected]>
62
 * @package Prado\Util
63
 * @since 4.2.0
64
 */
65
class TDbParameterModule extends TModule
66
{
67
	public const SERIALIZE_PHP = 'php';
68
	
69
	public const SERIALIZE_JSON = 'json';
70
	/**
71
	 * The name of the Application Parameter Lazy Load Behavior
72
	 */
73
	public const APP_PARAMETER_LAZY_BEHAVIOR = 'lazyTDbParameter';
74
	
75
	/**
76
	 * The name of the Application Parameter Lazy Load Behavior
77
	 */
78
	public const APP_PARAMETER_SET_BEHAVIOR = 'setTDbParameter';
79
	
80
	/**
81
	 * @var string the ID of TDataSourceConfig module
82
	 */
83
	private $_connID = '';
84
	
85
	/**
86
	 * @var TDbConnection the DB connection instance
87
	 */
88
	private $_conn;
89
	
90
	/**
91
	 * @var bool whether or not the database parameters have been loaded.
92
	 * when true none of the variables can be changed
93
	 */
94
	private $_initialized = false;
95
	
96
	/**
97
	 * @var string The key field for the parameter from the database
98
	 */
99
	private $_keyField = 'param_key';
100
	
101
	/**
102
	 * @var string The value field for the parameter from the database
103
	 */
104
	private $_valueField = 'param_value';
105
	
106
	/**
107
	 * @var string The table name for the parameters from the database
108
	 */
109
	private $_tableName = 'parameters';
110
	
111
	/**
112
	 * @var string autoload Field. default "", meaning no autoload field
113
	 */
114
	private $_autoLoadField = 'autoload';
115
	
116
	/**
117
	 * @var string autoload True value. default sql "1"
118
	 */
119
	private $_autoLoadValue = '1';
120
	
121
	/**
122
	 * @var string autoload False value. default sql "0"
123
	 */
124
	private $_autoLoadValueFalse = '0';
125
	
126
	/**
127
	 * @var bool whether the parameter DB table should be created automatically
128
	 */
129
	private $_autoCreate = true;
130
	
131
	/**
132
	 * @var callable|string which serialize function to use,
133
	 */
134
	private $_serializer = self::SERIALIZE_PHP;
135
	
136
	/**
137
	 * @var bool automatically capture changes to Parameters after Application Initialize
138
	 */
139
	private $_autoCapture = true;
140
	
141
	/** @var TMapRouteBehavior captures all the changes to the parameters to the db */
142
	private $_setBehavior;
143
144
	
145
	/**
146
	 * Initializes the module by loading parameters.
147
	 * @param mixed $config content enclosed within the module tag
148
	 */
149
	public function init($config)
150
	{
151
		$this->loadDbParameters();
152
		$this->_initialized = true;
153
		
154
		if ($this->_autoLoadField) {
155
			$this->getApplication()->getParameters()->attachBehavior(self::APP_PARAMETER_LAZY_BEHAVIOR, new TMapLazyLoadBehavior([$this, 'getFromBehavior']));
156
		}
157
		if ($this->_autoCapture) {
158
			$this->getApplication()->attachEventHandler('onBeginRequest', [$this, 'attachTPageServiceHandler']);
159
		}
160
		parent::init($config);
161
	}
162
	
163
	/**
164
	 * Loads parameters from the database into the application.
165
	 * @throws TDbException if the Fields and table is not correct
166
	 */
167
	protected function loadDbParameters()
168
	{
169
		$db = $this->getDbConnection();
170
		
171
		$this->ensureTable();
172
		
173
		$where = ($this->_autoLoadField ? " WHERE {$this->_autoLoadField}={$this->_autoLoadValue}" : '');
174
		$cmd = $db->createCommand(
175
			"SELECT {$this->_keyField} as keyField, {$this->_valueField}  as valueField FROM {$this->_tableName}{$where}"
176
		);
177
		$results = $cmd->query();
178
		
179
		$appParameters = $this->getApplication()->getParameters();
180
		$serializer = $this->getSerializer();
181
		foreach ($results->readAll() as $row) {
182
			$value = $row['valueField'];
183
			if ($serializer == self::SERIALIZE_PHP) {
184
				if (($avalue = @unserialize($value)) !== false) {
185
					$value = $avalue;
186
				}
187
			} elseif ($serializer == self::SERIALIZE_JSON) {
188
				if (($avalue = json_decode($value, true)) !== null) {
189
					$value = $avalue;
190
				}
191
			} elseif ($serializer) {
192
				if (($avalue = call_user_func($serializer, $value, false)) !== null) {
193
					$value = $avalue;
194
				}
195
			}
196
			$appParameters[$row['keyField']] = $value;
197
		}
198
	}
199
	
200
	/**
201
	 * TApplication::onBeginRequest Handler that adds {@link attachTPageBehaviors} to
202
	 * TPageService::onPreRunPage. In turn, this attaches {@link attachTPageBehaviors}
203
	 * to TPageService to then adds the page behaviors.
204
	 * @param object $sender the object that raised the event
205
	 * @param mixed $param parameter of the event
206
	 */
207
	public function attachTPageServiceHandler($sender, $param)
0 ignored issues
show
Unused Code introduced by
The parameter $sender is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

207
	public function attachTPageServiceHandler(/** @scrutinizer ignore-unused */ $sender, $param)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $param is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

207
	public function attachTPageServiceHandler($sender, /** @scrutinizer ignore-unused */ $param)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
208
	{
209
		$service = $this->getService();
210
		if ($service->hasEvent('onPreRunPage')) {
0 ignored issues
show
Bug introduced by
The method hasEvent() does not exist on Prado\IService. Since it exists in all sub-types, consider adding an abstract or default implementation to Prado\IService. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

210
		if ($service->/** @scrutinizer ignore-call */ hasEvent('onPreRunPage')) {
Loading history...
211
			$service->attachEventHandler('onPreRunPage', [$this, 'attachParameterStorage'], 0);
0 ignored issues
show
Bug introduced by
The method attachEventHandler() does not exist on Prado\IService. Since it exists in all sub-types, consider adding an abstract or default implementation to Prado\IService. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

211
			$service->/** @scrutinizer ignore-call */ 
212
             attachEventHandler('onPreRunPage', [$this, 'attachParameterStorage'], 0);
Loading history...
212
		}
213
	}
214
	
215
	/**
216
	 * This attaches the TMapRouteBehavior on the Parameters.
217
	 * @param object $sender
218
	 * @param null|mixed $param
219
	 * @throws TDbException if the Fields and table is not correct
220
	 */
221
	public function attachParameterStorage($sender, $param)
0 ignored issues
show
Unused Code introduced by
The parameter $param is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

221
	public function attachParameterStorage($sender, /** @scrutinizer ignore-unused */ $param)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $sender is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

221
	public function attachParameterStorage(/** @scrutinizer ignore-unused */ $sender, $param)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
222
	{
223
		$this->_setBehavior = new TMapRouteBehavior(null, [$this, 'setFromBehavior']);
224
		$this->getApplication()->getParameters()->attachBehavior(self::APP_PARAMETER_SET_BEHAVIOR, $this->_setBehavior);
225
	}
226
227
	/**
228
	 * Creates the DB table for storing log messages.
229
	 * @todo create sequence for PostgreSQL, and other db
230
	 */
231
	protected function createDbTable()
232
	{
233
		$db = $this->getDbConnection();
234
		$driver = $db->getDriverName();
235
		$autoidAttributes = '';
236
		$postIndices = '; CREATE UNIQUE INDEX tkey ON ' . $this->_tableName . '(' . $this->_keyField . ');' .
237
		($this->_autoLoadField ? ' CREATE INDEX tauto ON ' . $this->_tableName . '(' . $this->_autoLoadField . ');' : '');
238
		if ($driver === 'mysql') {
239
			$autoidAttributes = 'AUTO_INCREMENT';
240
		}
241
242
		$sql = 'CREATE TABLE ' . $this->_tableName . ' (
243
			param_id INTEGER NOT NULL PRIMARY KEY ' . $autoidAttributes . ', ' .
244
			$this->_keyField . ' VARCHAR(128),' .
245
			$this->_valueField . ' MEDIUMTEXT' .
246
			($this->_autoLoadField ? ', ' . $this->_autoLoadField . ' BOOLEAN DEFAULT 1' : '') .
247
			')' . $postIndices;
248
		$db->createCommand($sql)->execute();
249
	}
250
251
	/**
252
	 * checks for the table, and if not there and autoCreate, then creates the table else throw error.
253
	 * @throws TConfigurationException if the table does not exist and cannot autoCreate
254
	 */
255
	protected function ensureTable()
256
	{
257
		$db = $this->getDbConnection();
258
		$sql = 'SELECT * FROM ' . $this->_tableName . ' WHERE 0=1';
259
		try {
260
			$db->createCommand($sql)->query()->close();
261
		} catch (Exception $e) {
262
			// DB table not exists
263
			if ($this->_autoCreate) {
264
				$this->createDbTable();
265
			} else {
266
				throw new TConfigurationException('dbparametermodule_table_nonexistent', $this->_tableName);
267
			}
268
		}
269
	}
270
	
271
	
272
	/**
273
	 * Gets a specific parameter parameters into application.
274
	 * @param string $key key to get the value
275
	 * @param bool $checkParameter checks the Application Parameters first
276
	 * @param bool $setParameter should the method set the application parameters
277
	 * @throws TInvalidOperationException if the $key is blank
278
	 * @throws TDbException if the Fields and table is not correct
279
	 * @return mixed the value of the key
280
	 */
281
	public function get($key, $checkParameter = true, $setParameter = true)
282
	{
283
		if ($key == '') {
284
			throw new TInvalidOperationException('dbparametermodule_get_no_blank_key');
285
		}
286
		
287
		if ($checkParameter) {
288
			$appParams = $this->getApplication()->getParameters();
289
			if (isset($appParams[$key])) {
290
				return $appParams[$key];
291
			}
292
		}
293
		$db = $this->getDbConnection();
294
		$cmd = $db->createCommand(
295
			"SELECT {$this->_valueField} as valueField FROM {$this->_tableName} WHERE {$this->_keyField}=:key LIMIT 1"
296
		);
297
		$cmd->bindParameter(":key", $key, PDO::PARAM_STR);
298
		$results = $cmd->queryRow();
299
		$serializer = $this->getSerializer();
300
		if (is_array($results) && ($value = $results['valueField']) !== null) {
301
			if ($serializer == self::SERIALIZE_PHP) {
302
				if (($avalue = @unserialize($value)) !== false) {
303
					$value = $avalue;
304
				}
305
			} elseif ($serializer == self::SERIALIZE_JSON) {
306
				if (($avalue = json_decode($value, true)) !== null) {
307
					$value = $avalue;
308
				}
309
			} elseif ($serializer && ($avalue = call_user_func($serializer, $value, false)) !== null) {
310
				$value = $avalue;
311
			}
312
			if ($setParameter) {
313
				$appParams = $this->getApplication()->getParameters();
314
				$appParams[$key] = $value;
315
			}
316
			return $value;
317
		}
318
		return null;
319
	}
320
	
321
	/**
322
	 * Loads parameters into application.
323
	 * @param string $key key to get the value
324
	 * @throws TInvalidOperationException if the $key is blank
325
	 * @throws TDbException if the Fields and table is not correct
326
	 * @return mixed the value
327
	 */
328
	public function getFromBehavior($key)
329
	{
330
		return $this->get($key, false, $this->_setBehavior === null);
331
	}
332
	
333
	/**
334
	 * Sets a parameter in the database and the Application Parameter.
335
	 * @param string $key the key of the parameter
336
	 * @param mixed $value the key of the parameter
337
	 * @param bool $autoLoad should the key be autoloaded at init
338
	 * @param mixed $setParameter
339
	 * @throws TInvalidOperationException if the $key is blank
340
	 * @throws TDbException if the Fields and table is not correct
341
	 */
342
	public function set($key, $value, $autoLoad = true, $setParameter = true)
343
	{
344
		if (empty($key)) {
345
			throw new TInvalidOperationException('dbparametermodule_set_no_blank_key');
346
		}
347
			
348
		if (($serializer = $this->getSerializer()) && (is_array($value) || is_object($value))) {
349
			if ($serializer == self::SERIALIZE_PHP) {
350
				$value = @serialize($value);
351
			} elseif ($serializer == self::SERIALIZE_JSON) {
352
				$value = json_encode($value, JSON_UNESCAPED_UNICODE);
353
			} else {
354
				$value = call_user_func($serializer, $value, true);
355
			}
356
		}
357
		$db = $this->getDbConnection();
358
		$driver = $db->getDriverName();
359
		$appendix = '';
360
		if ($driver === 'mysql') {
361
			$dupl = ($this->_autoLoadField ? ", {$this->_autoLoadField}=values({$this->_autoLoadField})" : '');
362
			$appendix = " ON DUPLICATE KEY UPDATE {$this->_valueField}=values({$this->_valueField}){$dupl}";
363
		} else {
364
			$this->remove($key);
365
		}
366
		$field = ($this->_autoLoadField ? ", {$this->_autoLoadField}" : '');
367
		$values = ($this->_autoLoadField ? ", :auto" : '');
368
		$cmd = $db->createCommand("INSERT INTO {$this->_tableName} ({$this->_keyField}, {$this->_valueField}{$field}) " .
369
					"VALUES (:key, :value{$values})" . $appendix);
370
		$cmd->bindParameter(":key", $key, PDO::PARAM_STR);
371
		$cmd->bindParameter(":value", $value, PDO::PARAM_STR);
372
		if ($this->_autoLoadField) {
373
			$alv = $autoLoad ? $this->_autoLoadValue : $this->_autoLoadValueFalse;
374
			$cmd->bindParameter(":auto", $alv, PDO::PARAM_STR);
375
		}
376
		$cmd->execute();
377
		
378
		if ($setParameter) {
379
			$appParameters = $this->getApplication()->getParameters();
380
			$appParameters[$key] = $value;
381
		}
382
	}
383
	
384
	/**
385
	 * Sets a parameter in the database and the Application Parameter.
386
	 * from changes to the Parameter through a TMapRouteBehavior.
387
	 * @param string $key the key of the parameter
388
	 * @param mixed $value the key of the parameter
389
	 * @throws TInvalidOperationException if the $key is blank
390
	 * @throws TDbException if the Fields and table is not correct
391
	 */
392
	public function setFromBehavior($key, $value)
393
	{
394
		if ($value !== null) {
395
			$this->set($key, $value, true, false);
396
		} else {
397
			$this->remove($key);
398
		}
399
	}
400
	
401
	/**
402
	 * exists checks for a parameter in the database
403
	 * @param $key string parameter to check in the database
404
	 * @throws TDbException if the Fields and table is not correct
405
	 * @return bool whether the key exists in the database table
406
	 * @return mixed the value of the parameter, one last time
407
	 */
408
	public function exists($key)
409
	{
410
		$db = $this->getDbConnection();
411
		$cmd = $db->createCommand(
412
			"SELECT COUNT(*) AS count FROM {$this->_tableName} WHERE {$this->_keyField}=:key"
413
		);
414
		$cmd->bindParameter(":key", $key, PDO::PARAM_STR);
415
		$result = $cmd->queryRow();
416
		return $result['count'] > 0;
417
	}
418
	
419
	/**
420
	 * remove removes a parameter from the database
421
	 * @param $key string parameter to remove from the database
422
	 * @throws TDbException if the Fields and table is not correct
423
	 * @return mixed the value of the key removed
424
	 * @return mixed the value of the parameter, one last time
425
	 */
426
	public function remove($key)
427
	{
428
		$value = $this->get($key, false, false);
429
		$db = $this->getDbConnection();
430
		$driver = $db->getDriverName();
431
		$appendix = '';
432
		if ($driver === 'mysql') {
433
			$appendix = ' LIMIT 1';
434
		}
435
		$cmd = $db->createCommand("DELETE FROM {$this->_tableName} WHERE {$this->_keyField}=:key" . $appendix);
436
		$cmd->bindParameter(":key", $key, PDO::PARAM_STR);
437
		$cmd->execute();
438
		return $value;
439
	}
440
	
441
	/**
442
	 * @return string the ID of a TDataSourceConfig module. Defaults to empty string, meaning not set.
443
	 */
444
	public function getConnectionID()
445
	{
446
		return $this->_connID;
447
	}
448
449
	/**
450
	 * Sets the ID of a TDataSourceConfig module.
451
	 * The datasource module will be used to establish the DB connection
452
	 * that will be used by the user manager.
453
	 * @param string $value module ID.
454
	 * @throws TInvalidOperationException if the module is initialized
455
	 */
456
	public function setConnectionID($value)
457
	{
458
		if ($this->_initialized) {
459
			throw new TInvalidOperationException('dbparametermodule_property_unchangeable', 'ConnectionID');
460
		}
461
		$this->_connID = $value;
462
	}
463
464
	/**
465
	 * @return TDbConnection the database connection that may be used to retrieve user data.
466
	 */
467
	public function getDbConnection()
468
	{
469
		if ($this->_conn === null) {
470
			$this->_conn = $this->createDbConnection($this->_connID);
471
			$this->_conn->setActive(true);
472
		}
473
		return $this->_conn;
474
	}
475
476
	/**
477
	 * Creates the DB connection.  If no ConnectionID is set, this creates a
478
	 * sqlite3 database in the RuntimePath "sqlite3.params".  If the
479
	 * {@link getAutoLoadField} is not set, the default, then the autoLoadField
480
	 * is set to "autoload" to enable the feature by default.
481
	 * @param string $connectionID the module ID for TDataSourceConfig
482
	 * @throws TConfigurationException if module ID is invalid or empty
483
	 * @return TDbConnection the created DB connection
484
	 */
485
	protected function createDbConnection($connectionID)
486
	{
487
		if ($connectionID !== '') {
488
			$conn = $this->getApplication()->getModule($connectionID);
489
			if ($conn instanceof TDataSourceConfig) {
490
				return $conn->getDbConnection();
491
			} else {
492
				throw new TConfigurationException('dbparametermodule_connectionid_invalid', $connectionID);
493
			}
494
		} else {
495
			$db = new TDbConnection;
496
			// default to SQLite3 database
497
			$dbFile = $this->getApplication()->getRuntimePath() . '/app.params';
498
			$db->setConnectionString('sqlite:' . $dbFile);
499
			return $db;
500
		}
501
	}
502
503
	/**
504
	 * @return string the database parameter key field
505
	 */
506
	public function getKeyField()
507
	{
508
		return $this->_keyField;
509
	}
510
511
	/**
512
	 * @param string $value database parameter key field
513
	 * @throws TInvalidOperationException if the module is initialized
514
	 */
515
	public function setKeyField($value)
516
	{
517
		if ($this->_initialized) {
518
			throw new TInvalidOperationException('dbparametermodule_property_unchangeable', 'KeyField');
519
		}
520
		$this->_keyField = TPropertyValue::ensureString($value);
521
	}
522
523
	/**
524
	 * @return string the database parameter key value
525
	 */
526
	public function getValueField()
527
	{
528
		return $this->_valueField;
529
	}
530
531
	/**
532
	 * @param string $value database parameter key value
533
	 * @throws TInvalidOperationException if the module is initialized
534
	 */
535
	public function setValueField($value)
536
	{
537
		if ($this->_initialized) {
538
			throw new TInvalidOperationException('dbparametermodule_property_unchangeable', 'ValueField');
539
		}
540
		$this->_valueField = TPropertyValue::ensureString($value);
541
	}
542
543
	/**
544
	 * @return string the database parameter key value
545
	 */
546
	public function getTableName()
547
	{
548
		return $this->_tableName;
549
	}
550
551
	/**
552
	 * @param string $value database parameter key value
553
	 * @throws TInvalidOperationException if the module is initialized
554
	 */
555
	public function setTableName($value)
556
	{
557
		if ($this->_initialized) {
558
			throw new TInvalidOperationException('dbparametermodule_property_unchangeable', 'TableName');
559
		}
560
		$this->_tableName = TPropertyValue::ensureString($value);
561
	}
562
563
	/**
564
	 * @return string the database parameter key value
565
	 */
566
	public function getAutoLoadField()
567
	{
568
		return $this->_autoLoadField;
569
	}
570
571
	/**
572
	 * @param $value string database parameter key value
573
	 * @throws TInvalidOperationException if the module is initialized
574
	 */
575
	public function setAutoLoadField($value)
576
	{
577
		if ($this->_initialized) {
578
			throw new TInvalidOperationException('dbparametermodule_property_unchangeable', 'AutoLoadField');
579
		}
580
		$this->_autoLoadField = TPropertyValue::ensureString($value);
581
	}
582
583
	/**
584
	 * @return string the database parameter key value
585
	 */
586
	public function getAutoLoadValue()
587
	{
588
		return $this->_autoLoadValue;
589
	}
590
591
	/**
592
	 * @param string $value database parameter key value
593
	 * @throws TInvalidOperationException if the module is initialized
594
	 */
595
	public function setAutoLoadValue($value)
596
	{
597
		if ($this->_initialized) {
598
			throw new TInvalidOperationException('dbparametermodule_property_unchangeable', 'AutoLoadValue');
599
		}
600
		$this->_autoLoadValue = TPropertyValue::ensureString($value);
601
	}
602
603
	/**
604
	 * @return string the database parameter key value
605
	 */
606
	public function getAutoLoadValueFalse()
607
	{
608
		return $this->_autoLoadValueFalse;
609
	}
610
611
	/**
612
	 * @param string $value database parameter key value
613
	 * @throws TInvalidOperationException if the module is initialized
614
	 */
615
	public function setAutoLoadValueFalse($value)
616
	{
617
		if ($this->_initialized) {
618
			throw new TInvalidOperationException('dbparametermodule_property_unchangeable', 'AutoLoadValueFalse');
619
		}
620
		$this->_autoLoadValueFalse = TPropertyValue::ensureString($value);
621
	}
622
623
	/**
624
	 * @return bool whether the paramter DB table should be automatically created if not exists. Defaults to true.
625
	 * @see setAutoCreateParamTable
626
	 */
627
	public function getAutoCreateParamTable()
628
	{
629
		return $this->_autoCreate;
630
	}
631
632
	/**
633
	 * @param bool $value whether the parameter DB table should be automatically created if not exists.
634
	 * @see setTableName
635
	 */
636
	public function setAutoCreateParamTable($value)
637
	{
638
		$this->_autoCreate = TPropertyValue::ensureBoolean($value);
639
	}
640
	
641
	/**
642
	 * @return null|callable|string
643
	 */
644
	public function getSerializer()
645
	{
646
		return $this->_serializer;
647
	}
648
649
	/**
650
	 * Serializer sets the type of serialization of objects and arrays in parameters
651
	 * to and from the database.  'php' uses serialze and unserialize. 'json' uses
652
	 * json_encode and json_decade. or you can provide your own callable to serialized
653
	 * and unserialize objects and arrays.
654
	 * @param callable|string $value the type of un/serialization.
655
	 * @throws TInvalidOperationException if the module is initialized
656
	 * @throws TInvalidDataTypeException if the $value is not 'php', 'json', or a callable
657
	 */
658
	public function setSerializer($value)
659
	{
660
		if ($this->_initialized) {
661
			throw new TInvalidOperationException('dbparametermodule_property_unchangeable', 'Serializer');
662
		}
663
		if ($value !== self::SERIALIZE_PHP && $value !== self::SERIALIZE_JSON && !is_callable($value)) {
664
			throw new TInvalidDataTypeException('dbparametermodule_serializer_not_callable');
665
		}
666
		$this->_serializer = $value;
667
	}
668
669
	/**
670
	 * @return bool whether the paramter DB table should be automatically created if not exists. Defaults to true.
671
	 */
672
	public function getCaptureParameterChanges()
673
	{
674
		return $this->_autoCapture;
675
	}
676
677
	/**
678
	 * @param bool $value whether the parameter DB table should be automatically created if not exists.
679
	 */
680
	public function setCaptureParameterChanges($value)
681
	{
682
		$this->_autoCapture = TPropertyValue::ensureBoolean($value);
683
	}
684
}
685