Completed
Push — develop ( 3f13ed...151ba0 )
by Zack
30:06 queued 21:30
created

Settings::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 12 and the first side effect is on line 6.

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.

Loading history...
2
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * A generic Settings base class.
11
 */
12
class Settings {
13
	/**
14
	 * @var array Main storage for key-values in this collection.
15
	 */
16
	private $settings = array();
17
18
	/**
19
	 * Create with new.
20
	 *
21
	 * @api
22
	 * @since 2.0
23
	 *
24
	 * @param array $settings Initial settings. Default: none.
25
	 * @return \GV\Settings
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
26
	 */
27 153
	public function __construct( $settings = array() ) {
28 153
		if ( is_array( $settings ) && ! empty( $settings ) ) {
29 35
			$this->update( $settings );
30
		}
31 153
	}
32
33
	/**
34
	 * Mass update values from the allowed ones.
35
	 *
36
	 * @api
37
	 * @since 2.0
38
	 *
39
	 * @param array $settings An array of settings to update.
40
	 * @return \GV\Settings self chain.
41
	 */
42 154
	public function update( $settings ) {
43 154
		foreach ( $settings as $key => $value ) {
44 154
			$this->set( $key, $value );
45
		}
46 154
		return $this;
47
	}
48
49
	/**
50
	 * Set a setting.
51
	 *
52
	 * @param mixed $key The key the value should be added under.
53
	 * @param mixed $value The value to be added to the key.
54
	 *
55
	 * @api
56
	 * @since 2.0
57
	 * @return void
58
	 */
59 154
	public function set( $key, $value ) {
60 154
		$this->settings[ $key ] = $value;
61 154
	}
62
63
	/**
64
	 * Set an setting.
65
	 *
66
	 * @param mixed $key The key in this setting to retrieve.
67
	 * @param mixed $default A default in case the key is not set. 
68
	 *
69
	 * @api
70
	 * @since 2.0
71
	 * @return mixed|null
72
	 */
73 106
	public function get( $key, $default = null ) {
74 106
		return Utils::get( $this->settings, $key, $default );
75
	}
76
77
	/**
78
	 * Returns all the objects in this collection as an an array.
79
	 *
80
	 * @api
81
	 * @since 2.0
82
	 * @return array The objects in this collection.
83
	 */
84 7
	public function all() {
85 7
		return $this->settings;
86
	}
87
}
88