Skip to content

Commit

Permalink
init library from inhere/lite-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 28, 2018
0 parents commit cfbe691
Show file tree
Hide file tree
Showing 14 changed files with 433 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
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
10 changes: 10 additions & 0 deletions .gitignore
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
20 changes: 20 additions & 0 deletions LICENSE
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.
11 changes: 11 additions & 0 deletions README.md
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
28 changes: 28 additions & 0 deletions composer.json
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."
}
}
24 changes: 24 additions & 0 deletions phpunit.xml.dist
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>
41 changes: 41 additions & 0 deletions src/DataParserAwareTrait.php
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;
}
}
66 changes: 66 additions & 0 deletions src/JsonParser.php
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;
}
}
45 changes: 45 additions & 0 deletions src/MsgPackParser.php
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);
}
}
28 changes: 28 additions & 0 deletions src/ParserInterface.php
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);
}
34 changes: 34 additions & 0 deletions src/PhpParser.php
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]);
}
}
41 changes: 41 additions & 0 deletions test/JsonParserTest.php
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);
}
}
Loading

0 comments on commit cfbe691

Please sign in to comment.