Skip to content

Commit fbe8af0

Browse files
committed
test docs
1 parent 6265c71 commit fbe8af0

File tree

5 files changed

+172
-1
lines changed

5 files changed

+172
-1
lines changed

.gitbook.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
root: docs
2+
structure:
3+
langs:
4+
- label: 简体中文
5+
path: zh
6+
- label: English
7+
path: en

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Languages
66

77
- [Complete documen-English](./docs/en/README.md)
8-
- [完整文档-中文](./docs/zh/README.md)
8+
- [完整文档-中文](https://astrals-organization.gitbook.io/php-serialize)
99

1010
# php-serialize
1111
An advanced PHP serialization tool leveraging attributes for flexible object-to-array and JSON conversion. Supports property aliases, type conversions, and nested object handling. Ideal for APIs, data persistence, and configuration management.

docs/en/SUMMARY.md

Whitespace-only changes.

docs/zh/DTO 转换/类型转换.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
### 类型转换
2+
3+
#### 基本类型转换
4+
5+
##### 方式一:构造函数属性提升
6+
7+
```php
8+
use Astral\Serialize\Serialize;
9+
10+
class Profile extends Serialize {
11+
public function __construct(
12+
public string $username,
13+
public int $score,
14+
public float $balance,
15+
public bool $isActive
16+
) {}
17+
}
18+
```
19+
20+
##### 方式二:传统属性定义
21+
22+
```php
23+
use Astral\Serialize\Serialize;
24+
25+
class Profile extends Serialize {
26+
public string $username;
27+
public int $score;
28+
public float $balance;
29+
public bool $isActive;
30+
}
31+
32+
// 两种方式都支持相同的类型转换
33+
$profile = Profile::from([
34+
'username' => 123, // 整数转换为字符串
35+
'score' => '100', // 字符串转换为整数
36+
'balance' => '99.99', // 字符串转换为浮点数
37+
'isActive' => 1 // 数字转换为布尔值
38+
]);
39+
40+
// 转换为数组
41+
$profileArray = $profile->toArray();
42+
```
43+
44+
##### 方式三:只读属性
45+
46+
```php
47+
use Astral\Serialize\Serialize;
48+
49+
class Profile extends Serialize {
50+
public readonly string $username;
51+
public readonly int $score;
52+
public readonly float $balance;
53+
public readonly bool $isActive;
54+
55+
// 手动初始化
56+
public function __construct(
57+
string $username,
58+
int $score,
59+
float $balance,
60+
bool $isActive
61+
) {
62+
$this->username = $username;
63+
$this->score = $score;
64+
$this->balance = $balance;
65+
$this->isActive = $isActive;
66+
}
67+
}
68+
```
69+
70+
无论使用哪种方式,`Serialize` 类都能正常工作,并提供相同的类型转换和序列化功能。

docs/zh/快速开始.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
## 快速开始
2+
3+
### 安装
4+
5+
使用 Composer 安装:
6+
7+
```bash
8+
composer require astral/serialize
9+
```
10+
11+
### 基本用法
12+
13+
```php
14+
use Astral\Serialize\Serialize;
15+
16+
class User extends Serialize {
17+
public string $name,
18+
public int $age
19+
}
20+
21+
// 从数组创建对象
22+
$user = User::from([
23+
'name' => '张三',
24+
'age' => 30
25+
]);
26+
27+
// 访问对象属性
28+
echo $user->name; // 输出: 张三
29+
echo $user->age; // 输出: 30
30+
31+
// 转换为数组
32+
$userArray = $user->toArray();
33+
// $userArray 的内容:
34+
// [
35+
// 'name' => '张三',
36+
// 'age' => 30
37+
// ]
38+
```
39+
40+
### 其他特性
41+
42+
#### **不可变性**:只读属性在构造后无法修改
43+
44+
```php
45+
use Astral\Serialize\Serialize;
46+
47+
class User extends Serialize {
48+
public function __construct(
49+
public readonly string $name,
50+
public readonly int $age
51+
) {}
52+
}
53+
54+
$user = User::from([
55+
'name' => '张三',
56+
'age' => 30
57+
]);
58+
59+
try {
60+
$user->name = '李四'; // 编译时错误:无法修改只读属性
61+
} catch (Error $e) {
62+
echo "只读属性不能被重新赋值";
63+
}
64+
```
65+
66+
#### **类型安全的初始化**
67+
68+
```php
69+
$user = User::from([
70+
'name' => 123, // 整数会被转换为字符串
71+
'age' => '35' // 字符串会被转换为整数
72+
]);
73+
74+
echo $user->name; // 输出: "123"
75+
echo $user->age; // 输出: 35
76+
```
77+
78+
#### **构造函数初始化**
79+
80+
```php
81+
use Astral\Serialize\Serialize;
82+
83+
class User extends Serialize {
84+
public function __construct(
85+
public readonly string $name,
86+
public readonly int $age
87+
) {
88+
// 可以在构造函数中添加额外的验证或处理逻辑
89+
if (strlen($name) < 2) {
90+
throw new \InvalidArgumentException('名称太短');
91+
}
92+
}
93+
}
94+
```

0 commit comments

Comments
 (0)