/** * Converts an object into a YAML formatted string. * We use json_* to convert the passed object to an array. * * @param object $object Data source object. * @param array $options Options used by the formatter. * * @return string YAML formatted string. * * @since 2.0 */ public static function objectToString($object, $options = array()) { $array = json_decode(json_encode($object), true); $inline = RegistryHelper::getValue($options, 'inline', 2); $indent = RegistryHelper::getValue($options, 'indent', 0); return static::getDumper()->dump($array, $inline, $indent); }
/** * 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 * * @throws \InvalidArgumentException * @return string Config class formatted string */ public static function objectToString($object, $params = array()) { $header = RegistryHelper::getValue($params, 'header'); // Build the object variables string $vars = ""; foreach (get_object_vars($object) 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 it not set to false in parameters. if (RegistryHelper::getValue($params, 'closingtag', false)) { $str .= "\n?>"; } return $str; }
/** * 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 stringToObject($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); } }
/** * 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())); }
/** * Method to recursively bind data to a parent object. * * @param object $parent The parent object on which to attach the data values. * @param mixed $data An array or object of data to bind to the parent object. * @param boolean $recursive True to support recursive bindData. * @param boolean $allowNull Allow null * * @return void */ protected function bindData($parent, $data, $recursive = true, $allowNull = true) { // Ensure the input data is an array. if (is_object($data)) { $data = get_object_vars($data); } else { $data = (array) $data; } foreach ($data as $k => $v) { if (!$allowNull && !($v !== null && $v !== '')) { continue; } if ($recursive && (is_array($v) && RegistryHelper::isAssociativeArray($v) || is_object($v))) { if (!isset($parent->{$k})) { $parent->{$k} = new \stdClass(); } $this->bindData($parent->{$k}, $v); } else { $parent->{$k} = $v; } } }