Skip to content

Commit

Permalink
Add support for YAML.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Macnamara committed Apr 21, 2014
1 parent 58b5962 commit 719fd9e
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/Nathanmac/Parser/Parser.php
Expand Up @@ -2,6 +2,7 @@

namespace Nathanmac\Parser;

use Symfony\Component\Yaml\Yaml;
use Config, Exception;

class ParserException extends Exception {}
Expand All @@ -13,7 +14,7 @@ public function payload($format = false) {
if (isset(Config::get('parser::supported_formats')[$format])) {
return $this->{Config::get('parser::supported_formats')[$format]}($this->_payload());
}
throw new ParserException('invalid_or_unsupported_format');
throw new ParserException('Invalid Or Unsupported Format');
}
return $this->{$this->_format()}($this->_payload());
}
Expand All @@ -33,7 +34,7 @@ public function xml($string) {
if ($string) {
$xml = @simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA);
if(!$xml)
throw new ParserException('failed_to_parse_XML');
throw new ParserException('Failed To Parse XML');
return json_decode(json_encode((array) $xml), 1); // Work arround to accept xml input
}
return array();
Expand All @@ -43,7 +44,7 @@ public function json($string) {
if ($string) {
$json = json_decode(trim($string), true);
if (!$json)
throw new ParserException('failed_to_parse_JSON');
throw new ParserException('Failed To Parse JSON');
return $json;
}
return array();
Expand All @@ -53,7 +54,7 @@ public function serialize($string) {
if ($string) {
$serial = @unserialize(trim($string));
if (!$serial)
throw new ParserException('failed_to_parse_serialized_data');
throw new ParserException('Failed To Parse Serialized Data');
return $serial;
}
return array();
Expand All @@ -63,9 +64,20 @@ public function querystr($string) {
if ($string) {
@parse_str(trim($string), $querystr);
if (!$querystr)
throw new ParserException('failed_to_parse_querystring');
throw new ParserException('Failed To Parse Query String');
return $querystr;
}
return array();
}

public function yaml($string) {
if ($string) {
try {
return Yaml::parse($string);
} catch (Exception $ex) {
throw new ParserException('Failed To Parse YAML');
}
}
return array();
}
}

0 comments on commit 719fd9e

Please sign in to comment.