/** * Action for AJAX request * * @return void */ public function execute() { $bookmark = $this->bookmarkFactory->create(); $jsonData = $this->_request->getParam('data'); if (!$jsonData) { throw new \InvalidArgumentException('Invalid parameter "data"'); } $data = $this->jsonDecoder->decode($jsonData); $action = key($data); switch ($action) { case self::ACTIVE_IDENTIFIER: $this->updateCurrentBookmark($data[$action]); break; case self::CURRENT_IDENTIFIER: $this->updateBookmark($bookmark, $action, $bookmark->getTitle(), $jsonData); break; case self::VIEWS_IDENTIFIER: foreach ($data[$action] as $identifier => $data) { $this->updateBookmark($bookmark, $identifier, isset($data['label']) ? $data['label'] : '', $jsonData); $this->updateCurrentBookmark($identifier); } break; default: throw new \LogicException(__('Unsupported bookmark action.')); } }
public function testAfterGetOptions() { $resultJson = '[]'; $this->jsonDecoder->expects($this->once())->method('decode')->with('[]')->willReturn([]); $expected = ['configurable_attribute_1' => [['mediaType' => 'type', 'videoUrl' => 'url', 'isBase' => true]]]; $this->jsonEncoder->expects($this->any())->method('encode')->with($expected)->willReturn(json_encode($expected)); $blockMock = $this->getMockBuilder('\\Magento\\ProductVideo\\Block\\Product\\View\\Gallery')->disableOriginalConstructor()->getMock(); $result = $this->plugin->afterGetOptionsMediaGalleryDataJson($blockMock, $resultJson); $this->assertEquals(json_encode($expected), $result); }
/** * calculate currency value * get currency rate from webservice and multiply it by given value * * @param float $base * @param float $exchange * @param float $value * @return float $result * @throws \Exception */ public function calculate($base, $exchange, $value) { //prepare url for request $url = $this->_buildUrl($base, $exchange); //get response $response = $this->_getCurrencyRate($url); //get valid json $decoded = $this->_jsonDecoder->decode($response); if (!isset($decoded['rates']) || !isset($decoded['rates'][$exchange])) { throw new \Exception('Cannot convert value. No valid response!'); } //get rate for given currency $rate = $decoded['rates'][$exchange]; //multiply by given value and return return $rate * $value; }
/** * Get config * * @return array */ public function getConfig() { $config = $this->getData(self::CONFIG); if ($config) { return $this->jsonDecoder->decode($config); } return []; }
/** * Retrieve component paths and configs from composer.json files * * @return \Traversable */ public function getComponentsInfo() { $components = [\Magento\Framework\Component\ComponentRegistrar::THEME, \Magento\Framework\Component\ComponentRegistrar::MODULE]; foreach ($components as $component) { $paths = $this->registrar->getPaths($component); foreach ($paths as $name => $path) { if (!strstr($name, 'Swissup')) { continue; } $filePath = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, "{$path}/composer.json"); try { $config = $this->filesystemDriver->fileGetContents($filePath); $config = $this->jsonDecoder->decode($config); $config['path'] = $path; (yield [$config['name'], $config]); } catch (\Exception $e) { // skip module } } } }
/** * @param \Magento\Catalog\Block\Product\View\Gallery $subject * @param string $result * @return string * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterGetOptionsMediaGalleryDataJson(\Magento\Catalog\Block\Product\View\Gallery $subject, $result) { $result = $this->jsonDecoder->decode($result); if ($this->getProduct()->getTypeId() == 'configurable') { /** @var Configurable $productType */ $productType = $this->getProduct()->getTypeInstance(); $products = $productType->getUsedProducts($this->getProduct()); $attributes = $productType->getConfigurableAttributesAsArray($this->getProduct()); /** @var \Magento\Catalog\Model\Product $product */ foreach ($attributes as $attribute) { foreach ($products as $product) { $attributeValue = $product->getData($attribute['attribute_code']); if ($attributeValue) { $key = $attribute['attribute_code'] . '_' . $attributeValue; $result[$key] = $this->getProductGallery($product); } } } } return $this->jsonEncoder->encode($result); }
/** * Get config * * @return array */ public function getConfig() { return $this->jsonDecoder->decode($this->getData(self::CONFIG)); }
/** * Decodes the given $encodedValue string which is * encoded in the JSON format * * @param string $encodedValue * @return mixed */ public function jsonDecode($encodedValue) { return $this->jsonDecoder->decode($encodedValue); }