Standard::setUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2025
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
	 */
32
	public function __construct( string $prefix, array $values = [] )
33
	{
34
		parent::__construct( $prefix, $values );
35
36
		$this->initListItems( $values['.listitems'] ?? [] );
37
	}
38
39
40
	/**
41
	 * Returns the URL of the cms item.
42
	 *
43
	 * @return string URL of the cms item
44
	 */
45
	public function getUrl() : string
46
	{
47
		return $this->get( 'cms.url', '' );
48
	}
49
50
51
	/**
52
	 * Sets the URL of the cms item.
53
	 *
54
	 * @param string $value URL of the cms item
55
	 * @return \Aimeos\MShop\Cms\Item\Iface Cms item for chaining method calls
56
	 */
57
	public function setUrl( string $value ) : \Aimeos\MShop\Cms\Item\Iface
58
	{
59
		$url = \Aimeos\Map::explode( '/', trim( $value, '/' ) )->map( function( $segment ) {
60
			return \Aimeos\Base\Str::slug( $segment );
61
		} )->join( '/' );
62
63
		return $this->set( 'cms.url', '/' . $url );
64
	}
65
66
67
	/**
68
	 * Returns the name of the attribute item.
69
	 *
70
	 * @return string Label of the attribute item
71
	 */
72
	public function getLabel() : string
73
	{
74
		return $this->get( 'cms.label', '' );
75
	}
76
77
78
	/**
79
	 * Sets the new label of the attribute item.
80
	 *
81
	 * @param string $label Type label of the attribute item
82
	 * @return \Aimeos\MShop\Cms\Item\Iface Cms item for chaining method calls
83
	 */
84
	public function setLabel( ?string $label ) : \Aimeos\MShop\Cms\Item\Iface
85
	{
86
		return $this->set( 'cms.label', (string) $label );
87
	}
88
89
90
	/**
91
	 * Returns the status of the cms item.
92
	 *
93
	 * @return int Status of the cms item
94
	 */
95
	public function getStatus() : int
96
	{
97
		return $this->get( 'cms.status', 1 );
98
	}
99
100
101
	/**
102
	 * Sets the status of the cms item.
103
	 *
104
	 * @param int $status true/false for enabled/disabled
105
	 * @return \Aimeos\MShop\Cms\Item\Iface Cms item for chaining method calls
106
	 */
107
	public function setStatus( int $status ) : \Aimeos\MShop\Common\Item\Iface
108
	{
109
		return $this->set( 'cms.status', $status );
110
	}
111
112
113
	/**
114
	 * Tests if the item is available based on status, time, language and currency
115
	 *
116
	 * @return bool True if available, false if not
117
	 */
118
	public function isAvailable() : bool
119
	{
120
		return parent::isAvailable() && $this->getStatus() > 0;
121
	}
122
123
124
	/**
125
	 * Sets the item values from the given array and removes that entries from the list
126
	 *
127
	 * @param array &$list Associative list of item keys and their values
128
	 * @param bool True to set private properties too, false for public only
129
	 * @return \Aimeos\MShop\Cms\Item\Iface Cms item for chaining method calls
130
	 */
131
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
132
	{
133
		$item = parent::fromArray( $list, $private );
134
135
		foreach( $list as $key => $value )
136
		{
137
			switch( $key )
138
			{
139
				case 'cms.url': $item = $item->setUrl( $value ); break;
140
				case 'cms.label': $item = $item->setLabel( $value ); break;
141
				case 'cms.status': $item = $item->setStatus( (int) $value ); break;
142
				default: continue 2;
143
			}
144
145
			unset( $list[$key] );
146
		}
147
148
		return $item;
149
	}
150
151
152
	/**
153
	 * Returns the item values as array.
154
	 *
155
	 * @param bool True to return private properties, false for public only
156
	 * @return array Associative list of item properties and their values
157
	 */
158
	public function toArray( bool $private = false ) : array
159
	{
160
		$list = parent::toArray( true ); // include siteid, mtime, ctime, editor (private properties)
161
162
		$list['cms.url'] = $this->getUrl();
163
		$list['cms.label'] = $this->getLabel();
164
		$list['cms.status'] = $this->getStatus();
165
166
		return $list;
167
	}
168
}
169