1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102:
<?php
namespace Apptus\ESales\Connector;
use Apptus\Util\String\ListCodec;
use Apptus\Util\String\MapCodec;
class Order {
private $lines = array ();
public function serialize() {
$mapCodec = new MapCodec('|');
$lineCodec = new ListCodec(',');
$lineStrings = array ();
foreach ($this->lines as $line) {
$annot = $mapCodec->encodeMap($line->annotations);
$lineStrings[] = $lineCodec->encodeList(array ($line->key, $line->keyType, $annot));
}
return (new ListCodec(';'))->encodeList($lineStrings);
}
public function addProduct($productKey) {
if (!AttributeValidation::forProducts()->validItemKey($productKey)) {
throw new \InvalidArgumentException('Invalid product key: ' . $productKey);
}
$orderLine = new OrderLine($productKey, 'PRODUCT');
$this->lines[] = $orderLine;
return $orderLine;
}
public function addVariant() {
$nargs = func_num_args();
if ($nargs === 1) {
$variantKey = func_get_arg(0);
if (!AttributeValidation::forVariants()->validItemKey($variantKey)) {
throw new \InvalidArgumentException('Invalid variant key: ' . $variantKey);
}
$orderLine = new OrderLine($variantKey, 'VARIANT');
$this->lines[] = $orderLine;
return $orderLine;
} elseif ($nargs === 2) {
$productKey = func_get_arg(0);
$variantKey = func_get_arg(1);
if (!AttributeValidation::forProducts()->validItemKey($productKey)) {
throw new \InvalidArgumentException('Invalid product key: ' . $productKey);
}
if (!AttributeValidation::forVariants()->validItemKey($variantKey)) {
throw new \InvalidArgumentException('Invalid variant key: ' . $variantKey);
}
$orderLine = new OrderLine($productKey . ',' . $variantKey, 'VARIANT_WITH_PRODUCT');
$this->lines[] = $orderLine;
return $orderLine;
} else {
throw new \InvalidArgumentException('Invalid number of arguments. Expected a variant key or a product key and a variant key.');
}
}
public function addTicket($ticket) {
$orderLine = new OrderLine($ticket, 'TICKET');
$this->lines[] = $orderLine;
return $orderLine;
}
}