1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Joomla! common library. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2017 Roberto Segura López, Inc. All rights reserved. |
6
|
|
|
* @license GNU/GPL 2, http://www.gnu.org/licenses/gpl-2.0.htm |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Phproberto\Joomla\Traits; |
10
|
|
|
|
11
|
1 |
|
defined('JPATH_PLATFORM') || die; |
12
|
|
|
|
13
|
|
|
use Joomla\Registry\Registry; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Trait for classes that have parameters. |
17
|
|
|
* |
18
|
|
|
* @since 0.0.1 |
19
|
|
|
*/ |
20
|
|
|
trait HasParams |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Module parameters. |
24
|
|
|
* |
25
|
|
|
* @var Registry |
26
|
|
|
*/ |
27
|
|
|
protected $params; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get a param value. |
31
|
|
|
* |
32
|
|
|
* @param string $name Parameter name |
33
|
|
|
* @param mixed $default Optional default value, returned if the internal value is null. |
34
|
|
|
* |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
3 |
|
public function param($name, $default = null) |
38
|
|
|
{ |
39
|
3 |
|
return $this->params()->get($name, $default); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the parameters. |
44
|
|
|
* |
45
|
|
|
* @param boolean $reload Force reloading data from DB. |
46
|
|
|
* |
47
|
|
|
* @return Registry |
48
|
|
|
*/ |
49
|
4 |
|
public function params($reload = false) |
50
|
|
|
{ |
51
|
4 |
|
if ($reload || null === $this->params) |
52
|
|
|
{ |
53
|
4 |
|
$this->params = $this->loadParams(); |
54
|
|
|
} |
55
|
|
|
|
56
|
4 |
|
return clone $this->params; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Load parameters from database. |
61
|
|
|
* |
62
|
|
|
* @return Registry |
63
|
|
|
*/ |
64
|
|
|
abstract protected function loadParams(); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Save parameters to database. |
68
|
|
|
* |
69
|
|
|
* @return boolean |
70
|
|
|
*/ |
71
|
|
|
abstract public function saveParams(); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set the value of a parameter. |
75
|
|
|
* |
76
|
|
|
* @param string $name Parameter name |
77
|
|
|
* @param mixed $value Value to assign to selected parameter |
78
|
|
|
* |
79
|
|
|
* @return self |
80
|
|
|
*/ |
81
|
1 |
|
public function setParam($name, $value) |
82
|
|
|
{ |
83
|
1 |
|
if (null === $this->params) |
84
|
|
|
{ |
85
|
1 |
|
$this->params = $this->loadParams(); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
$this->params->set($name, $value); |
89
|
|
|
|
90
|
1 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set the module parameters. |
95
|
|
|
* |
96
|
|
|
* @param Registry $params Parameters to apply |
97
|
|
|
* |
98
|
|
|
* @return self |
99
|
|
|
*/ |
100
|
1 |
|
public function setParams(Registry $params) |
101
|
|
|
{ |
102
|
1 |
|
$this->params = $params; |
103
|
|
|
|
104
|
1 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.