Passed
Pull Request — master (#780)
by
unknown
05:43
created

TDbParameterModule::set()   D

Complexity

Conditions 14
Paths 289

Size

Total Lines 39
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 30
c 1
b 0
f 0
nc 289
nop 4
dl 0
loc 39
rs 4.3208

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	
142
	/**
143
	 * Initializes the module by loading parameters.
144
	 * @param mixed $config content enclosed within the module tag
145
	 */
146
	public function init($config)
147
	{
148
		$this->loadDbParameters();
149
		$this->_initialized = true;
150
		
151
		if ($this->_autoLoadField) {
152
			$this->getApplication()->getParameters()->attachBehavior(self::APP_PARAMETER_LAZY_BEHAVIOR, new TMapLazyLoadBehavior([$this, 'getFromBehavior']));
153
		}
154
		if ($this->_autoCapture) {
155
			$this->getApplication()->attachEventHandler('onBeginRequest', [$this, 'attachTPageServiceHandler']);
156
		}
157
		parent::init($config);
158
	}
159
	
160
	/**
161
	 * Loads parameters from the database into the application.
162
	 * @throws TDbException if the Fields and table is not correct
163
	 */
164
	protected function loadDbParameters()
165
	{
166
		$db = $this->getDbConnection();
167
		
168
		$this->ensureTable();
169
		
170
		$where = ($this->_autoLoadField ? " WHERE {$this->_autoLoadField}={$this->_autoLoadValue}" : '');
171
		$cmd = $db->createCommand(
172
			"SELECT {$this->_keyField} as keyField, {$this->_valueField}  as valueField FROM {$this->_tableName}{$where}"
173
		);
174
		$results = $cmd->query();
175
		
176
		$appParameters = $this->getApplication()->getParameters();
177
		$serializer = $this->getSerializer();
178
		foreach ($results->readAll() as $row) {
179
			$value = $row['valueField'];
180
			if ($serializer == self::SERIALIZE_PHP) {
181
				if (($avalue = @unserialize($value)) !== false) {
182
					$value = $avalue;
183
				}
184
			} elseif ($serializer == self::SERIALIZE_JSON) {
185
				if (($avalue = json_decode($value, true)) !== null) {
186
					$value = $avalue;
187
				}
188
			} elseif ($serializer) {
189
				if (($avalue = call_user_func($serializer, $value, false)) !== null) {
190
					$value = $avalue;
191
				}
192
			}
193
			$appParameters[$row['keyField']] = $value;
194
		}
195
	}
196
	
197
	/**
198
	 * TApplication::onBeginRequest Handler that adds {@link attachTPageBehaviors} to
199
	 * TPageService::onPreRunPage. In turn, this attaches {@link attachTPageBehaviors}
200
	 * to TPageService to then adds the page behaviors.
201
	 * @param object $sender the object that raised the event
202
	 * @param mixed $param parameter of the event
203
	 */
204
	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

204
	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

204
	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...
205
	{
206
		$service = $this->getService();
207
		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

207
		if ($service->/** @scrutinizer ignore-call */ hasEvent('onPreRunPage')) {
Loading history...
208
			$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

208
			$service->/** @scrutinizer ignore-call */ 
209
             attachEventHandler('onPreRunPage', [$this, 'attachParameterStorage'], 0);
Loading history...
209
		}
210
	}
211
	
212
	/**
213
	 * This attaches the TMapRouteBehavior on the Parameters.
214
	 * @param object $sender
215
	 * @param null|mixed $param
216
	 * @throws TDbException if the Fields and table is not correct
217
	 */
218
	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

218
	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

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