Skip to content

Commit 437508e

Browse files
committed
add validation docs
1 parent bd51ba8 commit 437508e

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

docs/en/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* [Array Object Mapping](mapper/array-mapper.md)
1212
* [Union Type Mapping](mapper/union-mapper.md)
1313
* [Output Format](mapper/out.md)
14+
* [Validation](mapper/validate.md)
1415

1516
## Annotation Usage
1617

docs/en/mapper/validate.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## Parameter Validation
2+
3+
### Validation via the `validate` Method
4+
5+
The `Serialize` class provides a `validate` method that is automatically called when an object is created using the `from` method. You can implement custom data validation logic within this method.
6+
7+
Here's an example:
8+
9+
```php
10+
use Astral\Serialize\Serialize;
11+
12+
class TestConstructValidationFromSerialize extends Serialize
13+
{
14+
public string $type_string;
15+
16+
/**
17+
* Data validation method
18+
* Automatically called after object creation via the from method
19+
*/
20+
public function validate(): void
21+
{
22+
// Validate the value of the type_string property
23+
if ($this->type_string !== '123') {
24+
throw new Exception('type_string must be equal to 123');
25+
}
26+
27+
// You can also modify property values
28+
$this->type_string = '234';
29+
}
30+
}
31+
```
32+
33+
### Validation in `__construct`
34+
35+
When creating objects directly using the `__construct` method, you can implement parameter validation logic in the constructor. However, note that this approach can only access properties defined in the constructor and cannot access other properties.
36+
37+
```php
38+
use Astral\Serialize\Serialize;
39+
40+
class TestConstructFromSerialize extends Serialize
41+
{
42+
// Note: This property cannot be accessed in the constructor
43+
// If you need to validate this property, use the validate method
44+
public string $not_validate_string;
45+
46+
/**
47+
* Parameter validation in the constructor
48+
* @param string $type_string The input string parameter
49+
*/
50+
public function __construct(
51+
public string $type_string,
52+
) {
53+
// Validate the input parameter
54+
if ($this->type_string !== '123') {
55+
throw new Exception('type_string must be equal to 123');
56+
}
57+
58+
// Modify the property value
59+
$this->type_string = '234';
60+
}
61+
}

docs/zh/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* [数组对象转换](mapper/array-mapper.md)
1212
* [联合类型转换](mapper/union-mapper.md)
1313
* [输出格式](mapper/out.md)
14+
* [参数校验](mapper/validate.md)
1415

1516
## 注解类使用
1617

docs/zh/mapper/validate.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## 参数校验
2+
3+
### 使用 validate 方法进行参数校验
4+
5+
`Serialize` 类提供了一个 `validate` 方法,当通过 `from` 方法创建对象时,该方法会被自动调用。我们可以在 `validate` 方法中实现自定义的数据验证逻辑。
6+
7+
下面是一个使用示例:
8+
9+
```php
10+
use Astral\Serialize\Serialize;
11+
12+
class TestConstructValidationFromSerialize extends Serialize
13+
{
14+
public string $type_string;
15+
16+
/**
17+
* 数据验证方法
18+
* 在对象通过 from 方法创建后自动调用
19+
*/
20+
public function validate(): void
21+
{
22+
// 验证 type_string 属性的值
23+
if ($this->type_string !== '123') {
24+
throw new Exception('type_string 必须等于 123');
25+
}
26+
27+
// 可以修改属性值
28+
$this->type_string = '234';
29+
}
30+
}
31+
```
32+
33+
### 在构造函数中进行参数校验
34+
35+
当直接使用 `__construct` 方法创建对象时,可以在构造函数中实现参数验证逻辑。但需要注意的是,这种方式只能访问构造函数中定义的属性,无法访问其他属性。
36+
37+
```php
38+
use Astral\Serialize\Serialize;
39+
40+
class TestConstructFromSerialize extends Serialize
41+
{
42+
// 注意:这个属性在构造函数中无法访问
43+
// 如果需要验证此属性,请使用 validate 方法
44+
public string $not_validate_string;
45+
46+
/**
47+
* 构造函数中的参数验证
48+
* @param string $type_string 输入的字符串参数
49+
*/
50+
public function __construct(
51+
public string $type_string,
52+
) {
53+
// 验证传入的参数
54+
if ($this->type_string !== '123') {
55+
throw new Exception('type_string 必须等于 123');
56+
}
57+
58+
// 修改属性值
59+
$this->type_string = '234';
60+
}
61+
}

0 commit comments

Comments
 (0)