Skip to content

Commit

Permalink
Merge pull request #1 from estahn/add_exception_mapping
Browse files Browse the repository at this point in the history
Map "parser error" to exception "DataTypeMapperException"
  • Loading branch information
estahn committed Feb 2, 2016
2 parents 06abd3c + 0bb1a4d commit 1677f32
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/JsonQueryWrapper/DataTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/

namespace JsonQueryWrapper;
use JsonQueryWrapper\Exception\DataTypeMapperException;

/**
* Map data returned from jq to PHP data types
Expand All @@ -41,6 +42,7 @@ class DataTypeMapper
*
* @param string $value
* @return mixed
* @throws DataTypeMapperException
*/
public function map($value)
{
Expand All @@ -62,6 +64,11 @@ public function map($value)
return $matches[1];
}

// Map parser error
if (preg_match('/^parse error: (.*)$/', $value, $matches)) {
throw new DataTypeMapperException($matches[1]);
}

return $value;
}
}
34 changes: 34 additions & 0 deletions src/JsonQueryWrapper/Exception/DataTypeMapperException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* JSON Query Wrapper
*
* (The MIT license)
* Copyright (c) 2016 Enrico Stahn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package JsonQueryWrapper
* @link http://github.com/estahn/json-query-wrapper for the canonical source repository
*/

namespace JsonQueryWrapper\Exception;

class DataTypeMapperException extends \Exception
{
}
4 changes: 2 additions & 2 deletions src/JsonQueryWrapper/JsonQueryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class JsonQueryFactory
*
* @return JsonQuery
*/
static public function create()
public static function create()
{
return new JsonQuery(new ProcessBuilder(), new DataTypeMapper());
}
Expand All @@ -51,7 +51,7 @@ static public function create()
* @param string $filenameOrText A path to a json file or json text
* @return JsonQuery
*/
static public function createWith($filenameOrText)
public static function createWith($filenameOrText)
{
$provider = file_exists($filenameOrText) ? new File($filenameOrText) : new Text($filenameOrText);

Expand Down
8 changes: 8 additions & 0 deletions tests/JsonQueryWrapper/DataTypeMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public function testInteger()
$this->assertEquals(33, $value);
}

/**
* @expectedException \JsonQueryWrapper\Exception\DataTypeMapperException
*/
public function testParserError()
{
$this->mapper->map('parse error: Expected another key-value pair at line 7, column 1');
}

public function testJson()
{
$this->markTestIncomplete('Need to decide whether to convert JSON data');
Expand Down

0 comments on commit 1677f32

Please sign in to comment.