|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author @jayS-de <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Commercetools\Core\Request\Carts\Command; |
|
7
|
|
|
|
|
8
|
|
|
use Commercetools\Core\Model\Cart\ExternalLineItemTotalPrice; |
|
9
|
|
|
use Commercetools\Core\Model\Common\Context; |
|
10
|
|
|
use Commercetools\Core\Model\Common\Money; |
|
11
|
|
|
use Commercetools\Core\Request\AbstractAction; |
|
12
|
|
|
use Commercetools\Core\Model\Channel\ChannelReference; |
|
13
|
|
|
use Commercetools\Core\Model\CustomField\CustomFieldObjectDraft; |
|
14
|
|
|
use Commercetools\Core\Model\TaxCategory\ExternalTaxRateDraft; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @package Commercetools\Core\Request\Carts\Command |
|
18
|
|
|
* @link https://dev.commercetools.com/http-api-projects-carts.html#add-lineitem |
|
19
|
|
|
* @method string getAction() |
|
20
|
|
|
* @method CartAddLineItemAction setAction(string $action = null) |
|
21
|
|
|
* @method string getProductId() |
|
22
|
|
|
* @method CartAddLineItemAction setProductId(string $productId = null) |
|
23
|
|
|
* @method int getVariantId() |
|
24
|
|
|
* @method CartAddLineItemAction setVariantId(int $variantId = null) |
|
25
|
|
|
* @method int getQuantity() |
|
26
|
|
|
* @method CartAddLineItemAction setQuantity(int $quantity = null) |
|
27
|
|
|
* @method ChannelReference getSupplyChannel() |
|
28
|
|
|
* @method CartAddLineItemAction setSupplyChannel(ChannelReference $supplyChannel = null) |
|
29
|
|
|
* @method ChannelReference getDistributionChannel() |
|
30
|
|
|
* @method CartAddLineItemAction setDistributionChannel(ChannelReference $distributionChannel = null) |
|
31
|
|
|
* @method CustomFieldObjectDraft getCustom() |
|
32
|
|
|
* @method CartAddLineItemAction setCustom(CustomFieldObjectDraft $custom = null) |
|
33
|
|
|
* @method ExternalTaxRateDraft getExternalTaxRate() |
|
34
|
|
|
* @method CartAddLineItemAction setExternalTaxRate(ExternalTaxRateDraft $externalTaxRate = null) |
|
35
|
|
|
* @method Money getExternalPrice() |
|
36
|
|
|
* @method CartAddLineItemAction setExternalPrice(Money $externalPrice = null) |
|
37
|
|
|
* @method ExternalLineItemTotalPrice getExternalTotalPrice() |
|
38
|
|
|
* @method CartAddLineItemAction setExternalTotalPrice(ExternalLineItemTotalPrice $externalTotalPrice = null) |
|
39
|
|
|
* @method string getSku() |
|
40
|
|
|
* @method CartAddLineItemAction setSku(string $sku = null) |
|
41
|
|
|
*/ |
|
42
|
|
|
class CartAddLineItemAction extends AbstractAction |
|
43
|
|
|
{ |
|
44
|
26 |
|
public function fieldDefinitions() |
|
45
|
|
|
{ |
|
46
|
|
|
return [ |
|
|
|
|
|
|
47
|
26 |
|
'action' => [static::TYPE => 'string'], |
|
48
|
26 |
|
'productId' => [static::TYPE => 'string'], |
|
49
|
26 |
|
'variantId' => [static::TYPE => 'int'], |
|
50
|
26 |
|
'sku' => [static::TYPE => 'string'], |
|
51
|
26 |
|
'quantity' => [static::TYPE => 'int'], |
|
52
|
26 |
|
'supplyChannel' => [static::TYPE => ChannelReference::class], |
|
53
|
26 |
|
'distributionChannel' => [static::TYPE => ChannelReference::class], |
|
54
|
26 |
|
'custom' => [static::TYPE => CustomFieldObjectDraft::class], |
|
55
|
26 |
|
'externalTaxRate' => [static::TYPE => ExternalTaxRateDraft::class], |
|
56
|
26 |
|
'externalPrice' => [static::TYPE => Money::class], |
|
57
|
26 |
|
'externalTotalPrice' => [static::TYPE => ExternalLineItemTotalPrice::class], |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $productId |
|
63
|
|
|
* @param int $variantId |
|
64
|
|
|
* @param Context|callable $context |
|
65
|
|
|
* @param int $quantity |
|
66
|
|
|
* @return CartAddLineItemAction |
|
67
|
|
|
*/ |
|
68
|
23 |
|
public static function ofProductIdVariantIdAndQuantity($productId, $variantId, $quantity, $context = null) |
|
69
|
|
|
{ |
|
70
|
23 |
|
return static::of($context)->setProductId($productId)->setVariantId($variantId)->setQuantity($quantity); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $sku |
|
75
|
|
|
* @param Context|callable $context |
|
76
|
|
|
* @param int $quantity |
|
77
|
|
|
* @return CartAddLineItemAction |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public static function ofSkuAndQuantity($sku, $quantity, $context = null) |
|
80
|
|
|
{ |
|
81
|
1 |
|
return static::of($context)->setSku($sku)->setQuantity($quantity); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param array $data |
|
86
|
|
|
* @param Context|callable $context |
|
87
|
|
|
*/ |
|
88
|
24 |
|
public function __construct(array $data = [], $context = null) |
|
89
|
|
|
{ |
|
90
|
24 |
|
parent::__construct($data, $context); |
|
91
|
24 |
|
$this->setAction('addLineItem'); |
|
92
|
24 |
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.