Standard::data()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 52
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 17
nc 2
nop 3
dl 0
loc 52
rs 9.7
c 0
b 0
f 0

How to fix   Long Method   

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
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Locale\Select;
12
13
14
/**
15
 * Default implementation of locale select HTML client.
16
 *
17
 * @package Client
18
 * @subpackage Html
19
 */
20
class Standard
21
	extends \Aimeos\Client\Html\Common\Client\Factory\Base
22
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
23
{
24
	/** client/html/locale/select/name
25
	 * Class name of the used locale select client implementation
26
	 *
27
	 * Each default HTML client can be replace by an alternative imlementation.
28
	 * To use this implementation, you have to set the last part of the class
29
	 * name as configuration value so the client factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\Client\Html\Locale\Select\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Client\Html\Locale\Select\Myselect
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  client/html/locale/select/name = Myselect
43
	 *
44
	 * The value is the last part of your own class name and it's case sensitive,
45
	 * so take care that the configuration value is exactly named like the last
46
	 * part of the class name.
47
	 *
48
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
49
	 * characters are possible! You should always start the last part of the class
50
	 * name with an upper case character and continue only with lower case characters
51
	 * or numbers. Avoid chamel case names like "MySelect"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2014.03
55
	 */
56
57
58
	/** client/html/locale/select/subparts
59
	 * List of HTML sub-clients rendered within the locale select section
60
	 *
61
	 * The output of the frontend is composed of the code generated by the HTML
62
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
63
	 * that are responsible for rendering certain sub-parts of the output. The
64
	 * sub-clients can contain HTML clients themselves and therefore a
65
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
66
	 * the output that is placed inside the container of its parent.
67
	 *
68
	 * At first, always the HTML code generated by the parent is printed, then
69
	 * the HTML code of its sub-clients. The order of the HTML sub-clients
70
	 * determines the order of the output of these sub-clients inside the parent
71
	 * container. If the configured list of clients is
72
	 *
73
	 *  array( "subclient1", "subclient2" )
74
	 *
75
	 * you can easily change the order of the output by reordering the subparts:
76
	 *
77
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
78
	 *
79
	 * You can also remove one or more parts if they shouldn't be rendered:
80
	 *
81
	 *  client/html/<clients>/subparts = array( "subclient1" )
82
	 *
83
	 * As the clients only generates structural HTML, the layout defined via CSS
84
	 * should support adding, removing or reordering content by a fluid like
85
	 * design.
86
	 *
87
	 * @param array List of sub-client names
88
	 * @since 2014.09
89
	 */
90
	private string $subPartPath = 'client/html/locale/select/subparts';
91
92
	/** client/html/locale/select/language/name
93
	 * Name of the language part used by the locale selector client implementation
94
	 *
95
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Locale\Select\Language\Myname".
96
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
97
	 *
98
	 * @param string Last part of the client class name
99
	 * @since 2014.09
100
	 */
101
102
	/** client/html/locale/select/currency/name
103
	 * Name of the currency part used by the locale selector client implementation
104
	 *
105
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Locale\Select\Currency\Myname".
106
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
107
	 *
108
	 * @param string Last part of the client class name
109
	 * @since 2014.09
110
	 */
111
	private array $subPartNames = array( 'language', 'currency' );
112
113
	private array $tags = [];
114
	private ?string $expire = null;
115
	private ?\Aimeos\Base\View\Iface $view = null;
116
117
118
	/**
119
	 * Returns the HTML code for insertion into the body.
120
	 *
121
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
122
	 * @return string HTML code
123
	 */
124
	public function body( string $uid = '' ) : string
125
	{
126
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
127
128
		$html = '';
129
		foreach( $this->getSubClients() as $subclient ) {
130
			$html .= $subclient->setView( $view )->body( $uid );
131
		}
132
133
		/** client/html/locale/select/template-body
134
		 * Relative path to the HTML body template of the locale select client.
135
		 *
136
		 * The template file contains the HTML code and processing instructions
137
		 * to generate the result shown in the body of the frontend. The
138
		 * configuration string is the path to the template file relative
139
		 * to the templates directory (usually in templates/client/html).
140
		 *
141
		 * You can overwrite the template file configuration in extensions and
142
		 * provide alternative templates. These alternative templates should be
143
		 * named like the default one but suffixed by
144
		 * an unique name. You may use the name of your project for this. If
145
		 * you've implemented an alternative client class as well, it
146
		 * should be suffixed by the name of the new class.
147
		 *
148
		 * @param string Relative path to the template creating code for the HTML page body
149
		 * @since 2014.09
150
		 * @see client/html/locale/select/template-header
151
		 */
152
		$template = $this->context()->config()->get( 'client/html/locale/select/template-body', 'locale/select/body' );
153
154
		return $view->set( 'body', $html )->render( $template );
155
	}
156
157
158
	/**
159
	 * Returns the HTML string for insertion into the header.
160
	 *
161
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
162
	 * @return string|null String including HTML tags for the header on error
163
	 */
164
	public function header( string $uid = '' ) : ?string
165
	{
166
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
167
168
		/** client/html/locale/select/template-header
169
		 * Relative path to the HTML header template of the locale select client.
170
		 *
171
		 * The template file contains the HTML code and processing instructions
172
		 * to generate the HTML code that is inserted into the HTML page header
173
		 * of the rendered page in the frontend. The configuration string is the
174
		 * path to the template file relative to the templates directory (usually
175
		 * in templates/client/html).
176
		 *
177
		 * You can overwrite the template file configuration in extensions and
178
		 * provide alternative templates. These alternative templates should be
179
		 * named like the default one but suffixed by
180
		 * an unique name. You may use the name of your project for this. If
181
		 * you've implemented an alternative client class as well, it
182
		 * should be suffixed by the name of the new class.
183
		 *
184
		 * @param string Relative path to the template creating code for the HTML page head
185
		 * @since 2014.09
186
		 * @see client/html/locale/select/template-body
187
		 */
188
		$template = $this->context()->config()->get( 'client/html/locale/select/template-header', 'locale/select/header' );
189
190
		return $view->render( $template );
191
	}
192
193
194
	/**
195
	 * Returns the sub-client given by its name.
196
	 *
197
	 * @param string $type Name of the client type
198
	 * @param string|null $name Name of the sub-client (Default if null)
199
	 * @return \Aimeos\Client\Html\Iface Sub-client object
200
	 */
201
	public function getSubClient( string $type, ?string $name = null ) : \Aimeos\Client\Html\Iface
202
	{
203
		return $this->createSubClient( 'locale/select/' . $type, $name );
204
	}
205
206
207
	/**
208
	 * Returns the list of sub-client names configured for the client.
209
	 *
210
	 * @return array List of HTML client names
211
	 */
212
	protected function getSubClientNames() : array
213
	{
214
		return $this->context()->config()->get( $this->subPartPath, $this->subPartNames );
215
	}
216
217
218
	/**
219
	 * Sets the necessary parameter values in the view.
220
	 *
221
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
222
	 * @param array &$tags Result array for the list of tags that are associated to the output
223
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
224
	 * @return \Aimeos\Base\View\Iface Modified view object
225
	 */
226
	public function data( \Aimeos\Base\View\Iface $view, array &$tags = [], ?string &$expire = null ) : \Aimeos\Base\View\Iface
227
	{
228
		$map = [];
229
		$context = $this->context();
230
		$config = $context->config();
231
		$locale = $context->locale();
232
233
		/** client/html/locale/select/language/param-name
234
		 * Name of the parameter that contains the language ID value
235
		 *
236
		 * Frameworks and applications normally use its own predefined parameter
237
		 * that contains the current language ID if they are multi-language
238
		 * capable. To adapt the Aimeos parameter name to the already used name,
239
		 * you are able to configure it by using this setting.
240
		 *
241
		 * @param string Parameter name for language ID
242
		 * @since 2015.06
243
		 * @see client/html/locale/select/currency/param-name
244
		 */
245
		$langname = $config->get( 'client/html/locale/select/language/param-name', 'locale' );
246
247
		/** client/html/locale/select/currency/param-name
248
		 * Name of the parameter that contains the currency ID value
249
		 *
250
		 * Frameworks and applications normally use its own predefined parameter
251
		 * that contains the current currency ID if they already support multiple
252
		 * currencies. To adapt the Aimeos parameter name to the already used name,
253
		 * you are able to configure it by using this setting.
254
		 *
255
		 * @param string Parameter name for currency ID
256
		 * @since 2015.06
257
		 * @see client/html/locale/select/language/param-name
258
		 */
259
		$curname = $config->get( 'client/html/locale/select/currency/param-name', 'currency' );
260
261
262
		$items = \Aimeos\Controller\Frontend::create( $context, 'locale' )
263
			->sort( 'position' )->slice( 0, 10000 )->search();
264
265
		foreach( $items as $item )
266
		{
267
			$curId = $item->getCurrencyId();
268
			$langId = $item->getLanguageId();
269
			$map[$langId][$curId] = [$langname => $langId, $curname => $curId];
270
		}
271
272
		$view->selectMap = map( $map );
273
		$view->selectParams = $view->param();
274
		$view->selectLanguageId = $locale->getLanguageId();
275
		$view->selectCurrencyId = $locale->getCurrencyId();
276
277
		return parent::data( $view, $tags, $expire );
278
	}
279
280
281
	/** client/html/locale/select/decorators/excludes
282
	 * Excludes decorators added by the "common" option from the locale select html client
283
	 *
284
	 * Decorators extend the functionality of a class by adding new aspects
285
	 * (e.g. log what is currently done), executing the methods of the underlying
286
	 * class only in certain conditions (e.g. only for logged in users) or
287
	 * modify what is returned to the caller.
288
	 *
289
	 * This option allows you to remove a decorator added via
290
	 * "client/html/common/decorators/default" before they are wrapped
291
	 * around the html client.
292
	 *
293
	 *  client/html/locale/select/decorators/excludes = array( 'decorator1' )
294
	 *
295
	 * This would remove the decorator named "decorator1" from the list of
296
	 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
297
	 * "client/html/common/decorators/default" to the html client.
298
	 *
299
	 * @param array List of decorator names
300
	 * @see client/html/common/decorators/default
301
	 * @see client/html/locale/select/decorators/global
302
	 * @see client/html/locale/select/decorators/local
303
	 */
304
305
	/** client/html/locale/select/decorators/global
306
	 * Adds a list of globally available decorators only to the locale select html client
307
	 *
308
	 * Decorators extend the functionality of a class by adding new aspects
309
	 * (e.g. log what is currently done), executing the methods of the underlying
310
	 * class only in certain conditions (e.g. only for logged in users) or
311
	 * modify what is returned to the caller.
312
	 *
313
	 * This option allows you to wrap global decorators
314
	 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
315
	 *
316
	 *  client/html/locale/select/decorators/global = array( 'decorator1' )
317
	 *
318
	 * This would add the decorator named "decorator1" defined by
319
	 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
320
	 *
321
	 * @param array List of decorator names
322
	 * @see client/html/common/decorators/default
323
	 * @see client/html/locale/select/decorators/excludes
324
	 * @see client/html/locale/select/decorators/local
325
	 */
326
327
	/** client/html/locale/select/decorators/local
328
	 * Adds a list of local decorators only to the locale select html client
329
	 *
330
	 * Decorators extend the functionality of a class by adding new aspects
331
	 * (e.g. log what is currently done), executing the methods of the underlying
332
	 * class only in certain conditions (e.g. only for logged in users) or
333
	 * modify what is returned to the caller.
334
	 *
335
	 * This option allows you to wrap local decorators
336
	 * ("\Aimeos\Client\Html\Locale\Decorator\*") around the html client.
337
	 *
338
	 *  client/html/locale/select/decorators/local = array( 'decorator2' )
339
	 *
340
	 * This would add the decorator named "decorator2" defined by
341
	 * "\Aimeos\Client\Html\Locale\Decorator\Decorator2" only to the html client.
342
	 *
343
	 * @param array List of decorator names
344
	 * @see client/html/common/decorators/default
345
	 * @see client/html/locale/select/decorators/excludes
346
	 * @see client/html/locale/select/decorators/global
347
	 */
348
}
349