Php.php
2.52 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* Part of the Joomla Framework Registry Package
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Registry\Format;
use Joomla\Registry\AbstractRegistryFormat;
/**
* PHP class format handler for Registry
*
* @since 1.0
*/
class Php extends AbstractRegistryFormat
{
/**
* Converts an object into a php class string.
* - NOTE: Only one depth level is supported.
*
* @param object $object Data Source Object
* @param array $params Parameters used by the formatter
*
* @return string Config class formatted string
*
* @since 1.0
*/
public function objectToString($object, $params = array())
{
// A class must be provided
$class = !empty($params['class']) ? $params['class'] : 'Registry';
// Build the object variables string
$vars = '';
foreach (get_object_vars($object) as $k => $v)
{
if (is_scalar($v))
{
$vars .= "\tpublic $" . $k . " = '" . addcslashes($v, '\\\'') . "';\n";
}
elseif (is_array($v) || is_object($v))
{
$vars .= "\tpublic $" . $k . ' = ' . $this->getArrayString((array) $v) . ";\n";
}
}
$str = "<?php\n";
// If supplied, add a namespace to the class object
if (isset($params['namespace']) && $params['namespace'] !== '')
{
$str .= 'namespace ' . $params['namespace'] . ";\n\n";
}
$str .= 'class ' . $class . " {\n";
$str .= $vars;
$str .= '}';
// Use the closing tag if it not set to false in parameters.
if (!isset($params['closingtag']) || $params['closingtag'] !== false)
{
$str .= "\n?>";
}
return $str;
}
/**
* Parse a PHP class formatted string and convert it into an object.
*
* @param string $data PHP Class formatted string to convert.
* @param array $options Options used by the formatter.
*
* @return object Data object.
*
* @since 1.0
*/
public function stringToObject($data, array $options = array())
{
return new \stdClass;
}
/**
* Method to get an array as an exported string.
*
* @param array $a The array to get as a string.
*
* @return string
*
* @since 1.0
*/
protected function getArrayString($a)
{
$s = 'array(';
$i = 0;
foreach ($a as $k => $v)
{
$s .= $i ? ', ' : '';
$s .= '"' . $k . '" => ';
if (is_array($v) || is_object($v))
{
$s .= $this->getArrayString((array) $v);
}
else
{
$s .= '"' . addslashes($v) . '"';
}
$i++;
}
$s .= ')';
return $s;
}
}