/** * Converts an object into an INI formatted string * - Unfortunately, there is no way to have ini values nested further than two * levels deep. Therefore we will only go through the first two levels of * the object. * * @param object $struct Data source object. * @param array $options Options used by the formatter. * * @return string INI formatted string. */ public static function structToString($struct, array $options = array()) { $local = array(); $global = array(); // Iterate over the object to set the properties. foreach ($struct as $key => $value) { // If the value is an object then we need to put it in a local section. if (is_array($value)) { if (!RegistryHelper::isAssociativeArray($value)) { continue; } // Add the section line. $local[] = ''; $local[] = '[' . $key . ']'; // Add the properties for this section. foreach ($value as $k => $v) { if (is_numeric($k)) { continue; } $local[] = $k . '=' . static::getValueAsINI($v); } } else { // Not in a section so add the property to the global array. $global[] = $key . '=' . static::getValueAsINI($value); } } return implode("\n", array_merge($global, $local)); }
/** * Converts an object into a php class string. * - NOTE: Only one depth level is supported. * * @param object $struct Data Source Object * @param array $options Parameters used by the formatter * * @return string */ public static function structToString($struct, array $options = array()) { $header = RegistryHelper::getValue($options, 'header'); // Build the object variables string $vars = ""; foreach ($struct as $k => $v) { if (is_scalar($v)) { $vars .= sprintf("\t'%s' => '%s',\n", $k, addcslashes($v, '\\\'')); } elseif (is_array($v) || is_object($v)) { $vars .= sprintf("\t'%s' => %s,\n", $k, static::getArrayString((array) $v)); } } $str = "<?php\n"; if ($header) { $str .= $header . "\n"; } $str .= "\nreturn array(\n"; $str .= $vars; $str .= ");\n"; // Use the closing tag if set to true in parameters. if (RegistryHelper::getValue($options, 'closingtag', false)) { $str .= "\n?>"; } return $str; }
/** * Converts an object into a YAML formatted string. * We use json_* to convert the passed object to an array. * * @param object $struct Data source object. * @param array $options Options used by the formatter. * * @return string YAML formatted string. * * @since 2.0 */ public static function structToString($struct, array $options = array()) { $array = json_decode(json_encode($struct), true); $inline = RegistryHelper::getValue($options, 'inline', 2); $indent = RegistryHelper::getValue($options, 'indent', 0); return static::getDumper()->dump($array, $inline, $indent); }
/** * Parse a JSON formatted string and convert it into an object. * * @param string $data JSON formatted string to convert. * @param array $options Options used by the formatter. * * @return object Data object. */ public static function stringToStruct($data, array $options = array()) { $assoc = RegistryHelper::getValue($options, 'assoc', false); $depth = RegistryHelper::getValue($options, 'depth', 512); $option = RegistryHelper::getValue($options, 'options', 0); if (PHP_VERSION >= 5.4) { return json_decode(trim($data), $assoc, $depth, $option); } else { return json_decode(trim($data), $assoc, $depth); } }
public function testDumpObjectValue() { $data = new StubDumpable(new StubDumpable()); $dumped = RegistryHelper::dumpObjectValues($data); $this->assertEquals('foo', $dumped['foo']); $this->assertEquals('bar', $dumped['bar']); $this->assertNull($dumped['data']['self']); $this->assertEquals(RegistryHelper::dumpObjectValues(new StubDumpable()), $dumped['data']['new']); $this->assertEquals(array('sakura', 'rose'), $dumped['data']['flower']); $this->assertEquals(array('wind' => 'walker'), $dumped['iterator']); }
/** * Method to test toObject(). * * @return void * * @covers Windwalker\Registry\Registry::toObject */ public function testToObject() { $registry = new Registry($this->getTestData()); $this->assertEquals($registry->toObject(), RegistryHelper::toObject($this->getTestData())); }