/** * Parses the inline php doc comments and embedded data. * * @param $subject * * @return array * @throws Exception */ private function parseEmbeddedData($subject) { $data = array(); //parse {@pattern } tags specially while (preg_match('|(?s-m)({@pattern (/.+/[imsxuADSUXJ]*)})|', $subject, $matches)) { $subject = str_replace($matches[0], '', $subject); $data['pattern'] = $matches[2]; } while (preg_match('/{@(\\w+)\\s?([^}]*)}/ms', $subject, $matches)) { $subject = str_replace($matches[0], '', $subject); if ($matches[2] == 'true' || $matches[2] == 'false') { $matches[2] = $matches[2] == 'true'; } elseif ($matches[2] == '') { $matches[2] = true; } if ($matches[1] == 'pattern') { throw new Exception('Inline pattern tag should follow {@pattern /REGEX_PATTERN_HERE/} format and can optionally include PCRE modifiers following the ending `/`'); } elseif (false !== strpos($matches[2], static::$arrayDelimiter)) { $matches[2] = explode(static::$arrayDelimiter, $matches[2]); } $data[$matches[1]] = $matches[2]; } while (preg_match(self::$embeddedDataPattern, $subject, $matches)) { $subject = str_replace($matches[0], '', $subject); $str = $matches[self::$embeddedDataIndex]; if (isset($this->restler) && self::$embeddedDataIndex > 1 && !empty($matches[1])) { $extension = $matches[1]; $formatMap = $this->restler->getFormatMap(); if (isset($formatMap[$extension])) { /** * @var \Luracast\Restler\Format\iFormat */ $format = $formatMap[$extension]; $format = new $format(); $data = $format->decode($str); } } else { // auto detect if ($str[0] == '{') { $d = json_decode($str, true); if (json_last_error() != JSON_ERROR_NONE) { throw new Exception('Error parsing embedded JSON data' . " {$str}"); } $data = $d + $data; } else { parse_str($str, $d); //clean up $d = array_filter($d); foreach ($d as $key => $val) { $kt = trim($key); if ($kt != $key) { unset($d[$key]); $key = $kt; $d[$key] = $val; } if (is_string($val)) { if ($val == 'true' || $val == 'false') { $d[$key] = $val == 'true' ? true : false; } else { $val = explode(self::$arrayDelimiter, $val); if (count($val) > 1) { $d[$key] = $val; } else { $d[$key] = preg_replace('/\\s+/msu', ' ', $d[$key]); } } } } $data = $d + $data; } } } return array($subject, $data); }
/** * Parses the inline php doc comments and embedded data. * * @param $subject * * @return array * @throws Exception */ private function parseEmbeddedData($subject) { $data = array(); while (preg_match('/{@(\\w+)\\s([^}]*)}/ms', $subject, $matches)) { $subject = str_replace($matches[0], '', $subject); if ($matches[2] == 'true' || $matches[2] == 'false') { $matches[2] = $matches[2] == 'true'; } if ($matches[1] != 'pattern' && false !== strpos($matches[2], static::$arrayDelimiter)) { $matches[2] = explode(static::$arrayDelimiter, $matches[2]); } $data[$matches[1]] = $matches[2]; } while (preg_match(self::$embeddedDataPattern, $subject, $matches)) { $subject = str_replace($matches[0], '', $subject); $str = $matches[self::$embeddedDataIndex]; if (isset($this->restler) && self::$embeddedDataIndex > 1 && !empty($matches[1])) { $extension = $matches[1]; $formatMap = $this->restler->getFormatMap(); if (isset($formatMap[$extension])) { /** * @var \Luracast\Restler\Format\iFormat */ $format = $formatMap[$extension]; $format = new $format(); $data = $format->decode($str); } } else { // auto detect if ($str[0] == '{') { $d = json_decode($str, true); if (json_last_error() != JSON_ERROR_NONE) { throw new Exception('Error parsing embedded JSON data' . " {$str}"); } $data = $d + $data; } else { parse_str($str, $d); //clean up $d = array_filter($d); foreach ($d as $key => $val) { $kt = trim($key); if ($kt != $key) { unset($d[$key]); $key = $kt; $d[$key] = $val; } if (is_string($val)) { if ($val == 'true' || $val == 'false') { $d[$key] = $val == 'true' ? true : false; } else { $val = explode(self::$arrayDelimiter, $val); if (count($val) > 1) { $d[$key] = $val; } else { $d[$key] = preg_replace('/\\s+/ms', ' ', $d[$key]); } } } } $data = $d + $data; } } } return array($subject, $data); }