-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cfbe691
Showing
14 changed files
with
433 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
root = true | ||
|
||
# 对所有文件生效 | ||
[*] | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 2 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
# 对后缀名为 md 的文件生效 | ||
[*.md] | ||
trim_trailing_whitespace = false | ||
|
||
[*.php] | ||
indent_size = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.idea/ | ||
.phpintel/ | ||
!README.md | ||
!.gitkeep | ||
composer.lock | ||
*.swp | ||
*.log | ||
*.pid | ||
*.patch | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 inhere | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# data-parser utils for php | ||
|
||
## Install | ||
|
||
```bash | ||
composer require mylib/data-parser | ||
``` | ||
|
||
## license | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "mylib/data-parser", | ||
"type": "library", | ||
"description": "some data parser tool library of the php", | ||
"keywords": ["library","tool","php"], | ||
"homepage": "https://github.com/php-mylib/data-parser", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "inhere", | ||
"email": "in.798@qq.com", | ||
"homepage": "http://www.yzone.net/" | ||
} | ||
], | ||
"require": { | ||
"php": ">7.0.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"MyLib\\DataParser\\" : "src/" | ||
} | ||
}, | ||
"suggest": { | ||
"inhere/simple-print-tool": "Very lightweight data printing tools", | ||
"inhere/php-validate": "Very lightweight data validate tool", | ||
"inhere/console": "a lightweight php console application library." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="test/boot.php" | ||
colors="false" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
> | ||
<testsuites> | ||
<testsuite name="Php Library Test Suite"> | ||
<directory>test/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: inhere | ||
* Date: 2017-12-14 | ||
* Time: 19:37 | ||
*/ | ||
|
||
namespace MyLib\DataParser; | ||
|
||
/** | ||
* Class DataParserAwareTrait | ||
* @package MyLib\DataParser | ||
*/ | ||
trait DataParserAwareTrait | ||
{ | ||
/** | ||
* @var ParserInterface | ||
*/ | ||
private $parser; | ||
|
||
/** | ||
* @return ParserInterface | ||
*/ | ||
public function getParser(): ParserInterface | ||
{ | ||
if (!$this->parser) { | ||
$this->parser = new PhpParser(); | ||
} | ||
|
||
return $this->parser; | ||
} | ||
|
||
/** | ||
* @param ParserInterface $parser | ||
*/ | ||
public function setParser(ParserInterface $parser) | ||
{ | ||
$this->parser = $parser; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: inhere | ||
* Date: 2017-12-14 | ||
* Time: 19:07 | ||
*/ | ||
|
||
namespace MyLib\DataParser; | ||
|
||
/** | ||
* Class JsonParser | ||
* @package MyLib\DataParser | ||
*/ | ||
class JsonParser implements ParserInterface | ||
{ | ||
/** | ||
* @var bool | ||
*/ | ||
protected $assoc = true; | ||
|
||
/** | ||
* JsonParser constructor. | ||
* @param null $assoc | ||
*/ | ||
public function __construct($assoc = null) | ||
{ | ||
if ($assoc !== null) { | ||
$this->setAssoc($assoc); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $data | ||
* @return mixed | ||
*/ | ||
public function decode(string $data) | ||
{ | ||
return \json_decode($data, $this->assoc); | ||
} | ||
|
||
/** | ||
* @param mixed $data | ||
* @return string | ||
*/ | ||
public function encode($data): string | ||
{ | ||
return \json_encode($data); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isAssoc(): bool | ||
{ | ||
return $this->assoc; | ||
} | ||
|
||
/** | ||
* @param bool $assoc | ||
*/ | ||
public function setAssoc($assoc) | ||
{ | ||
$this->assoc = (bool)$assoc; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Inhere | ||
* Date: 2017/12/16 0016 | ||
* Time: 21:23 | ||
*/ | ||
|
||
namespace MyLib\DataParser; | ||
|
||
/** | ||
* Class MsgPackParser | ||
* @package MyLib\DataParser | ||
*/ | ||
class MsgPackParser implements ParserInterface | ||
{ | ||
/** | ||
* class constructor. | ||
* @throws \RuntimeException | ||
*/ | ||
public function __construct() | ||
{ | ||
if (!\function_exists('msgpack_pack')) { | ||
throw new \RuntimeException("The php extension 'msgpack' is required!"); | ||
} | ||
} | ||
|
||
/** | ||
* @param mixed $data | ||
* @return string | ||
*/ | ||
public function encode($data): string | ||
{ | ||
return \msgpack_pack($data); | ||
} | ||
|
||
/** | ||
* @param string $data | ||
* @return mixed | ||
*/ | ||
public function decode(string $data) | ||
{ | ||
return \msgpack_unpack($data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: inhere | ||
* Date: 2017-12-14 | ||
* Time: 19:07 | ||
*/ | ||
|
||
namespace MyLib\DataParser; | ||
|
||
/** | ||
* Interface ParserInterface | ||
* @package MyLib\DataParser | ||
*/ | ||
interface ParserInterface | ||
{ | ||
/** | ||
* @param mixed $data | ||
* @return string | ||
*/ | ||
public function encode($data): string; | ||
|
||
/** | ||
* @param string $data | ||
* @return mixed | ||
*/ | ||
public function decode(string $data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: inhere | ||
* Date: 2017-12-14 | ||
* Time: 19:07 | ||
*/ | ||
|
||
namespace MyLib\DataParser; | ||
|
||
/** | ||
* Class PhpParser | ||
* @package MyLib\DataParser | ||
*/ | ||
class PhpParser implements ParserInterface | ||
{ | ||
/** | ||
* @param mixed $data | ||
* @return string | ||
*/ | ||
public function encode($data): string | ||
{ | ||
return \serialize($data); | ||
} | ||
|
||
/** | ||
* @param string $data | ||
* @return mixed | ||
*/ | ||
public function decode(string $data) | ||
{ | ||
return \unserialize($data, ['allowed_classes' => false]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Inhere | ||
* Date: 2018/3/28 0028 | ||
* Time: 19:25 | ||
*/ | ||
|
||
use MyLib\DataParser\JsonParser; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Class JsonParserTest | ||
* @covers JsonParser | ||
*/ | ||
class JsonParserTest extends TestCase | ||
{ | ||
public function testDecode() | ||
{ | ||
$str = '{"name": "value"}'; | ||
|
||
$parser = new JsonParser(); | ||
$ret = $parser->decode($str); | ||
|
||
$this->assertInternalType('array', $ret); | ||
$this->assertArrayHasKey('name', $ret); | ||
} | ||
|
||
public function testEncode() | ||
{ | ||
$data = [ | ||
'name' => 'value', | ||
]; | ||
|
||
$parser = new JsonParser(); | ||
$ret = $parser->encode($data); | ||
|
||
$this->assertInternalType('string', $ret); | ||
$this->assertJson($ret); | ||
} | ||
} |
Oops, something went wrong.