Passed
Pull Request — master (#780)
by
unknown
07:06
created

TDbParameterModule::attachParameterStorage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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