Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ParserInterface implementations #120

Merged
merged 7 commits into from
Mar 30, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ protected function loadFromFile($path, ParserInterface $parser = null)
$parser = $this->getParser($extension);

// Try to load file
$this->data = array_replace_recursive($this->data, (array) $parser->parseFile($path));
$this->data = array_replace_recursive($this->data, $parser->parseFile($path));

// Clean parser
$parser = null;
} else {
// Try to load file using specified parser
$this->data = array_replace_recursive($this->data, (array) $parser->parseFile($path));
$this->data = array_replace_recursive($this->data, $parser->parseFile($path));
}
}
}
Expand All @@ -116,7 +116,7 @@ protected function loadFromString($configuration, ParserInterface $parser)
$this->data = [];

// Try to parse string
$this->data = array_replace_recursive($this->data, (array) $parser->parseString($configuration));
$this->data = array_replace_recursive($this->data, $parser->parseString($configuration));
}

/**
Expand Down
10 changes: 7 additions & 3 deletions src/Parser/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class Json implements ParserInterface
public function parseFile($filename)
{
$data = json_decode(file_get_contents($filename), true);
return $this->parse($data, $filename);
$parsed = $this->parse($data, $filename);
return $parsed === null ? [] : $parsed;
}

/**
Expand All @@ -37,14 +38,16 @@ public function parseFile($filename)
public function parseString($config)
{
$data = json_decode($config, true);
return $this->parse($data);
$parsed = $this->parse($data);

return $parsed === null ? [] : $parsed;
}

/**
* Completes parsing of JSON data
*
* @param array $data
* @param strring $filename
* @param string $filename
*
* @throws ParseException If there is an error parsing the JSON data
*/
Expand All @@ -61,6 +64,7 @@ protected function parse($data = null, $filename = null)
'type' => json_last_error(),
'file' => $filename,
];

throw new ParseException($error);
}

Expand Down
13 changes: 8 additions & 5 deletions src/Parser/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public function parseFile($filename)
}

// Complete parsing
return $this->parse($data, $filename);
$parsed = $this->parse($data, $filename);
return $parsed === null ? [] : $parsed;
}

/**
Expand Down Expand Up @@ -71,16 +72,18 @@ public function parseString($config)
}

// Complete parsing
return $this->parse($data);
$parsed = $this->parse($data);
return $parsed === null ? [] : $parsed;
}

/**
* Completes parsing of PHP data
*
* @param array $data
* @param strring $filename
* @param array $data
* @param string $filename
*
* @throws ParseException If there is an error parsing the PHP data
* @return array|null
* @throws UnsupportedFormatException
*/
protected function parse($data = null, $filename = null)
{
Expand Down
13 changes: 9 additions & 4 deletions src/Parser/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public function parseFile($filename)
{
libxml_use_internal_errors(true);
$data = simplexml_load_file($filename, null, LIBXML_NOERROR);
return $this->parse($data, $filename);
$parsed = $this->parse($data, $filename);

return $parsed === null ? [] : $parsed;
}

/**
Expand All @@ -39,15 +41,18 @@ public function parseString($config)
{
libxml_use_internal_errors(true);
$data = simplexml_load_string($config, null, LIBXML_NOERROR);
return $this->parse($data);
$parsed = $this->parse($data);

return $parsed === null ? [] : $parsed;
}

/**
* Completes parsing of XML data
*
* @param array $data
* @param strring $filename
* @param array $data
* @param string $filename
*
* @return array|null
* @throws ParseException If there is an error parsing the XML data
*/
protected function parse($data = null, $filename = null)
Expand Down
17 changes: 10 additions & 7 deletions src/Parser/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Yaml implements ParserInterface
* {@inheritDoc}
* Loads a YAML/YML file as an array
*
* @throws ParseException If If there is an error parsing the YAML file
* @throws ParseException If there is an error parsing the YAML file
*/
public function parseFile($filename)
{
Expand All @@ -37,7 +37,9 @@ public function parseFile($filename)
);
}

return $this->parse($data, $filename);
$parsed = $this->parse($data);

return $parsed === null ? [] : $parsed;
}

/**
Expand All @@ -59,18 +61,19 @@ public function parseString($config)
);
}

return $this->parse($data);
$parsed = $this->parse($data);

return $parsed === null ? [] : $parsed;
}

/**
* Completes parsing of YAML/YML data
*
* @param array $data
* @param strring $filename
* @param array $data
*
* @throws ParseException If there is an error parsing the YAML data
* @return array|null
*/
protected function parse($data = null, $filename = null)
protected function parse($data = null)
{
return $data;
}
Expand Down