Skip to content

Commit cfbe691

Browse files
committed
init library from inhere/lite-cache
0 parents  commit cfbe691

14 files changed

+433
-0
lines changed

.editorconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
root = true
2+
3+
# 对所有文件生效
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 2
8+
end_of_line = lf
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
12+
# 对后缀名为 md 的文件生效
13+
[*.md]
14+
trim_trailing_whitespace = false
15+
16+
[*.php]
17+
indent_size = 4

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.idea/
2+
.phpintel/
3+
!README.md
4+
!.gitkeep
5+
composer.lock
6+
*.swp
7+
*.log
8+
*.pid
9+
*.patch
10+
.DS_Store

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 inhere
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# data-parser utils for php
2+
3+
## Install
4+
5+
```bash
6+
composer require mylib/data-parser
7+
```
8+
9+
## license
10+
11+
MIT

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "mylib/data-parser",
3+
"type": "library",
4+
"description": "some data parser tool library of the php",
5+
"keywords": ["library","tool","php"],
6+
"homepage": "https://github.com/php-mylib/data-parser",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "inhere",
11+
"email": "in.798@qq.com",
12+
"homepage": "http://www.yzone.net/"
13+
}
14+
],
15+
"require": {
16+
"php": ">7.0.0"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"MyLib\\DataParser\\" : "src/"
21+
}
22+
},
23+
"suggest": {
24+
"inhere/simple-print-tool": "Very lightweight data printing tools",
25+
"inhere/php-validate": "Very lightweight data validate tool",
26+
"inhere/console": "a lightweight php console application library."
27+
}
28+
}

phpunit.xml.dist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
bootstrap="test/boot.php"
6+
colors="false"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Php Library Test Suite">
15+
<directory>test/</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
<filter>
20+
<whitelist>
21+
<directory suffix=".php">src</directory>
22+
</whitelist>
23+
</filter>
24+
</phpunit>

src/DataParserAwareTrait.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-12-14
6+
* Time: 19:37
7+
*/
8+
9+
namespace MyLib\DataParser;
10+
11+
/**
12+
* Class DataParserAwareTrait
13+
* @package MyLib\DataParser
14+
*/
15+
trait DataParserAwareTrait
16+
{
17+
/**
18+
* @var ParserInterface
19+
*/
20+
private $parser;
21+
22+
/**
23+
* @return ParserInterface
24+
*/
25+
public function getParser(): ParserInterface
26+
{
27+
if (!$this->parser) {
28+
$this->parser = new PhpParser();
29+
}
30+
31+
return $this->parser;
32+
}
33+
34+
/**
35+
* @param ParserInterface $parser
36+
*/
37+
public function setParser(ParserInterface $parser)
38+
{
39+
$this->parser = $parser;
40+
}
41+
}

src/JsonParser.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-12-14
6+
* Time: 19:07
7+
*/
8+
9+
namespace MyLib\DataParser;
10+
11+
/**
12+
* Class JsonParser
13+
* @package MyLib\DataParser
14+
*/
15+
class JsonParser implements ParserInterface
16+
{
17+
/**
18+
* @var bool
19+
*/
20+
protected $assoc = true;
21+
22+
/**
23+
* JsonParser constructor.
24+
* @param null $assoc
25+
*/
26+
public function __construct($assoc = null)
27+
{
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);
40+
}
41+
42+
/**
43+
* @param mixed $data
44+
* @return string
45+
*/
46+
public function encode($data): string
47+
{
48+
return \json_encode($data);
49+
}
50+
51+
/**
52+
* @return bool
53+
*/
54+
public function isAssoc(): bool
55+
{
56+
return $this->assoc;
57+
}
58+
59+
/**
60+
* @param bool $assoc
61+
*/
62+
public function setAssoc($assoc)
63+
{
64+
$this->assoc = (bool)$assoc;
65+
}
66+
}

src/MsgPackParser.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2017/12/16 0016
6+
* Time: 21:23
7+
*/
8+
9+
namespace MyLib\DataParser;
10+
11+
/**
12+
* Class MsgPackParser
13+
* @package MyLib\DataParser
14+
*/
15+
class MsgPackParser implements ParserInterface
16+
{
17+
/**
18+
* class constructor.
19+
* @throws \RuntimeException
20+
*/
21+
public function __construct()
22+
{
23+
if (!\function_exists('msgpack_pack')) {
24+
throw new \RuntimeException("The php extension 'msgpack' is required!");
25+
}
26+
}
27+
28+
/**
29+
* @param mixed $data
30+
* @return string
31+
*/
32+
public function encode($data): string
33+
{
34+
return \msgpack_pack($data);
35+
}
36+
37+
/**
38+
* @param string $data
39+
* @return mixed
40+
*/
41+
public function decode(string $data)
42+
{
43+
return \msgpack_unpack($data);
44+
}
45+
}

src/ParserInterface.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-12-14
6+
* Time: 19:07
7+
*/
8+
9+
namespace MyLib\DataParser;
10+
11+
/**
12+
* Interface ParserInterface
13+
* @package MyLib\DataParser
14+
*/
15+
interface ParserInterface
16+
{
17+
/**
18+
* @param mixed $data
19+
* @return string
20+
*/
21+
public function encode($data): string;
22+
23+
/**
24+
* @param string $data
25+
* @return mixed
26+
*/
27+
public function decode(string $data);
28+
}

0 commit comments

Comments
 (0)