Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Yaml.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Config/Writer/FileAbstract.php';
26 
30 #require_once 'Zend/Config/Yaml.php';
31 
39 {
45  protected $_yamlEncoder = array('Zend_Config_Writer_Yaml', 'encode');
46 
52  public function getYamlEncoder()
53  {
54  return $this->_yamlEncoder;
55  }
56 
63  public function setYamlEncoder($yamlEncoder)
64  {
65  if (!is_callable($yamlEncoder)) {
66  #require_once 'Zend/Config/Exception.php';
67  throw new Zend_Config_Exception('Invalid parameter to setYamlEncoder - must be callable');
68  }
69 
70  $this->_yamlEncoder = $yamlEncoder;
71  return $this;
72  }
73 
80  public function render()
81  {
82  $data = $this->_config->toArray();
83  $sectionName = $this->_config->getSectionName();
84  $extends = $this->_config->getExtends();
85 
86  if (is_string($sectionName)) {
87  $data = array($sectionName => $data);
88  }
89 
90  foreach ($extends as $section => $parentSection) {
91  $data[$section][Zend_Config_Yaml::EXTENDS_NAME] = $parentSection;
92  }
93 
94  // Ensure that each "extends" section actually exists
95  foreach ($data as $section => $sectionData) {
96  if (is_array($sectionData) && isset($sectionData[Zend_Config_Yaml::EXTENDS_NAME])) {
97  $sectionExtends = $sectionData[Zend_Config_Yaml::EXTENDS_NAME];
98  if (!isset($data[$sectionExtends])) {
99  // Remove "extends" declaration if section does not exist
100  unset($data[$section][Zend_Config_Yaml::EXTENDS_NAME]);
101  }
102  }
103  }
104 
105  return call_user_func($this->getYamlEncoder(), $data);
106  }
107 
116  public static function encode($data)
117  {
118  return self::_encodeYaml(0, $data);
119  }
120 
128  protected static function _encodeYaml($indent, $data)
129  {
130  reset($data);
131  $result = "";
132  $numeric = is_numeric(key($data));
133 
134  foreach($data as $key => $value) {
135  if(is_array($value)) {
136  $encoded = "\n".self::_encodeYaml($indent+1, $value);
137  } else {
138  $encoded = (string)$value."\n";
139  }
140  $result .= str_repeat(" ", $indent).($numeric?"- ":"$key: ").$encoded;
141  }
142  return $result;
143  }
144 }
static encode($data)
Definition: Yaml.php:116
$value
Definition: gender.phtml:16
const EXTENDS_NAME
Definition: Yaml.php:40
setYamlEncoder($yamlEncoder)
Definition: Yaml.php:63
static _encodeYaml($indent, $data)
Definition: Yaml.php:128