本文整理汇总了PHP中Magento\Framework\Json\Helper\Data::jsonEncode方法的典型用法代码示例。如果您正苦于以下问题:PHP Data::jsonEncode方法的具体用法?PHP Data::jsonEncode怎么用?PHP Data::jsonEncode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\Framework\Json\Helper\Data
的用法示例。
在下文中一共展示了Data::jsonEncode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testJsonEncode
public function testJsonEncode()
{
$expected = '"valueToEncode"';
$valueToEncode = 'valueToEncode';
$this->jsonEncoderMock->expects($this->once())->method('encode')->willReturn($expected);
$this->assertEquals($expected, $this->helper->jsonEncode($valueToEncode));
}
示例2: testJsonEncodeDecode
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));
}
示例3: _redirect
/**
* Don't actually redirect if we've got AJAX request - return redirect URL instead.
*
* @param string $path
* @param array $arguments
* @return $this|\Magento\Backend\App\AbstractAction
*/
protected function _redirect($path, $arguments = [])
{
if ($this->getRequest()->isXmlHttpRequest()) {
$this->getResponse()->representJson($this->jsonHelper->jsonEncode(['_redirect' => $this->getUrl($path, $arguments)]));
return $this;
} else {
return parent::_redirect($path, $arguments);
}
}
示例4: jsonResponse
/**
* Compile JSON response
*
* @param string $error
* @return Http
*/
protected function jsonResponse($error = '')
{
$response = $this->sidebar->getResponseData($error);
if (empty($error)) {
$resultPage = $this->resultPageFactory->create();
$block = $resultPage->getLayout()->getBlock('minicart.content')->toHtml();
$response['content'] = $block;
}
return $this->getResponse()->representJson($this->jsonHelper->jsonEncode($response));
}
示例5: executeInternal
/**
* Search for attributes by part of attribute's label in admin store
*
* @return void
*/
public function executeInternal()
{
$this->storeManager->setCurrentStore(\Magento\Store\Model\Store::ADMIN_CODE);
$collection = $this->collectionFactory->create();
$collection->addFieldToFilter('main_table.attribute_id', $this->getRequest()->getParam('attributes'));
$attributes = [];
foreach ($collection->getItems() as $attribute) {
$attributes[] = ['id' => $attribute->getId(), 'label' => $attribute->getFrontendLabel(), 'code' => $attribute->getAttributeCode(), 'options' => $attribute->getSource()->getAllOptions(false)];
}
$this->getResponse()->representJson($this->jsonHelper->jsonEncode($attributes));
}
示例6: execute
/**
* @return void
*/
public function execute()
{
$severity = $this->getRequest()->getParam('severity');
if ($severity) {
$this->messageCollection->setSeverity($severity);
}
$result = [];
foreach ($this->messageCollection->getItems() as $item) {
$result[] = ['severity' => $item->getSeverity(), 'text' => $item->getText()];
}
if (empty($result)) {
$result[] = ['severity' => (string) \Magento\Framework\Notification\MessageInterface::SEVERITY_NOTICE, 'text' => 'You have viewed and resolved all recent system notices. ' . 'Please refresh the web page to clear the notice alert.'];
}
$this->getResponse()->representJson($this->jsonHelper->jsonEncode($result));
}
示例7: execute
/**
* Check Captcha On Checkout as Guest Page
*
* @param \Magento\Framework\Event\Observer $observer
* @return $this
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$formId = 'guest_checkout';
$captchaModel = $this->_helper->getCaptcha($formId);
$checkoutMethod = $this->_typeOnepage->getQuote()->getCheckoutMethod();
if ($checkoutMethod == \Magento\Checkout\Model\Type\Onepage::METHOD_GUEST) {
if ($captchaModel->isRequired()) {
$controller = $observer->getControllerAction();
if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) {
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$result = ['error' => 1, 'message' => __('Incorrect CAPTCHA')];
$controller->getResponse()->representJson($this->jsonHelper->jsonEncode($result));
}
}
}
return $this;
}
示例8: getJsonConfigurableAttributes
/**
* Get JSON string that contains attribute code and value
*
* @param array $options
* @return string
*/
protected function getJsonConfigurableAttributes(array $options = [])
{
$result = [];
foreach ($options as $option) {
$result[$option['attribute_code']] = $option['value'];
}
return $this->jsonHelper->jsonEncode($result);
}
示例9: getCountriesWithStatesRequired
/**
* Returns the list of countries, for which region is required
*
* @param boolean $asJson
* @return array
*/
public function getCountriesWithStatesRequired($asJson = false)
{
$value = trim($this->scopeConfig->getValue(self::XML_PATH_STATES_REQUIRED, ScopeInterface::SCOPE_STORE));
$countryList = preg_split('/\\,/', $value, 0, PREG_SPLIT_NO_EMPTY);
if ($asJson) {
return $this->jsonHelper->jsonEncode($countryList);
}
return $countryList;
}
示例10: getAllRatesByProductClassJson
/**
* Get all tax rates JSON for all product tax classes.
*
* @return string
*/
public function getAllRatesByProductClassJson()
{
$result = [];
foreach ($this->productTaxClassSource->getAllOptions() as $productTaxClass) {
$taxClassId = $productTaxClass['value'];
$taxRate = $this->calculationService->getDefaultCalculatedRate($taxClassId, $this->currentCustomer->getCustomerId(), $this->getStore()->getId());
$result["value_{$taxClassId}"] = $taxRate;
}
return $this->jsonHelper->jsonEncode($result);
}
示例11: getPriceFormat
/**
* Get prices javascript format json
*
* @param null|int|string|Store $store
* @return string
*/
public function getPriceFormat($store = null)
{
$this->_localeResolver->emulate($store);
$priceFormat = $this->_localeFormat->getPriceFormat();
$this->_localeResolver->revert();
if ($store) {
$priceFormat['pattern'] = $this->_storeManager->getStore($store)->getCurrentCurrency()->getOutputFormat();
}
return $this->jsonHelper->jsonEncode($priceFormat);
}
示例12: getUsedDefaultForPaths
/**
* Get paths of where current template is used as default
*
* @param bool $asJSON
* @return string
*/
public function getUsedDefaultForPaths($asJSON = true)
{
/** @var $template \Magento\Email\Model\BackendTemplate */
$template = $this->getEmailTemplate();
$paths = $template->getSystemConfigPathsWhereUsedAsDefault();
$pathsParts = $this->_getSystemConfigPathsParts($paths);
if ($asJSON) {
return $this->jsonHelper->jsonEncode($pathsParts);
}
return $pathsParts;
}
示例13: execute
/**
* Validate api user.
*/
public function execute()
{
$params = $this->getRequest()->getParams();
$apiUsername = $params['api_username'];
//@codingStandardsIgnoreStart
$apiPassword = base64_decode($params['api_password']);
//@codingStandardsIgnoreEnd
//validate api, check against account info.
if ($this->data->isEnabled()) {
$client = $this->data->getWebsiteApiClient();
$result = $client->validate($apiUsername, $apiPassword);
$resonseData['success'] = true;
//validation failed
if (!$result) {
$resonseData['success'] = false;
$resonseData['message'] = 'Authorization has been denied for this request.';
}
$this->getResponse()->representJson($this->jsonHelper->jsonEncode($resonseData));
}
}
示例14: build
/**
* Duplicating downloadable product data
*
* @param \Magento\Catalog\Model\Product $product
* @param \Magento\Catalog\Model\Product $duplicate
* @return void
*/
public function build(\Magento\Catalog\Model\Product $product, \Magento\Catalog\Model\Product $duplicate)
{
if ($product->getTypeId() !== \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) {
//do nothing if not downloadable
return;
}
$data = [];
/** @var \Magento\Downloadable\Model\Product\Type $type */
$type = $product->getTypeInstance();
foreach ($type->getLinks($product) as $link) {
/* @var \Magento\Downloadable\Model\Link $link */
$linkData = $link->getData();
$data['link'][] = ['is_delete' => false, 'link_id' => null, 'title' => $linkData['title'], 'is_shareable' => $linkData['is_shareable'], 'sample' => ['type' => $linkData['sample_type'], 'url' => $linkData['sample_url'], 'file' => $this->jsonHelper->jsonEncode([['file' => $linkData['sample_file'], 'name' => $linkData['sample_file'], 'size' => 0, 'status' => null]])], 'file' => $this->jsonHelper->jsonEncode([['file' => $linkData['link_file'], 'name' => $linkData['link_file'], 'size' => 0, 'status' => null]]), 'type' => $linkData['link_type'], 'link_url' => $linkData['link_url'], 'sort_order' => $linkData['sort_order'], 'number_of_downloads' => $linkData['number_of_downloads'], 'price' => $linkData['price']];
}
/** @var \Magento\Downloadable\Model\Sample $sample */
foreach ($type->getSamples($product) as $sample) {
$sampleData = $sample->getData();
$data['sample'][] = ['is_delete' => false, 'sample_id' => null, 'title' => $sampleData['title'], 'type' => $sampleData['sample_type'], 'file' => $this->jsonHelper->jsonEncode([['file' => $sampleData['sample_file'], 'name' => $sampleData['sample_file'], 'size' => 0, 'status' => null]]), 'sample_url' => $sampleData['sample_url'], 'sort_order' => $sampleData['sort_order']];
}
$duplicate->setDownloadableData($data);
}
示例15: getCookiesMessages
/**
* Return messages stored in cookies
*
* @return array
*/
protected function getCookiesMessages()
{
try {
$messages = $this->jsonHelper->jsonDecode($this->cookieManager->getCookie(self::MESSAGES_COOKIES_NAME, $this->jsonHelper->jsonEncode([])));
if (!is_array($messages)) {
$messages = [];
}
} catch (\Zend_Json_Exception $e) {
$messages = [];
}
return $messages;
}