Standard::getLabel()   A
last analyzed

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
eloc 1
nc 1
nop 0
dl 0
loc 3
c 1
b 0
f 0
cc 1
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021
6
 * @package MShop
7
 * @subpackage Cms
8
 */
9
10
11
namespace Aimeos\MShop\Cms\Item;
12
13
14
/**
15
 * Default cms manager implementation.
16
 *
17
 * @package MShop
18
 * @subpackage Cms
19
 */
20
class Standard
21
	extends \Aimeos\MShop\Common\Item\Base
22
	implements \Aimeos\MShop\Cms\Item\Iface
23
{
24
	use \Aimeos\MShop\Common\Item\ListsRef\Traits;
25
26
27
	/**
28
	 * Initializes the cms item object with the given values.
29
	 *
30
	 * @param array $values Associative list of key/value pairs
31
	 * @param \Aimeos\MShop\Common\Item\Lists\Iface[] $listItems List of list items
32
	 * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items
33
	 */
34
	public function __construct( array $values = [], array $listItems = [], array $refItems = [] )
35
	{
36
		parent::__construct( 'cms.', $values );
37
38
		$this->initListItems( $listItems, $refItems );
39
	}
40
41
42
	/**
43
	 * Returns the URL of the cms item.
44
	 *
45
	 * @return string URL of the cms item
46
	 */
47
	public function getUrl() : string
48
	{
49
		return $this->get( 'cms.url', '' );
50
	}
51
52
53
	/**
54
	 * Sets the URL of the cms item.
55
	 *
56
	 * @param string $value URL of the cms item
57
	 * @return \Aimeos\MShop\Cms\Item\Iface Cms item for chaining method calls
58
	 */
59
	public function setUrl( string $value ) : \Aimeos\MShop\Cms\Item\Iface
60
	{
61
		$url = \Aimeos\Map::explode( '/', trim( $value, '/' ) )->map( function( $segment ) {
62
			return \Aimeos\MW\Str::slug( $segment );
63
		} )->join( '/' );
64
65
		return $this->set( 'cms.url', '/' . $url );
66
	}
67
68
69
	/**
70
	 * Returns the name of the attribute item.
71
	 *
72
	 * @return string Label of the attribute item
73
	 */
74
	public function getLabel() : string
75
	{
76
		return $this->get( 'cms.label', '' );
77
	}
78
79
80
	/**
81
	 * Sets the new label of the attribute item.
82
	 *
83
	 * @param string $label Type label of the attribute item
84
	 * @return \Aimeos\MShop\Cms\Item\Iface Cms item for chaining method calls
85
	 */
86
	public function setLabel( ?string $label ) : \Aimeos\MShop\Cms\Item\Iface
87
	{
88
		return $this->set( 'cms.label', (string) $label );
89
	}
90
91
92
	/**
93
	 * Returns the status of the cms item.
94
	 *
95
	 * @return int Status of the cms item
96
	 */
97
	public function getStatus() : int
98
	{
99
		return $this->get( 'cms.status', 1 );
100
	}
101
102
103
	/**
104
	 * Sets the status of the cms item.
105
	 *
106
	 * @param int $status true/false for enabled/disabled
107
	 * @return \Aimeos\MShop\Cms\Item\Iface Cms item for chaining method calls
108
	 */
109
	public function setStatus( int $status ) : \Aimeos\MShop\Common\Item\Iface
110
	{
111
		return $this->set( 'cms.status', $status );
112
	}
113
114
115
	/**
116
	 * Returns the item type
117
	 *
118
	 * @return string Item type, subtypes are separated by slashes
119
	 */
120
	public function getResourceType() : string
121
	{
122
		return 'cms';
123
	}
124
125
126
	/**
127
	 * Tests if the item is available based on status, time, language and currency
128
	 *
129
	 * @return bool True if available, false if not
130
	 */
131
	public function isAvailable() : bool
132
	{
133
		return parent::isAvailable() && $this->getStatus() > 0;
134
	}
135
136
137
	/**
138
	 * Sets the item values from the given array and removes that entries from the list
139
	 *
140
	 * @param array &$list Associative list of item keys and their values
141
	 * @param bool True to set private properties too, false for public only
142
	 * @return \Aimeos\MShop\Cms\Item\Iface Cms item for chaining method calls
143
	 */
144
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
145
	{
146
		$item = parent::fromArray( $list, $private );
147
148
		foreach( $list as $key => $value )
149
		{
150
			switch( $key )
151
			{
152
				case 'cms.url': $item = $item->setUrl( $value ); break;
153
				case 'cms.label': $item = $item->setLabel( $value ); break;
154
				case 'cms.status': $item = $item->setStatus( (int) $value ); break;
155
				default: continue 2;
156
			}
157
158
			unset( $list[$key] );
159
		}
160
161
		return $item;
162
	}
163
164
165
	/**
166
	 * Returns the item values as array.
167
	 *
168
	 * @param bool True to return private properties, false for public only
169
	 * @return array Associative list of item properties and their values
170
	 */
171
	public function toArray( bool $private = false ) : array
172
	{
173
		$list = parent::toArray( $private );
174
175
		$list['cms.url'] = $this->getUrl();
176
		$list['cms.label'] = $this->getLabel();
177
		$list['cms.status'] = $this->getStatus();
178
179
		return $list;
180
	}
181
}
182