Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Json.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Config/Writer/FileAbstract.php';
26 
30 #require_once 'Zend/Config/Json.php';
31 
39 {
45  protected $_prettyPrint = false;
46 
52  public function prettyPrint()
53  {
54  return $this->_prettyPrint;
55  }
56 
63  public function setPrettyPrint($flag)
64  {
65  $this->_prettyPrint = (bool) $flag;
66  return $this;
67  }
68 
75  public function render()
76  {
77  $data = $this->_config->toArray();
78  $sectionName = $this->_config->getSectionName();
79  $extends = $this->_config->getExtends();
80 
81  if (is_string($sectionName)) {
82  $data = array($sectionName => $data);
83  }
84 
85  foreach ($extends as $section => $parentSection) {
86  $data[$section][Zend_Config_Json::EXTENDS_NAME] = $parentSection;
87  }
88 
89  // Ensure that each "extends" section actually exists
90  foreach ($data as $section => $sectionData) {
91  if (is_array($sectionData) && isset($sectionData[Zend_Config_Json::EXTENDS_NAME])) {
92  $sectionExtends = $sectionData[Zend_Config_Json::EXTENDS_NAME];
93  if (!isset($data[$sectionExtends])) {
94  // Remove "extends" declaration if section does not exist
95  unset($data[$section][Zend_Config_Json::EXTENDS_NAME]);
96  }
97  }
98  }
99 
100  $out = Zend_Json::encode($data);
101  if ($this->prettyPrint()) {
102  $out = Zend_Json::prettyPrint($out);
103  }
104  return $out;
105  }
106 }
setPrettyPrint($flag)
Definition: Json.php:63
static prettyPrint($json, $options=array())
Definition: Json.php:379
const EXTENDS_NAME
Definition: Json.php:45
static encode($valueToEncode, $cycleCheck=false, $options=array())
Definition: Json.php:130