public function testJsonEncodeDecode() { $data = ['one' => 1, 'two' => 'two']; $jsonData = '{"one":1,"two":"two"}'; $this->assertEquals($jsonData, $this->_helper->jsonEncode($data)); $this->assertEquals($data, $this->_helper->jsonDecode($jsonData)); }
/** * Set data for response of frontend saveOrder action * * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function addAdditionalFieldsToResponseFrontend(\Magento\Framework\Event\Observer $observer) { /* @var $order \Magento\Sales\Model\Order */ $order = $this->_coreRegistry->registry('directpost_order'); if ($order && $order->getId()) { $payment = $order->getPayment(); if ($payment && $payment->getMethod() == $this->_payment->getCode()) { /** @var \Magento\Checkout\Controller\Action $controller */ $controller = $observer->getEvent()->getData('controller_action'); $request = $controller->getRequest(); $response = $controller->getResponse(); $result = $this->_coreData->jsonDecode($response->getBody('default')); if (empty($result['error'])) { $payment = $order->getPayment(); //if success, then set order to session and add new fields $this->_session->addCheckoutOrderIncrementId($order->getIncrementId()); $this->_session->setLastOrderIncrementId($order->getIncrementId()); $requestToAuthorizenet = $payment->getMethodInstance()->generateRequestFromOrder($order); $requestToAuthorizenet->setControllerActionName($request->getControllerName()); $requestToAuthorizenet->setIsSecure((string) $this->_storeManager->getStore()->isCurrentlySecure()); $result['directpost'] = array('fields' => $requestToAuthorizenet->getData()); $response->clearHeader('Location'); $response->representJson($this->_coreData->jsonEncode($result)); } } } return $this; }
/** * Parse Request body into array of params. * * @param string $encodedBody Posted content from request. * @return array|null Return NULL if content is invalid. * @throws \InvalidArgumentException * @throws \Magento\Webapi\Exception If decoding error was encountered. */ public function deserialize($encodedBody) { if (!is_string($encodedBody)) { throw new \InvalidArgumentException(sprintf('"%s" data type is invalid. String is expected.', gettype($encodedBody))); } try { $decodedBody = $this->_helper->jsonDecode($encodedBody); } catch (\Zend_Json_Exception $e) { if ($this->_appState->getMode() !== State::MODE_DEVELOPER) { throw new \Magento\Webapi\Exception(__('Decoding error.')); } else { throw new \Magento\Webapi\Exception(__('Decoding error: %1%2%3%4', PHP_EOL, $e->getMessage(), PHP_EOL, $e->getTraceAsString())); } } return $decodedBody; }
/** * @param \Magento\Framework\Object $object * @return $this|void */ public function beforeSave($object) { $attrCode = $this->getAttribute()->getAttributeCode(); $value = $object->getData($attrCode); if (!is_array($value) || !isset($value['images'])) { return; } if (!is_array($value['images']) && strlen($value['images']) > 0) { $value['images'] = $this->_coreData->jsonDecode($value['images']); } if (!is_array($value['images'])) { $value['images'] = array(); } $clearImages = array(); $newImages = array(); $existImages = array(); if ($object->getIsDuplicate() != true) { foreach ($value['images'] as &$image) { if (!empty($image['removed'])) { $clearImages[] = $image['file']; } elseif (empty($image['value_id'])) { $newFile = $this->_moveImageFromTmp($image['file']); $image['new_file'] = $newFile; $newImages[$image['file']] = $image; $this->_renamedImages[$image['file']] = $newFile; $image['file'] = $newFile; } else { $existImages[$image['file']] = $image; } } } else { // For duplicating we need copy original images. $duplicate = array(); foreach ($value['images'] as &$image) { if (empty($image['value_id'])) { continue; } $duplicate[$image['value_id']] = $this->_copyImage($image['file']); $image['new_file'] = $duplicate[$image['value_id']]; $newImages[$image['file']] = $image; } $value['duplicate'] = $duplicate; } foreach ($object->getMediaAttributes() as $mediaAttribute) { $mediaAttrCode = $mediaAttribute->getAttributeCode(); $attrData = $object->getData($mediaAttrCode); if (in_array($attrData, $clearImages)) { $object->setData($mediaAttrCode, 'no_selection'); } if (in_array($attrData, array_keys($newImages))) { $object->setData($mediaAttrCode, $newImages[$attrData]['new_file']); $object->setData($mediaAttrCode . '_label', $newImages[$attrData]['label']); } if (in_array($attrData, array_keys($existImages))) { $object->setData($mediaAttrCode . '_label', $existImages[$attrData]['label']); } } $object->setData($attrCode, $value); return $this; }
/** * Product variations attributes validation * * @param Product $parentProduct * @param array $products * @param RequestInterface $request * @return array */ protected function _validateProductVariations(Product $parentProduct, array $products, RequestInterface $request) { $this->eventManager->dispatch('catalog_product_validate_variations_before', array('product' => $parentProduct, 'variations' => $products)); $validationResult = array(); foreach ($products as $productData) { $product = $this->productFactory->create(); $product->setData('_edit_mode', true); $storeId = $request->getParam('store'); if ($storeId) { $product->setStoreId($storeId); } $product->setAttributeSetId($parentProduct->getAttributeSetId()); $product->addData($productData); $product->setCollectExceptionMessages(true); $configurableAttribute = $this->coreHelper->jsonDecode($productData['configurable_attribute']); $configurableAttribute = implode('-', $configurableAttribute); $errorAttributes = $product->validate(); if (is_array($errorAttributes)) { foreach ($errorAttributes as $attributeCode => $result) { if (is_string($result)) { $key = 'variations-matrix-' . $configurableAttribute . '-' . $attributeCode; $validationResult[$key] = $result; } } } } return $validationResult; }
/** * Set model data from info field * * @return $this */ protected function _afterLoad() { parent::_afterLoad(); $info = $this->_coreData->jsonDecode($this->getInfo()); if (is_array($info)) { foreach ($info as $key => $value) { $this->setData($key, $value); } } return $this; }
/** * Get next bunch of validated rows. * * @return array|null */ public function getNextBunch() { if (null === $this->_iterator) { $this->_iterator = $this->getIterator(); $this->_iterator->rewind(); } if ($this->_iterator->valid()) { $dataRow = $this->_iterator->current(); $dataRow = $this->_jsonHelper->jsonDecode($dataRow[0]); $this->_iterator->next(); } else { $this->_iterator = null; $dataRow = null; } return $dataRow; }
/** * Set data for response of frontend saveOrder action * * @param EventObserver $observer * @return $this */ public function setResponseAfterSaveOrder(EventObserver $observer) { /* @var $order \Magento\Sales\Model\Order */ $order = $this->_coreRegistry->registry('hss_order'); if ($order && $order->getId()) { $payment = $order->getPayment(); if ($payment && in_array($payment->getMethod(), $this->_paypalHss->getHssMethods())) { /* @var $controller \Magento\Framework\App\Action\Action */ $controller = $observer->getEvent()->getData('controller_action'); $result = $this->_coreData->jsonDecode($controller->getResponse()->getBody('default')); if (empty($result['error'])) { $this->_view->loadLayout('checkout_onepage_review', true, true, false); $html = $this->_view->getLayout()->getBlock('paypal.iframe')->toHtml(); $result['update_section'] = array('name' => 'paypaliframe', 'html' => $html); $result['redirect'] = false; $result['success'] = false; $controller->getResponse()->clearHeader('Location'); $controller->getResponse()->representJson($this->_coreData->jsonEncode($result)); } } } return $this; }