Skip to content

Commit 8b26550

Browse files
committed
update, add some test
1 parent 6758a63 commit 8b26550

File tree

9 files changed

+70
-14
lines changed

9 files changed

+70
-14
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# file-parse for php
22

3+
parse ini,json,yml file.
4+
5+
## install
6+
7+
```bash
8+
composer require mylib/file-parse
9+
```
310

411
## license
512

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
}
1414
],
1515
"require": {
16-
"php": ">=7.0.0"
16+
"php": ">=7.0.0",
17+
"mylib/str-utils": "~1.0"
1718
},
1819
"autoload": {
1920
"psr-4": {
2021
"MyLib\\FileParse\\" : "src/"
2122
}
2223
},
2324
"suggest": {
24-
"inhere/simple-print-tool": "Very lightweight data printing tools",
2525
"inhere/php-validate": "Very lightweight data validate tool",
2626
"inhere/console": "a lightweight php console application library."
2727
}

src/BaseParser.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ abstract protected static function doParse(
3030
$string,
3131
$enhancement = false,
3232
callable $pathHandler = null,
33-
$fileDir = ''
34-
);
33+
string $fileDir = ''
34+
): array;
3535

3636
/**
3737
* @param $string
@@ -40,7 +40,7 @@ abstract protected static function doParse(
4040
* @param string $fileDir
4141
* @return array
4242
*/
43-
public static function parse($string, $enhancement = false, callable $pathHandler = null, $fileDir = '')
43+
public static function parse($string, $enhancement = false, callable $pathHandler = null, $fileDir = ''): array
4444
{
4545
if (is_file($string)) {
4646
return self::parseFile($string, $enhancement, $pathHandler, $fileDir);
@@ -57,7 +57,7 @@ public static function parse($string, $enhancement = false, callable $pathHandle
5757
* @return array
5858
* @throws \InvalidArgumentException
5959
*/
60-
public static function parseFile($file, $enhancement = false, callable $pathHandler = null, $fileDir = '')
60+
public static function parseFile($file, $enhancement = false, callable $pathHandler = null, $fileDir = ''): array
6161
{
6262
if (!is_file($file)) {
6363
throw new \InvalidArgumentException("Target file [$file] not exists");
@@ -76,7 +76,7 @@ public static function parseFile($file, $enhancement = false, callable $pathHand
7676
* @param string $fileDir
7777
* @return array
7878
*/
79-
public static function parseString($string, $enhancement = false, callable $pathHandler = null, $fileDir = '')
79+
public static function parseString($string, $enhancement = false, callable $pathHandler = null, $fileDir = ''): array
8080
{
8181
return static::doParse($string, $enhancement, $pathHandler, $fileDir);
8282
}

src/IniParser.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class IniParser extends BaseParser
2424
* @throws \InvalidArgumentException
2525
* @throws \UnexpectedValueException
2626
*/
27-
protected static function doParse($string, $enhancement = false, callable $pathHandler = null, $fileDir = '')
27+
protected static function doParse($string, $enhancement = false, callable $pathHandler = null, string $fileDir = ''): array
2828
{
2929
if (!$string) {
3030
return [];
@@ -40,8 +40,8 @@ protected static function doParse($string, $enhancement = false, callable $pathH
4040
/*
4141
* Parse special keywords
4242
*
43-
* extend = ../parent.yml
44-
* db = import#../db.yml
43+
* extend = ../parent.ini
44+
* db = import#../db.ini
4545
* [cache]
4646
* debug = reference#debug
4747
*/
@@ -72,7 +72,7 @@ protected static function doParse($string, $enhancement = false, callable $pathH
7272
}
7373

7474
if (0 === strpos($item, self::IMPORT_KEY . '#')) {
75-
$importFile = trim(substr($item, 6));
75+
$importFile = trim(substr($item, 7));
7676

7777
// if needed custom handle $importFile path. e.g: Maybe it uses custom alias path
7878
if ($pathHandler && \is_callable($pathHandler)) {

src/JsonParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace MyLib\FileParse;
1010

11-
use Inhere\Library\Helpers\JsonHelper;
11+
use MyLib\StrUtil\JsonHelper;
1212

1313
/**
1414
* Class JsonParser
@@ -26,7 +26,7 @@ class JsonParser extends BaseParser
2626
* @throws \InvalidArgumentException
2727
* @throws \UnexpectedValueException
2828
*/
29-
protected static function doParse($string, $enhancement = false, callable $pathHandler = null, $fileDir = '')
29+
protected static function doParse($string, $enhancement = false, callable $pathHandler = null, string $fileDir = ''): array
3030
{
3131
if (!$string) {
3232
return [];

src/YmlParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class YmlParser extends BaseParser
2626
* @throws \InvalidArgumentException
2727
* @throws \UnexpectedValueException
2828
*/
29-
protected static function doParse($string, $enhancement = false, callable $pathHandler = null, $fileDir = '')
29+
protected static function doParse($string, $enhancement = false, callable $pathHandler = null, string $fileDir = ''): array
3030
{
3131
if (!$string) {
3232
return [];

test/IniParserTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2018/3/8 0008
6+
* Time: 20:25
7+
*/
8+
9+
namespace MyLib\FileParse\Test;
10+
11+
use MyLib\FileParse\IniParser;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* Class IniParserTest
16+
* @package MyLib\FileParse\Test
17+
*/
18+
class IniParserTest extends TestCase
19+
{
20+
/**
21+
* simple parse
22+
*/
23+
public function testParse()
24+
{
25+
$ret = IniParser::parseFile(__DIR__ . '/data/test.ini');
26+
27+
$this->assertArrayHasKey('name', $ret);
28+
$this->assertSame('import#include.ini', $ret['include']);
29+
30+
31+
$ret = IniParser::parseFile(__DIR__ . '/data/test.ini', true);
32+
33+
$this->assertArrayHasKey('name', $ret);
34+
$this->assertArrayHasKey('he', $ret['include']);
35+
}
36+
}

test/data/include.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
; comments
2+
he='i am in the other file'
3+
4+
[arr]
5+
key = val

test/data/test.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
; comments
2+
name = join
3+
age=23
4+
5+
include=import#include.ini
6+
7+
[sub]
8+
hello=world

0 commit comments

Comments
 (0)