File tree Expand file tree Collapse file tree 2 files changed +82
-2
lines changed
Expand file tree Collapse file tree 2 files changed +82
-2
lines changed Original file line number Diff line number Diff line change 1- # data-parser utils for php
1+ # data parser utils
2+
3+ simple data parser for php
4+
5+ driver
6+
7+ - json(by ` json_encode ` )
8+ - php(by ` serialize ` )
9+ - swoole(by extension ` swoole_serialize ` )
10+ - msgpack(by extension ` msgpack ` )
211
312## Install
413
14+ - composer command
15+
16+ ``` bash
17+ composer require swoft/data-parser
18+ ```
19+
20+ ## Usage
21+
22+ ``` php
23+ $parser = new SwooleParser();
24+ // $parser = new JsonParser();
25+ // $parser = new PhpParser();
26+ // $parser = new MsgPackParser();
27+
28+ // encode
29+ $encoded = $parser->encode($data);
30+
31+ // decode
32+ $decoded = $parser->encode($encoded);
33+ ```
34+
35+ ## Unit testing
36+
537``` bash
6- composer require mylib/data-parser
38+ phpunit
739```
840
941## license
Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: Inhere
5+ * Date: 2018/3/29 0029
6+ * Time: 19:22
7+ */
8+
9+ namespace MyLib \DataParser ;
10+
11+ use Swoole \Serialize ;
12+
13+ /**
14+ * Class SwooleParser
15+ * @author inhere <in.798@qq.com>
16+ * @link https://wiki.swoole.com/wiki/page/p-serialize.html
17+ */
18+ class SwooleParser implements ParserInterface
19+ {
20+ /**
21+ * class constructor.
22+ * @throws \RuntimeException
23+ */
24+ public function __construct ()
25+ {
26+ if (!\class_exists (Serialize::class, false )) {
27+ throw new \RuntimeException ("The php extension 'swoole_serialize' is required! " );
28+ }
29+ }
30+
31+ /**
32+ * @param mixed $data
33+ * @return string
34+ */
35+ public function encode ($ data ): string
36+ {
37+ return (string )Serialize::pack ($ data );
38+ }
39+
40+ /**
41+ * @param string $data
42+ * @return mixed
43+ */
44+ public function decode (string $ data )
45+ {
46+ return Serialize::unpack ($ data );
47+ }
48+ }
You can’t perform that action at this time.
0 commit comments