[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Part of the Joomla Framework Registry Package 4 * 5 * @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved. 6 * @license GNU General Public License version 2 or later; see LICENSE 7 */ 8 9 namespace Joomla\Registry\Format; 10 11 use Joomla\Registry\FormatInterface; 12 13 /** 14 * PHP class format handler for Registry 15 * 16 * @since 1.0 17 */ 18 class Php implements FormatInterface 19 { 20 /** 21 * Converts an object into a php class string. 22 * - NOTE: Only one depth level is supported. 23 * 24 * @param object $object Data Source Object 25 * @param array $params Parameters used by the formatter 26 * 27 * @return string Config class formatted string 28 * 29 * @since 1.0 30 */ 31 public function objectToString($object, array $params = []) 32 { 33 // A class must be provided 34 $class = $params['class'] ?? 'Registry'; 35 36 // Build the object variables string 37 $vars = ''; 38 39 foreach (get_object_vars($object) as $k => $v) 40 { 41 $vars .= "\tpublic \$$k = " . $this->formatValue($v) . ";\n"; 42 } 43 44 $str = "<?php\n"; 45 46 // If supplied, add a namespace to the class object 47 if (isset($params['namespace']) && $params['namespace'] !== '') 48 { 49 $str .= 'namespace ' . $params['namespace'] . ";\n\n"; 50 } 51 52 $str .= "class $class {\n"; 53 $str .= $vars; 54 $str .= '}'; 55 56 // Use the closing tag if it not set to false in parameters. 57 if (!isset($params['closingtag']) || $params['closingtag'] !== false) 58 { 59 $str .= "\n?>"; 60 } 61 62 return $str; 63 } 64 65 /** 66 * Parse a PHP class formatted string and convert it into an object. 67 * 68 * @param string $data PHP Class formatted string to convert. 69 * @param array $options Options used by the formatter. 70 * 71 * @return object Data object. 72 * 73 * @since 1.0 74 */ 75 public function stringToObject($data, array $options = []) 76 { 77 return new \stdClass; 78 } 79 80 /** 81 * Format a value for the string conversion 82 * 83 * @param mixed $value The value to format 84 * 85 * @return mixed The formatted value 86 * 87 * @since 2.0.0 88 */ 89 protected function formatValue($value) 90 { 91 switch (\gettype($value)) 92 { 93 case 'string': 94 return "'" . addcslashes($value, '\\\'') . "'"; 95 96 case 'array': 97 case 'object': 98 return $this->getArrayString((array) $value); 99 100 case 'double': 101 case 'integer': 102 return $value; 103 104 case 'boolean': 105 return $value ? 'true' : 'false'; 106 107 case 'NULL': 108 return 'null'; 109 } 110 } 111 112 /** 113 * Method to get an array as an exported string. 114 * 115 * @param array $a The array to get as a string. 116 * 117 * @return string 118 * 119 * @since 1.0 120 */ 121 protected function getArrayString($a) 122 { 123 $s = 'array('; 124 $i = 0; 125 126 foreach ($a as $k => $v) 127 { 128 $s .= $i ? ', ' : ''; 129 $s .= "'" . addcslashes($k, '\\\'') . "' => "; 130 $s .= $this->formatValue($v); 131 132 $i++; 133 } 134 135 $s .= ')'; 136 137 return $s; 138 } 139 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |