/** * Import from stdClass * * @param stdClass * @param bool * @return Discount */ public function importStdClass($obj, $reset = true) { if ($reset) { $this->reset(); } $id = isset($obj->id) ? $obj->id : ''; $key = $id > 0 ? self::getKey($id) : ''; $name = isset($obj->name) ? $obj->name : ''; $as = isset($obj->as) ? $obj->as : ''; $as = $as == self::$asFlat ? self::$asFlat : self::$asPercent; $to = isset($obj->to) ? $obj->to : ''; if (!in_array($to, array(self::$toSpecified, self::$toItems, self::$toShipments))) { $to = self::$toItems; } $value = isset($obj->value) ? $obj->value : 0; $isCompound = isset($obj->is_compound) ? $obj->is_compound : false; $isProportional = isset($obj->is_proportional) ? $obj->is_proportional : false; $isPreTax = isset($obj->is_pre_tax) ? $obj->is_pre_tax : false; $toItems = isset($obj->items) ? $obj->items : array(); $toShipments = isset($obj->shipments) ? $obj->shipments : array(); $shipments = array(); $items = array(); if (count($toItems) > 0) { foreach ($toItems as $key) { $items[] = $key; } } if (count($toShipments) > 0) { foreach ($toShipments as $key) { $shipments[] = $key; } } $preConditionObj = isset($obj->pre_conditions) ? $obj->pre_conditions : new stdClass(); $targetConditionObj = isset($obj->target_conditions) ? $obj->target_conditions : new stdClass(); $preCondition = null; if ($preConditionObj instanceof stdClass) { $preCondition = new DiscountConditionCompare(); $preCondition->importStdClass($preConditionObj); } else { if (is_array($preConditionObj)) { $preCondition = new DiscountConditionCompare(); $preCondition->importJson(json_encode($preConditionObj)); } } $targetCondition = null; if ($targetConditionObj instanceof stdClass) { $targetCondition = new DiscountConditionCompare(); $targetCondition->importStdClass($targetConditionObj); } else { if (is_array($targetConditionObj)) { $targetCondition = new DiscountConditionCompare(); $targetCondition->importJson(json_encode($targetConditionObj)); } } $couponCode = isset($obj->coupon_code) ? $obj->coupon_code : ''; $isAuto = isset($obj->is_auto) ? $obj->is_auto : false; $isStopper = (bool) isset($obj->is_stopper) ? $obj->is_stopper : false; $priority = isset($obj->priority) ? $obj->priority : 0; $startDatetime = ''; //TODO $endDatetime = ''; //TODO $maxQty = isset($obj->max_qty) ? $obj->max_qty : 0; $maxAmount = isset($obj->max_amount) ? $obj->max_amount : 0; $isMaxPerItem = isset($obj->is_max_per_item) ? $obj->is_max_per_item : false; $this->_id = $id; $this->_name = $name; $this->_as = $as; $this->_to = $to; $this->_value = $value; $this->_maxQty = $maxQty; $this->_maxAmount = $maxAmount; $this->_isCompound = $isCompound; $this->_isProportional = $isProportional; $this->_isMaxPerItem = $isMaxPerItem; $this->_isPreTax = $isPreTax; $this->_isAuto = $isAuto; $this->_isStopper = $isStopper; $this->_priority = $priority; $this->_startDatetime = $startDatetime; $this->_endDatetime = $endDatetime; $this->_couponCode = $couponCode; $this->_items = $items; $this->_shipments = $shipments; $this->_targetConditionCompare = $preCondition; $this->_preConditionCompare = $targetCondition; return $this; }
/** * Import object from stdClass * * @param stdClass * @param bool * @return DiscountConditionCompare */ public function importStdClass($obj, $reset = true) { if ($reset) { $this->reset(); } $op = isset($obj->op) ? $obj->op : ''; $isNot = (bool) isset($obj->is_not) ? $obj->is_not : false; $left = null; $right = null; $leftData = isset($obj->left) ? $obj->left : null; $rightData = isset($obj->right) ? $obj->right : null; $conditions = isset($obj->conditions) ? $obj->conditions : array(); if (is_array($leftData) || $leftData instanceof stdClass) { if (isset($leftData['op']) || isset($leftData->op)) { //we have DiscountConditionCompare data if ($leftData instanceof stdClass) { $left = new DiscountConditionCompare(); $left->importStdClass($leftData); } else { if (is_array($leftData)) { $left = new DiscountConditionCompare(); $left->importJson(json_encode($leftData)); } } } else { //we have DiscountCondition data if ($leftData instanceof stdClass) { $left = new DiscountCondition(); $left->importStdClass($leftData); } else { if (is_array($leftData)) { $left = new DiscountCondition(); $left->importJson(json_encode($leftData)); } } } } if (is_array($rightData) || $rightData instanceof stdClass) { if (isset($rightData['op']) || isset($rightData->op)) { //we have DiscountConditionCompare data if ($rightData instanceof stdClass) { $right = new DiscountConditionCompare(); $right->importStdClass($rightData); } else { if (is_array($rightData)) { $right = new DiscountConditionCompare(); $right->importJson(json_encode($rightData)); } } } else { //we have DiscountCondition data if ($rightData instanceof stdClass) { $right = new DiscountCondition(); $right->importStdClass($rightData); } else { if (is_array($rightData)) { $right = new DiscountCondition(); $right->importJson(json_encode($rightData)); } } } } if ((is_array($conditions) || $conditions instanceof stdClass) && count($conditions) > 0) { foreach ($conditions as $data) { if (isset($data->op) || is_array($data) && isset($data['op'])) { //we have DiscountConditionCompare data $compare = new DiscountConditionCompare(); if ($data instanceof stdClass) { $compare->importStdClass($data); $this->addCondition($compare); } else { if (is_array($data)) { $compare->importJson(json_encode($data)); $this->addCondition($compare); } } } else { //we have DiscountCondition data $condition = new DiscountCondition(); if ($data instanceof stdClass) { $condition->importStdClass($data); $this->addCondition($condition); } else { if (is_array($data)) { $condition->importJson(json_encode($data)); $this->addCondition($condition); } } } } } $this->setOp($op)->setIsNot($isNot)->setLeftCondition($left)->setRightCondition($right)->setConditions($conditions); return $this; }