Skip to content

Commit c76a843

Browse files
author
苏青安
committed
feat(core): 最小可运行版本发布
1 parent ca291cd commit c76a843

37 files changed

+1181
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
/test.php

composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "hejunjie/schema-validator",
3+
"description": "基于规则类实现的 PHP 类型验证组件,支持字符串、整数、布尔值等常用类型验证 | A lightweight and extensible PHP type validation library based on rule classes.",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Hejunjie\\SchemaValidator\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "何俊杰",
14+
"email": "[email protected]"
15+
}
16+
],
17+
"require": {
18+
"php": ">=8.1"
19+
}
20+
}

src/Contracts/RuleInterface.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Hejunjie\SchemaValidator\Contracts;
4+
5+
interface RuleInterface
6+
{
7+
/**
8+
* 验证字段值
9+
*
10+
* @param string $field 字段名(例:user.email)
11+
* @param mixed $value 传入值
12+
* @param mixed|null $params 规则附加参数(如 min:3 中的 3)
13+
*
14+
* @throws \Hejunjie\SchemaValidator\Exceptions\ValidationException
15+
* @return void
16+
*/
17+
public function validate(string $field, $value, $params = null): void;
18+
19+
/**
20+
* 规则名称(用于注册)
21+
*/
22+
public function getName(): string;
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Hejunjie\SchemaValidator\Exceptions;
4+
5+
use Exception;
6+
7+
class ValidationException extends \Exception
8+
{
9+
protected array $errors;
10+
11+
public function __construct(array $errors)
12+
{
13+
parent::__construct("Validation failed");
14+
$this->errors = $errors;
15+
}
16+
17+
/**
18+
* 获取所有验证错误
19+
*/
20+
public function getErrors(): array
21+
{
22+
return $this->errors;
23+
}
24+
}

src/Helpers.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Hejunjie\SchemaValidator;
4+
5+
class Helpers
6+
{
7+
/**
8+
* 从数组中获取字段值(支持点语法,如 user.email)
9+
*/
10+
public static function getValue(array $data, string $field)
11+
{
12+
if (strpos($field, '.') === false) {
13+
return $data[$field] ?? null;
14+
}
15+
16+
$segments = explode('.', $field);
17+
foreach ($segments as $segment) {
18+
if (!is_array($data) || !array_key_exists($segment, $data)) {
19+
return null;
20+
}
21+
$data = $data[$segment];
22+
}
23+
return $data;
24+
}
25+
26+
/**
27+
* 将字符串转换为驼峰命名法(如 "user_name" 转为 "UserName")
28+
*/
29+
public static function studly_case(string $string): string
30+
{
31+
return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $string)));
32+
}
33+
}

src/Rules/AcceptedRule.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Hejunjie\SchemaValidator\Rules;
4+
5+
use Hejunjie\SchemaValidator\Exceptions\ValidationException;
6+
use Hejunjie\SchemaValidator\Contracts\RuleInterface;
7+
8+
/**
9+
* 验证:内容只能为 yes、on、1、true
10+
* @package Hejunjie\SchemaValidator\Rules
11+
*/
12+
class AcceptedRule implements RuleInterface
13+
{
14+
private string $name = 'accepted';
15+
16+
public function getName(): string
17+
{
18+
return $this->name;
19+
}
20+
21+
public function validate(string $field, $value, $params = null): void
22+
{
23+
$validValues = ['yes', 'on', '1', 'true'];
24+
if (!in_array(strtolower($value), $validValues, true)) {
25+
throw new ValidationException([$field => [$this->name]]);
26+
}
27+
}
28+
}

src/Rules/AlphaDashRule.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Hejunjie\SchemaValidator\Rules;
4+
5+
use Hejunjie\SchemaValidator\Exceptions\ValidationException;
6+
use Hejunjie\SchemaValidator\Contracts\RuleInterface;
7+
8+
/**
9+
* 验证:内容只能包含字母、数字、破折号、下划线
10+
* @package Hejunjie\SchemaValidator\Rules
11+
*/
12+
class AlphaDashRule implements RuleInterface
13+
{
14+
private string $name = 'alpha_dash';
15+
16+
public function getName(): string
17+
{
18+
return $this->name;
19+
}
20+
21+
public function validate(string $field, $value, $params = null): void
22+
{
23+
if (!preg_match('/^[a-zA-Z0-9_-]+$/', $value)) {
24+
throw new ValidationException([$field => [$this->name]]);
25+
}
26+
}
27+
}

src/Rules/AlphaNumRule.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Hejunjie\SchemaValidator\Rules;
4+
5+
use Hejunjie\SchemaValidator\Exceptions\ValidationException;
6+
use Hejunjie\SchemaValidator\Contracts\RuleInterface;
7+
8+
/**
9+
* 验证:内容只能包含字母和数字
10+
* @package Hejunjie\SchemaValidator\Rules
11+
*/
12+
class AlphaNumRule implements RuleInterface
13+
{
14+
private string $name = 'alpha_num';
15+
16+
public function getName(): string
17+
{
18+
return $this->name;
19+
}
20+
21+
public function validate(string $field, $value, $params = null): void
22+
{
23+
if (!preg_match('/^[a-zA-Z0-9]+$/', $value)) {
24+
throw new ValidationException([$field => [$this->name]]);
25+
}
26+
}
27+
}

src/Rules/AlphaRule.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Hejunjie\SchemaValidator\Rules;
4+
5+
use Hejunjie\SchemaValidator\Exceptions\ValidationException;
6+
use Hejunjie\SchemaValidator\Contracts\RuleInterface;
7+
8+
/**
9+
* 验证:内容只能包含字母
10+
* @package Hejunjie\SchemaValidator\Rules
11+
*/
12+
class AlphaRule implements RuleInterface
13+
{
14+
private string $name = 'alpha';
15+
16+
public function getName(): string
17+
{
18+
return $this->name;
19+
}
20+
21+
public function validate(string $field, $value, $params = null): void
22+
{
23+
if (!preg_match('/^[a-zA-Z]+$/', $value)) {
24+
throw new ValidationException([$field => [$this->name]]);
25+
}
26+
}
27+
}

src/Rules/ArrayRule.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Hejunjie\SchemaValidator\Rules;
4+
5+
use Hejunjie\SchemaValidator\Exceptions\ValidationException;
6+
use Hejunjie\SchemaValidator\Contracts\RuleInterface;
7+
8+
/**
9+
* 验证:内容必须为数组
10+
* @package Hejunjie\SchemaValidator\Rules
11+
*/
12+
class ArrayRule implements RuleInterface
13+
{
14+
private string $name = 'array';
15+
16+
public function getName(): string
17+
{
18+
return $this->name;
19+
}
20+
21+
public function validate(string $field, $value, $params = null): void
22+
{
23+
if (!is_array($value)) {
24+
throw new ValidationException([$field => [$this->name]]);
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)