Skip to content

Commit c371961

Browse files
committed
feat: init project
0 parents  commit c371961

11 files changed

Lines changed: 219 additions & 0 deletions

File tree

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.buildpath
2+
.settings/
3+
.project
4+
*.patch
5+
.idea/
6+
.git/
7+
runtime/
8+
vendor/
9+
.phpintel/
10+
.env
11+
.DS_Store
12+
*.lock
13+
.phpunit*
14+
*.cache

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## php工具包 (php basic tool)
2+
3+
### DTO
4+
5+
6+
### phpunit 配置
7+
添加测试用例,保证源代码不会报错
8+
9+
10+
11+
### 文章
12+
13+
[创建属于自己的 composer 包](https://dmf-code.github.io/posts/54650cde2a44/)
14+
15+

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "rice/basic",
3+
"description": "basic tool",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Rice\\Basic\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "dengminfeng",
14+
"email": "1015814408@qq.com"
15+
}
16+
],
17+
"minimum-stability": "dev",
18+
"require-dev": {
19+
"phpunit/phpunit": "9"
20+
},
21+
"autoload-dev": {
22+
"psr-4": {
23+
"Tests\\": "tests/"
24+
},
25+
"files": [
26+
"tests/common.php"
27+
]
28+
}
29+
}

phpunit.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<phpunit colors="true" bootstrap="./vendor/autoload.php">
2+
<testsuites>
3+
<testsuite name="Application Test Suite">
4+
<directory suffix="Test.php">tests</directory>
5+
</testsuite>
6+
</testsuites>
7+
8+
<filter>
9+
<whitelist processUncoveredFilesFromWhitelist="true">
10+
<directory suffix=".php">./src</directory>
11+
</whitelist>
12+
</filter>
13+
</phpunit>

src/dto/DTO.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Rice\Basic\dto;
4+
5+
6+
use Rice\Basic\Exception\DTOException;
7+
8+
abstract class DTO
9+
{
10+
public function __call($name, $args)
11+
{
12+
preg_match('/^([sg]et)(.*)/', $name, $matchArr);
13+
14+
$style = $matchArr[1] ?? null;
15+
$attrName = $matchArr[2] ?? null;
16+
17+
if (!is_null($attrName)) {
18+
$attrName = lcfirst($attrName);
19+
}
20+
21+
if (!property_exists($this, $attrName)) {
22+
throw new DTOException('this attr not define');
23+
}
24+
25+
switch ($style) {
26+
case 'set':
27+
$this->setValue($attrName, $args);
28+
return $this;
29+
case 'get':
30+
return $this->getValue($attrName);
31+
}
32+
33+
throw new DTOException('this method not define');
34+
}
35+
36+
private function setValue($attrName, $args)
37+
{
38+
$this->{$attrName} = $args[0];
39+
}
40+
41+
private function getValue($attrName)
42+
{
43+
return $this->{$attrName};
44+
}
45+
46+
public function toArray($fields = [])
47+
{
48+
$result = [];
49+
foreach (get_object_vars($this) as $k => $v) {
50+
if (empty($fields)) {
51+
$result[$k] = $v;
52+
} elseif (in_array($k, $fields)) {
53+
$result[$k] = $v;
54+
}
55+
}
56+
57+
return $result;
58+
}
59+
}

src/dto/Response.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
4+
namespace Rice\Basic\dto;
5+
6+
7+
class Response extends DTO
8+
{
9+
private $success;
10+
11+
private $errCode;
12+
13+
private $errMessage;
14+
15+
public function isSuccess(): bool
16+
{
17+
return $this->success;
18+
}
19+
20+
public function setSuccess(bool $success)
21+
{
22+
$this->success = $success;
23+
}
24+
25+
public function getErrCode(): string
26+
{
27+
return $this->errCode;
28+
}
29+
30+
}

src/enum/DTOEnum.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace enum;
3+
4+
class DTOEnum
5+
{
6+
const VERSION = 1.0;
7+
}

src/exception/DTOException.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Rice\Basic\Exception;
4+
5+
class DTOException extends \Exception
6+
{
7+
8+
}

tests/common.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
if (!function_exists('dump')) {
4+
function dump($vars)
5+
{
6+
var_dump($vars);
7+
die();
8+
}
9+
}
10+

tests/dto/DtoTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Tests\dto;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
8+
class DtoTest extends TestCase
9+
{
10+
public function testSetElement()
11+
{
12+
$dto = new ObjDTO();
13+
$dto->setName('tests');
14+
$this->assertEquals('tests', $dto->getName());
15+
$dto->toArray();
16+
}
17+
}

0 commit comments

Comments
 (0)