mirror of
https://github.com/flextype/flextype.git
synced 2025-08-13 08:34:19 +02:00
@@ -18,7 +18,6 @@
|
||||
"require": {
|
||||
"php": ">=7.1.3",
|
||||
"doctrine/cache": "1.8.0",
|
||||
"symfony/yaml": "4.2.5",
|
||||
"thunderer/shortcode": "0.7.1",
|
||||
"flextype-components/arr" : "1.2.5",
|
||||
"flextype-components/assets" : "1.0.3",
|
||||
|
@@ -1,116 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Flextype
|
||||
*
|
||||
* @author Sergey Romanenko <hello@romanenko.digital>
|
||||
* @link http://romanenko.digital
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flextype;
|
||||
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Symfony\Component\Yaml\Exception\DumpException;
|
||||
use Symfony\Component\Yaml\Exception\ParseException;
|
||||
|
||||
class YamlParser {
|
||||
|
||||
/**
|
||||
* Inline
|
||||
*
|
||||
* The level where you switch to inline YAML
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public static $inline = 5;
|
||||
|
||||
/**
|
||||
* Ident
|
||||
*
|
||||
* The amount of spaces to use for indentation of nested nodes
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public static $indent = 2;
|
||||
|
||||
/**
|
||||
* Native
|
||||
*
|
||||
* Use native parser or symfony
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public static $native = true;
|
||||
|
||||
/**
|
||||
* Flag
|
||||
*
|
||||
* A bit field of PARSE_* constants to customize the YAML parser behavior
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public static $flag = Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK|Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
|
||||
|
||||
/**
|
||||
* Dumps a PHP value to a YAML string.
|
||||
*
|
||||
* The dump method, when supplied with an array, will do its best
|
||||
* to convert the array into friendly YAML.
|
||||
*
|
||||
* @param mixed $input The PHP value
|
||||
* @param int $inline The level where you switch to inline YAML
|
||||
* @param int $indent The amount of spaces to use for indentation of nested nodes
|
||||
* @param int $flags A bit field of DUMP_* constants to customize the dumped YAML string
|
||||
*
|
||||
* @return string A YAML string representing the original PHP value
|
||||
*/
|
||||
public static function encode($input, int $inline = 5, int $indent = 2, int $flags = Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK|Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE) : string
|
||||
{
|
||||
try {
|
||||
return Yaml::dump(
|
||||
$input,
|
||||
$inline ? $inline : YamlParser::$inline,
|
||||
$indent ? $indent : YamlParser::$indent,
|
||||
$flags ? $flags : YamlParser::$flag
|
||||
);
|
||||
} catch (DumpException $e) {
|
||||
throw new \RuntimeException('Encoding YAML failed: ' . $e->getMessage(), 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses YAML into a PHP value.
|
||||
*
|
||||
* $array = YamlParser::decode($yaml_file_content);
|
||||
*
|
||||
* @param string $input A string containing YAML
|
||||
* @return mixed The YAML converted to a PHP value
|
||||
*
|
||||
* @throws ParseException If the YAML is not valid
|
||||
*/
|
||||
public static function decode(string $input)
|
||||
{
|
||||
// Try native PECL YAML PHP extension first if available.
|
||||
if (YamlParser::$native && function_exists('yaml_parse')) {
|
||||
|
||||
// Safely decode YAML.
|
||||
$saved = @ini_get('yaml.decode_php');
|
||||
@ini_set('yaml.decode_php', 0);
|
||||
$decoded = @yaml_parse($input);
|
||||
@ini_set('yaml.decode_php', $saved);
|
||||
|
||||
if ($decoded !== false) {
|
||||
return (array) $decoded;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return (array) Yaml::parse($input);
|
||||
} catch (ParseException $e) {
|
||||
throw new \RuntimeException('Decoding YAML failed: ' . $e->getMessage(), 0, $e);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user