Completed
Push — master ( 2fdc96...4f1f24 )
by Damian
12:09
created

HtmlEditorConfig::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
/**
4
 * A PHP version of TinyMCE's configuration, to allow various parameters to be configured on a site or section basis
5
 *
6
 * There can be multiple HtmlEditorConfig's, which should always be created / accessed using HtmlEditorConfig::get.
7
 * You can then set the currently active config using set_active.
8
 * The order of precendence for which config is used is (lowest to highest):
9
 *
10
 * - default_config config setting
11
 * - Active config assigned
12
 * - Config name assigned to HtmlEditorField
13
 * - Config instance assigned to HtmlEditorField
14
 *
15
 * Typically global config changes should set the active config.
16
 *
17
 * The defaut config class can be changed via dependency injection to replace HtmlEditorConfig.
18
 *
19
 * @author "Hamish Friedlander" <[email protected]>
20
 * @package forms
21
 * @subpackage fields-formattedinput
22
 */
23
abstract class HtmlEditorConfig extends Object {
24
25
	/**
26
	 * Array of registered configurations
27
	 *
28
	 * @var HtmlEditorConfig[]
29
	 */
30
	protected static $configs = array();
31
32
	/**
33
	 * Identifier key of current config. This will match an array key in $configs.
34
	 * If left blank, will fall back to value of default_config set via config.
35
	 *
36
	 * @var string
37
	 */
38
	protected static $current = null;
39
40
	/**
41
	 * Name of default config. This will be ignored if $current is assigned a value.
42
	 *
43
	 * @config
44
	 * @var string
45
	 */
46
	private static $default_config = 'default';
47
48
	/**
49
	 * Get the HtmlEditorConfig object for the given identifier. This is a correct way to get an HtmlEditorConfig
50
	 * instance - do not call 'new'
51
	 *
52
	 * @param string $identifier The identifier for the config set. If omitted, the active config is returned.
53
	 * @return HtmlEditorConfig The configuration object.
54
	 * This will be created if it does not yet exist for that identifier
55
	 */
56
	public static function get($identifier = null) {
57
		if(!$identifier) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $identifier of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
58
			return static::get_active();
59
		}
60
		// Create new instance if unconfigured
61
		if (!isset(self::$configs[$identifier])) {
62
			self::$configs[$identifier] = static::create();
63
		}
64
		return self::$configs[$identifier];
65
	}
66
67
	/**
68
	 * Assign a new config for the given identifier
69
	 *
70
	 * @param string $identifier A specific identifier
71
	 * @param HtmlEditorConfig $config
72
	 * @return HtmlEditorConfig The assigned config
73
	 */
74
	public static function set_config($identifier, HtmlEditorConfig $config) {
75
		self::$configs[$identifier] = $config;
76
		return $config;
77
	}
78
79
	/**
80
	 * Set the currently active configuration object. Note that the existing active
81
	 * config will not be renamed to the new identifier.
82
	 *
83
	 * @param string $identifier The identifier for the config set
84
	 */
85
	public static function set_active_identifier($identifier) {
86
		self::$current = $identifier;
87
	}
88
89
	/**
90
	 * Get the currently active configuration identifier. Will fall back to default_config
91
	 * if unassigned.
92
	 *
93
	 * @return string The active configuration identifier
94
	 */
95
	public static function get_active_identifier() {
96
		$identifier = self::$current ?: static::config()->default_config;
97
		return $identifier;
98
	}
99
100
	/**
101
	 * Get the currently active configuration object
102
	 *
103
	 * @return HtmlEditorConfig The active configuration object
104
	 */
105
	public static function get_active() {
106
		$identifier = self::get_active_identifier();
107
		return self::get($identifier);
108
	}
109
110
	/**
111
	 * Assigns the currently active config an explicit instance
112
	 *
113
	 * @param HtmlEditorConfig $config
114
	 * @return HtmlEditorConfig The given config
115
	 */
116
	public static function set_active(HtmlEditorConfig $config) {
117
		$identifier = static::get_active_identifier();
118
		return static::set_config($identifier, $config);
119
	}
120
121
	/**
122
	 * Get the available configurations as a map of friendly_name to
123
	 * configuration name.
124
	 *
125
	 * @return array
126
	 */
127
	public static function get_available_configs_map() {
128
		$configs = array();
129
130
		foreach(self::$configs as $identifier => $config) {
131
			$configs[$identifier] = $config->getOption('friendly_name');
132
		}
133
134
		return $configs;
135
	}
136
137
	/**
138
	 * Get the current value of an option
139
     *
140
	 * @param string $key The key of the option to get
141
	 * @return mixed The value of the specified option
142
	 */
143
	abstract public function getOption($key);
144
145
	/**
146
	 * Set the value of one option
147
	 * @param string $key The key of the option to set
148
	 * @param mixed $value The value of the option to set
149
	 * @return $this
150
	 */
151
	abstract public function setOption($key, $value);
152
153
	/**
154
	 * Set multiple options. This does not merge recursively, but only at the top level.
155
     *
156
	 * @param array $options The options to set, as keys and values of the array
157
	 * @return $this
158
	 */
159
	abstract public function setOptions($options);
160
161
	/**
162
	 * Associative array of data-attributes to apply to the underlying text-area
163
	 *
164
	 * @return array
165
	 */
166
	abstract public function getAttributes();
167
168
	/**
169
	 * Initialise the editor on the client side
170
	 */
171
	abstract public function init();
172
173
}
174