Read YAML from a string

@param string $data The YAML data
@return array The parsed data
340739
public function readString(string $data)
    {
        $interceptor = new ErrorInterceptor(function ($data) {
            return yaml_parse($data);
        });

        $interceptor->registerError(E_WARNING, "yaml_parse");
        
        $result = $interceptor->execute($data);

        foreach ($interceptor->getInterceptedErrors() as $error)
            throw new IOException("Failed to read YAML data", $error->getCode(), $error);

        return $result;
    }
920008
private function readData($data)
    {
        $this->line('Processing...');
        $this->line('');

        $reader = Reader::createFromString($data);

        // Apply any filters.
        if (array_has($this->config, 'filter')) {
            $reader = $this->config['filter']($reader);
        }

        if (!array_has($this->config, 'no_header', false)) {
            $reader->setHeaderOffset(0);
        }

        $result = [];
        $this->progress_bar = $this->output->createProgressBar(1);

        foreach ($reader as $row) {
            if (count($row = $this->processRow($row))) {
                $result[] = $row;
            }
            $this->progress_bar->advance();
        }

        // Apply any filters.
        if (array_has($this->config, 'change_read_data')) {
            $result = $this->config['change_read_data']($result);
        }

        $this->line('     ');
        $this->line('     ');

        return $result;
    }
1046282
public function decodeData($data)
    {
        $output = array();
        while (strlen($data) > 0) {
            preg_match('/(\w+)(\[(\d+)\])?=/', $data, $matches);
            $key = $matches[1];
            $data = substr($data, strlen($matches[0]));

            if (isset($matches[3])) {
                $value = substr($data, 0, $matches[3]);
            } else {
                $next = strpos($data, '&');
                $value = $next === false ? $data : substr($data, 0, $next);
            }

            $data = substr($data, strlen($value) + 1);
            $output[$key] = $value;
        }

        return $output;
    }
164507
protected function _parseStructureParams($data)
    {
        $params = array();

        if (is_null($data)) {
            return $params;
        }

        while (($name = $data->next()) !== false) {
            $params[Horde_String::lower($name)] = $data->next();
        }

        $cp = new Horde_Mime_Headers_ContentParam('Unused', $params);

        return $cp->params;
    }
384207
public function decode($data)
    {
        if ($this->extensionLoaded()) {
            $oData = $data;
            $data = @((\strlen($data) > 0) ? \gzuncompress($data) : '');
            if ($data === false) {
                // Try this fallback
                $tries = 1;
                while ($tries < 10 && ($data === false || \strlen($data) < (\strlen($oData) - $tries - 1))) {
                    $data = @(\gzinflate(\substr($oData, $tries)));
                    $tries++;
                }

                if ($data === false) {
                    // let's try if the checksum is CRC32
                    $fh = fopen('php://temp', 'w+b');
                    \fwrite($fh, "\x1f\x8b\x08\x00\x00\x00\x00\x00" . $oData);
                    \stream_filter_append($fh, 'zlib.inflate', \STREAM_FILTER_READ, ['window' => 30]);
                    \fseek($fh, 0);
                    $data = \stream_get_contents($fh);
                    \fclose($fh);
                }

                if (!$data) {
                    throw new FlateException(
                        'Error while decompressing stream.',
                        FlateException::DECOMPRESS_ERROR
                    );
                }
            }
        } else {
            throw new FlateException(
                'To handle FlateDecode filter, enable zlib support in PHP.',
                FlateException::NO_ZLIB
            );
        }

        return $data;
    }
94518
protected function message_read($data)
    {
        $cmd = Utility::ParseCommand(ord($data[0]));
        $message_type = $cmd['message_type'];
        $flags = $cmd['flags'];
        Debug::Log(Debug::DEBUG, "message_read(): message_type=" . Message::$name[$message_type] . ", flags={$flags}");
        Debug::Log(Debug::DEBUG, 'message_read(): Dump', $data);
        $pos = 1;
        $remaining_length = Utility::DecodeLength($data, $pos);
        $message_object = $this->getMessageObject($message_type);
        $message_object->decode($data, $remaining_length);
        return $message_object;
    }
320206
public function parse(string $string)
    {
        try {
            if (defined('\Symfony\Component\Yaml\Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE')) {
                return $this->parser->parse(
                    $string,
                    Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE | Yaml::PARSE_OBJECT_FOR_MAP
                );
            } else {
                $data = $this->parser->parse($string, true, false, false);

                return $this->fixHashMaps($data);
            }
        } catch (\Throwable $e) {
            throw new ParseException("Failed to parse as YAML", 0, $e);
        }
    }
558541
protected function parse($file)
    {
        $data = json_decode(file_get_contents($file), true) ?: Yaml::parse(file_get_contents($file));

        return $data;
    }
977828
private static function decode($data) {
		if((is_array($data) && isset($data[0])) || (isset($data[0]) && !is_string($data[0]))) {
			return self::parseArray($data);
		} else if (is_null($data) || is_scalar($data)) {
			return $data;
		} else {
			return self::decodeNewObject($data);
		}
	}
1319944
private function parse(string $data): array
    {
        $registers = [
            'a'   => [],
            'd'   => false,
            'di'  => 0,
            'def' => [0 => '', 1 => '']
        ];

        for ($i = 0; $i < strlen($data); $i++) {
            if ($data[$i] === '@') {
                $registers['d'] = true;
            } elseif ($registers['d'] === true) {
                $this->parseCharacter($registers, $data[$i]);
            }
        }

        return $registers['a'];
    }
733586
protected function getData($data)
    {
        $method = "load" . strtoupper($this->mode);

        libxml_use_internal_errors(true);
        $this->dom->$method($data);
        $this->errors = libxml_get_errors();
        libxml_clear_errors();

        return $data;
    }
1194675
public function parseData(array $data)
    {
        $newData = [];

        foreach ($data as $key => $value) {
            if (is_array($value)) {
                $value = $this->parseData($value);
            }

            // If the data key contains a dot, we will replace it with another character
            // sequence so it doesn't interfere with dot processing when working with
            // array based validation rules and array_dot later in the validations.
            if (Str::contains($key, '.')) {
                $newData[str_replace('.', '->', $key)] = $value;
            } else {
                $newData[$key] = $value;
            }
        }

        return $newData;
    }
1288348
public static function parse($data)
    {
        $xml = null;
        if ($xml = simplexml_load_string($data)) {
            $xml = (array)$xml;
        }
        return $xml;
    }
649332
protected function readString($data, $path)
    {
        $path = (string)$path;

        if (!$path) {
            return null;
        }

        if ($path[0] != '@') {
            return $path; // static string
        }

        $path = substr($path, 1);

        // Use the property accessor so extract the value from the data. If the path does not exist in the
        // data then don't return a error but just null.

        try {
            return $this->convert($this->accessor->getValue($data, (string)$path));
        } catch (ExceptionInterface $e) {
            return null;
        }
    }
698378
protected function extractAndSetData($data)
    {
        // Attempt to load as Json first, as it's easier to recognise a failure on
        $data_parsed = json_decode($data, true);

        // If there's an error, maybe it's array data? We do this second because parse_str is super-uncaring.
        if (json_last_error() !== JSON_ERROR_NONE) {
            parse_str($data, $data_parsed);
        }

        if (is_array($data_parsed)) {
            $this->setAll($data_parsed);
        }
    }
324835
public function stringToObject($data, array $options=[])
  {
    $data = trim($data);
    if ( substr($data, 0, 1) !== '{' && substr($data, -1, 1) !== '}' ){
      return Format::getInstance('Yaml')->stringToObject($data, $options);
    }
    
    $decoded = json_decode($data);
    
    if ( $decoded === null ){
      throw new \RuntimeException(sprintf('Error decoding JSON data: %s', json_last_error_msg()));
    }
    
    return (object)$decoded;
  }
405455
public function parse($data) {
    $info = [];

    if (preg_match_all('
      @^\s*                           # Start at the beginning of a line, ignoring leading whitespace
      ((?:
        [^=;\[\]]|                    # Key names cannot contain equal signs, semi-colons or square brackets,
        \[[^\[\]]*\]                  # unless they are balanced and not nested
      )+?)
      \s*=\s*                         # Key/value pairs are separated by equal signs (ignoring white-space)
      (?:
        ("(?:[^"]|(?<=\\\\)")*")|     # Double-quoted string, which may contain slash-escaped quotes/slashes
        (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
        ([^\r\n]*?)                   # Non-quoted string
      )\s*$                           # Stop at the next end of a line, ignoring trailing whitespace
      @msx', $data, $matches, PREG_SET_ORDER)) {
      foreach ($matches as $match) {
        // Fetch the key and value string.
        $i = 0;
        foreach (['key', 'value1', 'value2', 'value3'] as $var) {
          $$var = isset($match[++$i]) ? $match[$i] : '';
        }

        if (isset($key) && isset($value1) && isset($value2) && isset($value3)) {
          $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;

          // Parse array syntax.
          $keys = preg_split('/\]?\[/', rtrim($key, ']'));
          $last = array_pop($keys);
          $parent = &$info;

          // Create nested arrays.
          foreach ($keys as $key) {
            if ($key == '') {
              $key = count($parent);
            }
            if (!isset($parent[$key]) || !is_array($parent[$key])) {
              $parent[$key] = [];
            }
            $parent = &$parent[$key];
          }

          // Handle PHP constants.
          if (preg_match('/^\w+$/i', $value) && defined($value)) {
            $value = constant($value);
          }

          // Insert actual value.
          if ($last == '') {
            $last = count($parent);
          }
          $parent[$last] = $value;
        }
      }
    }

    return $info;
  }