Skip to content

Commit 49d9d2f

Browse files
authored
Merge pull request #31 from danhunsaker/feature/encoder-updates
Updates For Encoder Changes
2 parents 23c1596 + a26475c commit 49d9d2f

File tree

9 files changed

+81
-25
lines changed

9 files changed

+81
-25
lines changed

.travis.yml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,27 @@ matrix:
1111
- php: 7.0
1212
include:
1313
- php: 5.4
14-
env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"'
14+
env: 'BSON=0 MSGPACK=0 COMPOSER_FLAGS="--prefer-stable --prefer-lowest"'
15+
16+
env:
17+
- 'BSON=0 MSGPACK=0'
18+
- 'BSON=0 MSGPACK=1'
19+
- 'BSON=mongo MSGPACK=0'
20+
- 'BSON=mongodb MSGPACK=0'
21+
- 'BSON=mongodb MSGPACK=1'
1522

1623
before_script:
24+
- sh -c "if [ $MSGPACK -eq 1 -a $(phpenv version-name) \< 7.0 ]; then wget https://github.com/msgpack/msgpack-php/archive/php5.zip -O php-msgpack.zip && unzip php-msgpack.zip && mv msgpack-php-php5 msgpack-php; fi"
25+
- sh -c "if [ $MSGPACK -eq 1 -a $(phpenv version-name) \> 6.0 ]; then wget https://github.com/msgpack/msgpack-php/archive/master.zip -O php-msgpack.zip && unzip php-msgpack.zip && mv msgpack-php-master msgpack-php; fi"
26+
- sh -c "if [ $MSGPACK -eq 1 ]; then cd msgpack-php/ && phpize && ./configure && make && make install; fi"
27+
- sh -c "if [ $MSGPACK -eq 1 ]; then echo \"extension=msgpack.so\" >> `php --ini | grep \"Loaded Configuration\" | sed -e \"s|.*:\s*||\"`; fi"
28+
- sh -c "if [ $MSGPACK -eq 0 ]; then echo \"Not Installing MSGPack Extension\"; fi"
29+
- sh -c "if [ $BSON = mongo -a $(phpenv version-name) \< 7.0 ]; then wget https://github.com/mongodb/mongo-php-driver-legacy/archive/master.zip -O php-mongo.zip && unzip php-mongo.zip; fi"
30+
- sh -c "if [ $BSON = mongo -a $(phpenv version-name) \< 7.0 ]; then cd mongo-php-driver-legacy-master/ && phpize && ./configure && make && make install; fi"
31+
- sh -c "if [ $BSON = mongo -a $(phpenv version-name) \< 7.0 ]; then echo \"extension=mongo.so\" >> `php --ini | grep \"Loaded Configuration\" | sed -e \"s|.*:\s*||\"`; fi"
32+
- sh -c "if [ $BSON = mongodb ]; then git clone --depth=50 --recursive https://github.com/mongodb/mongo-php-driver/; fi"
33+
- sh -c "if [ $BSON = mongodb ]; then cd mongo-php-driver/ && git submodule status --recursive && ./.travis.scripts/compile.sh; fi"
34+
- sh -c "if [ $BSON = 0 ]; then echo \"Not Installing Mongo or MongoDB Extension\"; fi"
1735
- travis_retry composer self-update
1836
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
1937

src/Facades/Parser.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,5 @@ class Parser extends Facade
1818
*
1919
* @return string
2020
*/
21-
protected static function getFacadeAccessor()
22-
{
23-
return 'Parser';
24-
}
21+
protected static function getFacadeAccessor() { return 'Parser'; }
2522
}

src/Formats/BSON.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,37 @@ class BSON implements FormatInterface
1919
* @param string $payload
2020
*
2121
* @throws ParserException
22-
* @return array
2322
*
23+
* @return array
2424
*/
2525
public function parse($payload)
2626
{
27-
if (function_exists('bson_decode')) {
28-
if ($payload) {
29-
$bson = bson_decode(trim($payload));
27+
if (function_exists('MongoDB\BSON\toPHP') && ! function_exists('bson_decode')) {
28+
require_once(__DIR__ . '/BSONPolyfill.php'); //@codeCoverageIgnore
29+
} elseif ( ! (function_exists('bson_decode') || function_exists('MongoDB\BSON\toPHP'))) {
30+
throw new ParserException('Failed To Parse BSON - Supporting Library Not Available'); // @codeCoverageIgnore
31+
}
32+
33+
if ($payload) {
34+
$prevHandler = set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) {
35+
throw new \Exception($errstr); // @codeCoverageIgnore
36+
});
37+
38+
try {
39+
$bson = bson_decode(trim($payload, " \t\n\r\x0b")); // Don't trim \0, as it has valid meaning in BSON
3040
if ( ! $bson) {
31-
throw new ParserException('Failed To Parse BSON');
41+
throw new \Exception('Unknown error'); // @codeCoverageIgnore
3242
}
33-
return $bson;
43+
} catch (\Exception $e) {
44+
set_error_handler($prevHandler);
45+
throw new ParserException('Failed To Parse BSON - ' . $e->getMessage());
3446
}
35-
return [];
47+
48+
set_error_handler($prevHandler);
49+
50+
return $bson;
3651
}
3752

38-
throw new ParserException('Failed To Parse BSON - Supporting Library Not Available');
53+
return [];
3954
}
4055
}

src/Formats/BSONPolyfill.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
if ( ! function_exists('bson_decode')) {
4+
function bson_decode($payload)
5+
{
6+
return MongoDB\BSON\toPHP($payload, ['root' => 'array', 'document' => 'array']);
7+
}
8+
}

src/Formats/FormatInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ interface FormatInterface
1515
* Parse Payload Data
1616
*
1717
* @param string $payload
18+
*
19+
* @throws ParserException
20+
*
1821
* @return array
1922
*/
2023
public function parse($payload);

src/Formats/MSGPack.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,30 @@ class MSGPack implements FormatInterface
1919
* @param string $payload
2020
*
2121
* @throws ParserException
22-
* @return array
2322
*
23+
* @return array
2424
*/
2525
public function parse($payload)
2626
{
2727
if (function_exists('msgpack_unpack')) {
2828
if ($payload) {
29+
$prevHandler = set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) {
30+
throw new ParserException('Failed To Parse MSGPack'); // @codeCoverageIgnore
31+
});
32+
2933
$msg = msgpack_unpack(trim($payload));
3034
if ( ! $msg) {
31-
throw new ParserException('Failed To Parse MSGPack');
35+
set_error_handler($prevHandler); // @codeCoverageIgnore
36+
throw new ParserException('Failed To Parse MSGPack'); // @codeCoverageIgnore
3237
}
38+
39+
set_error_handler($prevHandler);
40+
3341
return $msg;
3442
}
3543
return [];
3644
}
3745

38-
throw new ParserException('Failed To Parse MSGPack - Supporting Library Not Available');
46+
throw new ParserException('Failed To Parse MSGPack - Supporting Library Not Available'); // @codeCoverageIgnore
3947
}
4048
}

src/Formats/YAML.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Nathanmac\Utilities\Parser\Formats;
44

55
use Nathanmac\Utilities\Parser\Exceptions\ParserException;
6+
use Symfony\Component\Yaml\Yaml as SFYaml;
67

78
/**
89
* YAML Formatter
@@ -26,7 +27,8 @@ public function parse($payload)
2627
{
2728
if ($payload) {
2829
try {
29-
return \Symfony\Component\Yaml\Yaml::parse(trim(preg_replace('/\t+/', '', $payload)));
30+
$flags = (defined('Symfony\Component\Yaml\Yaml::PARSE_DATETIME')) ? (SFYaml::PARSE_EXCEPTION_ON_INVALID_TYPE | SFYaml::PARSE_DATETIME) : true;
31+
return SFYaml::parse(trim(preg_replace('/\t+/', '', $payload)), $flags);
3032
} catch (\Exception $ex) {
3133
throw new ParserException('Failed To Parse YAML');
3234
}

src/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public function all()
194194
/**
195195
* Autodetect the payload data type using content-type value.
196196
*
197-
* @return string Return the short format code (xml, json, ...).
197+
* @return string Return the name of the formatter class.
198198
*/
199199
public function getFormatClass($format = '')
200200
{

tests/BSONTest.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ class BSONTest extends \PHPUnit_Framework_TestCase
99
/** @test */
1010
public function parser_validates_bson_data()
1111
{
12-
if (function_exists('bson_decode')) {
13-
$expected = ['status' => 123, 'message' => 'hello world'];
14-
$payload = bson_encode($expected);
12+
$expected = ['status' => 123, 'message' => 'hello world'];
1513

14+
if (function_exists('bson_encode')) {
15+
$payload = bson_encode($expected);
16+
} elseif (function_exists('MongoDB\BSON\fromPHP')) {
17+
$payload = \MongoDB\BSON\fromPHP($expected);
18+
}
19+
20+
if (function_exists('bson_decode') || function_exists('MongoDB\BSON\toPHP')) {
1621
$parser = new Parser();
1722
$this->assertEquals($expected, $parser->bson($payload));
1823
}
@@ -21,7 +26,7 @@ public function parser_validates_bson_data()
2126
/** @test */
2227
public function parser_empty_bson_data()
2328
{
24-
if (function_exists('bson_decode')) {
29+
if (function_exists('bson_decode') || function_exists('MongoDB\BSON\toPHP')) {
2530
$parser = new Parser();
2631
$this->assertEquals([], $parser->bson(""));
2732
}
@@ -30,7 +35,7 @@ public function parser_empty_bson_data()
3035
/** @test */
3136
public function throw_an_exception_when_bson_library_not_loaded()
3237
{
33-
if ( ! function_exists('bson_decode')) {
38+
if ( ! (function_exists('bson_decode') || function_exists('MongoDB\BSON\toPHP'))) {
3439
$this->setExpectedException('Exception', 'Failed To Parse BSON - Supporting Library Not Available');
3540

3641
$parser = new Parser();
@@ -41,7 +46,7 @@ public function throw_an_exception_when_bson_library_not_loaded()
4146
/** @test */
4247
public function throws_an_exception_when_parsed_bson_bad_data()
4348
{
44-
if (function_exists('bson_decode')) {
49+
if (function_exists('bson_decode') || function_exists('MongoDB\BSON\toPHP')) {
4550
$parser = new Parser();
4651
$this->setExpectedException('Exception', 'Failed To Parse BSON');
4752
$parser->bson('as|df>ASFBw924hg2=');
@@ -52,7 +57,7 @@ public function throws_an_exception_when_parsed_bson_bad_data()
5257
public function format_detection_bson()
5358
{
5459
$parser = new Parser();
55-
60+
5661
$_SERVER['HTTP_CONTENT_TYPE'] = "application/bson";
5762
$this->assertEquals('Nathanmac\Utilities\Parser\Formats\BSON', $parser->getFormatClass());
5863

0 commit comments

Comments
 (0)