Skip to content

Commit 5a97bcd

Browse files
committed
add test config. update sys-utils namespace
1 parent 31f3e40 commit 5a97bcd

11 files changed

+157
-49
lines changed

src/AbstractDataParser.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2018/4/30 0030
6+
* Time: 12:47
7+
*/
8+
9+
namespace Toolkit\DataParser;
10+
11+
/**
12+
* Class AbstractDataParser
13+
* @package Toolkit\DataParser
14+
* @author inhere <in.798@qq.com>
15+
*/
16+
abstract class AbstractDataParser implements DataParserInterface
17+
{
18+
/**
19+
* @var array
20+
*/
21+
protected $encodeOpts;
22+
23+
/**
24+
* @var array
25+
*/
26+
protected $decodeOpts;
27+
28+
/**
29+
* JsonParser constructor.
30+
* @param array $encodeOpts
31+
* @param array $decodeOpts
32+
*/
33+
public function __construct(array $encodeOpts = [], array $decodeOpts = [])
34+
{
35+
$this->encodeOpts = $encodeOpts;
36+
$this->decodeOpts = $decodeOpts;
37+
}
38+
39+
/**
40+
* @return array
41+
*/
42+
public function getEncodeOpts(): array
43+
{
44+
return $this->encodeOpts;
45+
}
46+
47+
/**
48+
* @return array
49+
*/
50+
public function getDecodeOpts(): array
51+
{
52+
return $this->decodeOpts;
53+
}
54+
}

src/DataParserAwareTrait.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@
1111
/**
1212
* Class DataParserAwareTrait
1313
* @package Toolkit\DataParser
14+
* @author inhere <in.798@qq.com>
1415
*/
1516
trait DataParserAwareTrait
1617
{
1718
/**
18-
* @var ParserInterface
19+
* @var DataParserInterface
1920
*/
2021
private $parser;
2122

2223
/**
23-
* @return ParserInterface
24+
* @return DataParserInterface
2425
*/
25-
public function getParser(): ParserInterface
26+
public function getParser(): DataParserInterface
2627
{
2728
if (!$this->parser) {
2829
$this->parser = new PhpParser();
@@ -32,10 +33,13 @@ public function getParser(): ParserInterface
3233
}
3334

3435
/**
35-
* @param ParserInterface $parser
36+
* @param DataParserInterface $parser
37+
* @return DataParserAwareTrait
3638
*/
37-
public function setParser(ParserInterface $parser)
39+
public function setParser(DataParserInterface $parser): self
3840
{
3941
$this->parser = $parser;
42+
43+
return $this;
4044
}
4145
}

src/ParserInterface.php renamed to src/DataParserInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
/**
1212
* Interface ParserInterface
1313
* @package Toolkit\DataParser
14+
* @author inhere <in.798@qq.com>
1415
*/
15-
interface ParserInterface
16+
interface DataParserInterface
1617
{
1718
/**
1819
* @param mixed $data

src/JsonParser.php

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,18 @@
1111
/**
1212
* Class JsonParser
1313
* @package Toolkit\DataParser
14+
* @author inhere <in.798@qq.com>
1415
*/
15-
class JsonParser implements ParserInterface
16+
class JsonParser extends AbstractDataParser
1617
{
1718
/**
18-
* @var bool
19+
* class constructor.
20+
* @param array $encodeOpts
21+
* @param array $decodeOpts
1922
*/
20-
protected $assoc = true;
21-
22-
/**
23-
* JsonParser constructor.
24-
* @param null $assoc
25-
*/
26-
public function __construct($assoc = null)
23+
public function __construct(array $encodeOpts = [], array $decodeOpts = [])
2724
{
28-
if ($assoc !== null) {
29-
$this->setAssoc($assoc);
30-
}
31-
}
32-
33-
/**
34-
* @param string $data
35-
* @return mixed
36-
*/
37-
public function decode(string $data)
38-
{
39-
return \json_decode($data, $this->assoc);
25+
parent::__construct($encodeOpts, $decodeOpts ?: [true]);
4026
}
4127

4228
/**
@@ -45,22 +31,15 @@ public function decode(string $data)
4531
*/
4632
public function encode($data): string
4733
{
48-
return \json_encode($data);
34+
return \json_encode($data, ...$this->encodeOpts);
4935
}
5036

5137
/**
52-
* @return bool
53-
*/
54-
public function isAssoc(): bool
55-
{
56-
return $this->assoc;
57-
}
58-
59-
/**
60-
* @param bool $assoc
38+
* @param string $data
39+
* @return mixed
6140
*/
62-
public function setAssoc($assoc)
41+
public function decode(string $data)
6342
{
64-
$this->assoc = (bool)$assoc;
43+
return \json_decode($data, ...$this->decodeOpts);
6544
}
6645
}

src/MsgPackParser.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
/**
1212
* Class MsgPackParser
1313
* @package Toolkit\DataParser
14+
* @author inhere <in.798@qq.com>
15+
* @link https://github.com/msgpack/msgpack-php php-ext
16+
* @link https://github.com/rybakit/msgpack.php php
1417
*/
15-
class MsgPackParser implements ParserInterface
18+
class MsgPackParser extends AbstractDataParser
1619
{
1720
/**
1821
* class constructor.
@@ -23,6 +26,8 @@ public function __construct()
2326
if (!\function_exists('msgpack_pack')) {
2427
throw new \RuntimeException("The php extension 'msgpack' is required!");
2528
}
29+
30+
parent::__construct();
2631
}
2732

2833
/**

src/PhpParser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
/**
1212
* Class PhpParser
1313
* @package Toolkit\DataParser
14+
* @author inhere <in.798@qq.com>
1415
*/
15-
class PhpParser implements ParserInterface
16+
class PhpParser implements DataParserInterface
1617
{
1718
/**
1819
* @param mixed $data

src/SwooleParser.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,24 @@
1212

1313
/**
1414
* Class SwooleParser
15+
* @package Toolkit\DataParser
1516
* @author inhere <in.798@qq.com>
1617
* @link https://wiki.swoole.com/wiki/page/p-serialize.html
1718
*/
18-
class SwooleParser implements ParserInterface
19+
class SwooleParser extends AbstractDataParser
1920
{
2021
/**
2122
* class constructor.
23+
* @param array $encodeOpts
2224
* @throws \RuntimeException
2325
*/
24-
public function __construct()
26+
public function __construct(array $encodeOpts = [])
2527
{
2628
if (!\class_exists(Serialize::class, false)) {
2729
throw new \RuntimeException("The php extension 'swoole_serialize' is required!");
2830
}
31+
32+
parent::__construct($encodeOpts);
2933
}
3034

3135
/**
@@ -34,7 +38,7 @@ public function __construct()
3438
*/
3539
public function encode($data): string
3640
{
37-
return (string)Serialize::pack($data);
41+
return (string)Serialize::pack($data, ...$this->encodeOpts);
3842
}
3943

4044
/**
@@ -43,6 +47,6 @@ public function encode($data): string
4347
*/
4448
public function decode(string $data)
4549
{
46-
return Serialize::unpack($data);
50+
return Serialize::unpack($data, ...$this->decodeOpts);
4751
}
4852
}

test/JsonParserTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
* Time: 19:25
77
*/
88

9+
namespace Toolkit\DataParserTest;
10+
911
use Toolkit\DataParser\JsonParser;
1012
use PHPUnit\Framework\TestCase;
1113

1214
/**
1315
* Class JsonParserTest
14-
* @covers JsonParser
16+
* @covers \Toolkit\DataParser\JsonParser
1517
*/
1618
class JsonParserTest extends TestCase
1719
{

test/PhpParserTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
* Time: 19:25
77
*/
88

9+
namespace Toolkit\DataParserTest;
10+
911
use Toolkit\DataParser\PhpParser;
1012
use PHPUnit\Framework\TestCase;
1113

1214
/**
1315
* Class PhpParserTest
14-
* @covers PhpParser
16+
* @covers \Toolkit\DataParser\PhpParser
1517
*/
1618
class PhpParserTest extends TestCase
1719
{

test/SwooleParserTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2018/4/30 0030
6+
* Time: 12:55
7+
*/
8+
9+
namespace Toolkit\DataParserTest;
10+
11+
use Toolkit\DataParser\SwooleParser;
12+
use PHPUnit\Framework\TestCase;
13+
use Swoole\Serialize;
14+
15+
/**
16+
* Class SwooleParserTest
17+
* @covers \Toolkit\DataParser\SwooleParser
18+
*/
19+
class SwooleParserTest extends TestCase
20+
{
21+
public function testEncode()
22+
{
23+
if (!\class_exists(Serialize::class, false)) {
24+
return;
25+
}
26+
27+
$data = [
28+
'name' => 'value',
29+
];
30+
31+
$parser = new SwooleParser();
32+
$ret = $parser->encode($data);
33+
34+
$this->assertInternalType('string', $ret);
35+
}
36+
37+
public function testDecode()
38+
{
39+
if (!\class_exists(Serialize::class, false)) {
40+
return;
41+
}
42+
43+
$data = [
44+
'name' => 'value',
45+
];
46+
47+
$parser = new SwooleParser();
48+
$str = $parser->encode($data);
49+
$ret = $parser->decode($str);
50+
51+
$this->assertInternalType('array', $ret);
52+
$this->assertArrayHasKey('name', $ret);
53+
}
54+
55+
}
56+

test/boot.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
if (0 === strpos($class,'Toolkit\DataParser\Example\\')) {
1414
$path = str_replace('\\', '/', substr($class, strlen('Toolkit\DataParser\Example\\')));
1515
$file = dirname(__DIR__) . "/example/{$path}.php";
16-
} elseif (0 === strpos($class,'Toolkit\DataParser\Test\\')) {
17-
$path = str_replace('\\', '/', substr($class, strlen('Toolkit\DataParser\Test\\')));
16+
} elseif (0 === strpos($class,'Toolkit\DataParserTest\\')) {
17+
$path = str_replace('\\', '/', substr($class, strlen('Toolkit\DataParserTest\\')));
1818
$file = __DIR__ . "/{$path}.php";
1919
} elseif (0 === strpos($class,'Toolkit\DataParser\\')) {
2020
$path = str_replace('\\', '/', substr($class, strlen('Toolkit\DataParser\\')));

0 commit comments

Comments
 (0)