Json.class.php
3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* Util Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.Util.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@Util.com so we can send you a copy immediately.
*
* @category Util
* @package Util_Common_Config
* @copyright Copyright (c) 2005-2009 Util Technologies USA Inc. (http://www.Util.com)
* @license http://framework.Util.com/license/new-bsd New BSD License
* @version $Id: Xml.php 18862 2009-11-05 18:25:20Z beberlei $
*/
/**
* @see Util_Common_Config_Writer
*/
// require_once 'Util/Config/Writer/FileAbstract.php';
/**
* @see Util_Common_Config_Json
*/
// require_once 'Util/Config/Json.class.php';
/**
* @category Util
* @package Util_Common_Config
* @copyright Copyright (c) 2005-2009 Util Technologies USA Inc. (http://www.Util.com)
* @license http://framework.Util.com/license/new-bsd New BSD License
*/
class Util_Common_Config_Writer_Json extends Util_Common_Config_Writer_FileAbstract
{
/**
* If we need to pretty-print JSON data
*
* @var boolean
*/
protected $_prettyPrint = false;
/**
* Get prettyPrint flag
*
* @return the prettyPrint flag
*/
public function prettyPrint()
{
return $this->_prettyPrint;
}
/**
* Set prettyPrint flag
*
* @param bool $prettyPrint PrettyPrint flag
* @return Util_Common_Config_Writer_Json
*/
public function setPrettyPrint($flag)
{
$this->_prettyPrint = (bool) $flag;
return $this;
}
/**
* Render a Util_Common_Config into a JSON config string.
*
* @since 1.10
* @return string
*/
public function render()
{
$data = $this->_config->toArray();
$sectionName = $this->_config->getSectionName();
$extends = $this->_config->getExtends();
if (is_string($sectionName)) {
$data = array($sectionName => $data);
}
foreach ($extends as $section => $parentSection) {
$data[$section][Util_Common_Config_Json::EXTENDS_NAME] = $parentSection;
}
// Ensure that each "extends" section actually exists
foreach ($data as $section => $sectionData) {
if (is_array($sectionData) && isset($sectionData[Util_Common_Config_Json::EXTENDS_NAME])) {
$sectionExtends = $sectionData[Util_Common_Config_Json::EXTENDS_NAME];
if (!isset($data[$sectionExtends])) {
// Remove "extends" declaration if section does not exist
unset($data[$section][Util_Common_Config_Json::EXTENDS_NAME]);
}
}
}
$out = Util_Json::encode($data);
if ($this->prettyPrint()) {
$out = Util_Json::prettyPrint($out);
}
return $out;
}
}