[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/yaml/Symfony/Component/Yaml/ -> Yaml.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\Yaml;
  13  
  14  use Symfony\Component\Yaml\Exception\ParseException;
  15  
  16  /**
  17   * Yaml offers convenience methods to load and dump YAML.
  18   *
  19   * @author Fabien Potencier <fabien@symfony.com>
  20   */
  21  class Yaml
  22  {
  23      /**
  24       * Parses YAML into a PHP value.
  25       *
  26       *  Usage:
  27       *  <code>
  28       *   $array = Yaml::parse('config.yml');
  29       *   print_r($array);
  30       *  </code>
  31       *
  32       * As this method accepts both plain strings and file names as an input,
  33       * you must validate the input before calling this method. Passing a file
  34       * as an input is a deprecated feature and will be removed in 3.0.
  35       *
  36       * @param string $input                  Path to a YAML file or a string containing YAML
  37       * @param bool   $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise
  38       * @param bool   $objectSupport          True if object support is enabled, false otherwise
  39       *
  40       * @return mixed The YAML converted to a PHP value
  41       *
  42       * @throws ParseException If the YAML is not valid
  43       */
  44      public static function parse($input, $exceptionOnInvalidType = false, $objectSupport = false)
  45      {
  46          // if input is a file, process it
  47          $file = '';
  48          if (strpos($input, "\n") === false && is_file($input)) {
  49              if (false === is_readable($input)) {
  50                  throw new ParseException(sprintf('Unable to parse "%s" as the file is not readable.', $input));
  51              }
  52  
  53              $file = $input;
  54              $input = file_get_contents($file);
  55          }
  56  
  57          $yaml = new Parser();
  58  
  59          try {
  60              return $yaml->parse($input, $exceptionOnInvalidType, $objectSupport);
  61          } catch (ParseException $e) {
  62              if ($file) {
  63                  $e->setParsedFile($file);
  64              }
  65  
  66              throw $e;
  67          }
  68      }
  69  
  70      /**
  71       * Dumps a PHP array to a YAML string.
  72       *
  73       * The dump method, when supplied with an array, will do its best
  74       * to convert the array into friendly YAML.
  75       *
  76       * @param array $array                  PHP array
  77       * @param int   $inline                 The level where you switch to inline YAML
  78       * @param int   $indent                 The amount of spaces to use for indentation of nested nodes.
  79       * @param bool  $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
  80       * @param bool  $objectSupport          true if object support is enabled, false otherwise
  81       *
  82       * @return string A YAML string representing the original PHP array
  83       */
  84      public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false)
  85      {
  86          if ($indent < 1) {
  87              throw new \InvalidArgumentException('The indentation must be greater than zero.');
  88          }
  89  
  90          $yaml = new Dumper();
  91          $yaml->setIndentation($indent);
  92  
  93          return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $objectSupport);
  94      }
  95  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1