[ 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 use Symfony\Component\Yaml\Dumper as SymfonyYamlDumper; 13 use Symfony\Component\Yaml\Parser as SymfonyYamlParser; 14 use Symfony\Component\Yaml\Yaml as SymfonyYaml; 15 16 /** 17 * YAML format handler for Registry. 18 * 19 * @since 1.0 20 */ 21 class Yaml implements FormatInterface 22 { 23 /** 24 * The YAML parser class. 25 * 26 * @var SymfonyYamlParser 27 * @since 1.0 28 */ 29 private $parser; 30 31 /** 32 * The YAML dumper class. 33 * 34 * @var SymfonyYamlDumper 35 * @since 1.0 36 */ 37 private $dumper; 38 39 /** 40 * Construct to set up the parser and dumper 41 * 42 * @since 1.0 43 */ 44 public function __construct() 45 { 46 if (!class_exists(SymfonyYaml::class)) 47 { 48 throw new \RuntimeException( 49 \sprintf( 50 'The "%s" class could not be found, make sure you have installed the "symfony/yaml" package.', 51 SymfonyYaml::class 52 ) 53 ); 54 } 55 56 $this->parser = new SymfonyYamlParser; 57 $this->dumper = new SymfonyYamlDumper; 58 } 59 60 /** 61 * Converts an object into a YAML formatted string. 62 * We use json_* to convert the passed object to an array. 63 * 64 * @param object $object Data source object. 65 * @param array $options Options used by the formatter. 66 * 67 * @return string YAML formatted string. 68 * 69 * @since 1.0 70 */ 71 public function objectToString($object, array $options = []) 72 { 73 $array = json_decode(json_encode($object), true); 74 75 return $this->dumper->dump($array, 2, 0); 76 } 77 78 /** 79 * Parse a YAML formatted string and convert it into an object. 80 * We use the json_* methods to convert the parsed YAML array to an object. 81 * 82 * @param string $data YAML formatted string to convert. 83 * @param array $options Options used by the formatter. 84 * 85 * @return object Data object. 86 * 87 * @since 1.0 88 */ 89 public function stringToObject($data, array $options = []) 90 { 91 $array = $this->parser->parse(trim($data)); 92 93 return (object) json_decode(json_encode($array)); 94 } 95 }
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 |