Passed
Push — master ( 2cc80f...ef78f2 )
by Aimeos
05:36
created

Standard::setItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
4
 * @copyright Aimeos (aimeos.org), 2022-2024
5
 * @package MShop
6
 * @subpackage Basket
7
 */
8
9
10
namespace Aimeos\MShop\Basket\Item;
11
12
13
/**
14
 * Default implementation of the basket item
15
 *
16
 * @package MShop
17
 * @subpackage Basket
18
 */
19
class Standard
20
	extends \Aimeos\MShop\Common\Item\Base
21
	implements \Aimeos\MShop\Basket\Item\Iface
22
{
23
	private ?\Aimeos\MShop\Order\Item\Iface $item;
24
25
26
	/**
27
	 * Initializes the object
28
	 *
29
	 * @param array $values Associative list of key/value pairs with basket properties
30
	 * @param \Aimeos\MShop\Order\Item\Iface|null $item Basket object
31
	 */
32
	public function __construct( array $values = [], ?\Aimeos\MShop\Order\Item\Iface $item = null )
33
	{
34
		parent::__construct( 'basket.', $values );
35
		$this->item = $item;
36
	}
37
38
39
	/**
40
	 * Sets the new ID of the item.
41
	 *
42
	 * @param string|null $id ID of the item
43
	 * @return \Aimeos\MShop\Common\Item\Iface Item for chaining method calls
44
	 */
45
	public function setId( ?string $id ) : \Aimeos\MShop\Common\Item\Iface
46
	{
47
		return parent::setId( $id )->setModified();
48
	}
49
50
51
	/**
52
	 * Returns the basket object.
53
	 *
54
	 * @return \Aimeos\MShop\Order\Item\Iface|null $basket Basket object
55
	 */
56
	public function getItem() : ?\Aimeos\MShop\Order\Item\Iface
57
	{
58
		return $this->item;
59
	}
60
61
62
	/**
63
	 * Sets the basket object.
64
	 *
65
	 * @param \Aimeos\MShop\Order\Item\Iface $basket Basket object
66
	 * @return \Aimeos\MShop\Basket\Item\Iface Basket item for chaining method calls
67
	 */
68
	public function setItem( \Aimeos\MShop\Order\Item\Iface $basket ) : \Aimeos\MShop\Basket\Item\Iface
69
	{
70
		$this->item = $basket;
71
		return $this->setModified();
72
	}
73
74
75
	/**
76
	 * Returns the ID of the customer who owns the basket.
77
	 *
78
	 * @return string Unique ID of the customer
79
	 */
80
	public function getCustomerId() : string
81
	{
82
		return (string) $this->get( 'basket.customerid', '' );
83
	}
84
85
86
	/**
87
	 * Sets the ID of the customer who owned the basket.
88
	 *
89
	 * @param string $customerid Unique ID of the customer
90
	 * @return \Aimeos\MShop\Basket\Item\Iface Basket item for chaining method calls
91
	 */
92
	public function setCustomerId( ?string $value ) : \Aimeos\MShop\Basket\Item\Iface
93
	{
94
		return $this->set( 'basket.customerid', (string) $value );
95
	}
96
97
98
	/**
99
	 * Returns the name of the basket.
100
	 *
101
	 * @return string Name for the basket
102
	 */
103
	public function getName() : string
104
	{
105
		return (string) $this->get( 'basket.name', '' );
106
	}
107
108
109
	/**
110
	 * Sets the name of the basket.
111
	 *
112
	 * @param string $value Name for the basket
113
	 * @return \Aimeos\MShop\Basket\Item\Iface Basket item for chaining method calls
114
	 */
115
	public function setName( ?string $value ) : \Aimeos\MShop\Basket\Item\Iface
116
	{
117
		return $this->set( 'basket.name', (string) $value );
118
	}
119
120
121
	/*
122
	 * Sets the item values from the given array and removes that entries from the list
123
	 *
124
	 * @param array &$list Associative list of item keys and their values
125
	 * @param bool True to set private properties too, false for public only
126
	 * @return \Aimeos\MShop\Basket\Item\Iface Order status item for chaining method calls
127
	 */
128
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
129
	{
130
		$item = parent::fromArray( $list, $private );
131
132
		foreach( $list as $key => $value )
133
		{
134
			switch( $key )
135
			{
136
				case 'basket.customerid': $item->setCustomerId( $value ); break;
137
				case 'basket.name': $item->setName( $value ); break;
138
				default: continue 2;
139
			}
140
141
			unset( $list[$key] );
142
		}
143
144
		return $item;
145
	}
146
147
148
149
	/**
150
	 * Returns the item values as array.
151
	 *
152
	 * @param bool True to return private properties, false for public only
153
	 * @return array Associative list of item properties and their values
154
	 */
155
	public function toArray( bool $private = false ) : array
156
	{
157
		$list = parent::toArray( $private );
158
159
		$list['basket.name'] = $this->getName();
160
		$list['basket.customerid'] = $this->getCustomerId();
161
162
		return $list;
163
	}
164
165
}
166