Skip to content

Commit b1b1c44

Browse files
author
苏青安
committed
refactor(docs): 完善README
1 parent 1a02ee2 commit b1b1c44

File tree

2 files changed

+452
-49
lines changed

2 files changed

+452
-49
lines changed

README.md

Lines changed: 172 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
11
### PHP Trade Splitter
22

3-
一个灵活、可扩展的交易/利润分账组件,提供百分比、固定金额、阶梯与递归分账等内置策略,并支持注册自定义策略。
3+
<div align="center">
4+
<a href="./README.md">English</a>|<a href="./README.zh-CN.md">简体中文</a>
5+
<hr width="50%"/>
6+
</div>
7+
8+
A **flexible and extensible** trade/profit distribution component.
9+
Built-in strategies include **Percentage**, **Fixed Amount**, **Ladder**, and **Recursive**, and you can easily register **custom strategies**.
10+
11+
Perfect for multi-level commissions, e-commerce settlements, platform fees, and agent profit sharing.
12+
13+
**This project has been parsed by Zread. If you need a quick overview of the project, you can click here to view it:[Understand this project](https://zread.ai/zxc7563598/php-trade-splitter)**
414

515
---
616

7-
## 安装
17+
## ✨ Features
18+
19+
- 🔧 **Multiple Strategies** – 4 common built-in distribution modes
20+
- 🧩 **Extensible Design** – easily register your own strategy
21+
-**Simple to Use** – one static method does it all
22+
- 🧠 **Clean Architecture** – object-oriented with the Strategy pattern
23+
24+
---
25+
26+
## 📦 Installation
827

928
```bash
1029
composer require hejunjie/trade-splitter
1130
```
1231

13-
要求:PHP >= 8.1
32+
---
1433

15-
## 快速开始
34+
## 🚀 Quick Start
1635

1736
```php
1837
<?php
@@ -21,113 +40,190 @@ require __DIR__ . '/vendor/autoload.php';
2140

2241
use Hejunjie\TradeSplitter\Splitter;
2342

24-
// 百分比分账(比例之和必须为 1.0
43+
// Percentage-based split (the sum of all rates must equal 1.0)
2544
$allocations = Splitter::split(1000, [
26-
['name' => '平台', 'rate' => 0.1],
27-
['name' => '作者', 'rate' => 0.9],
45+
['name' => 'Platform', 'rate' => 0.1],
46+
['name' => 'Author', 'rate' => 0.9],
2847
], 'percentage');
2948

30-
// 将结果转换为数组
49+
// Convert to array and print results
3150
$result = array_map(fn($a) => $a->toArray(), $allocations);
3251
print_r($result);
3352
```
3453

35-
输出示例:
54+
Example output:
3655

3756
```php
3857
Array
3958
(
4059
[0] => Array
4160
(
42-
[name] => 平台
61+
[name] => Platform
4362
[amount] => 100
4463
[ratio] => 0.1
4564
)
4665

4766
[1] => Array
4867
(
49-
[name] => 作者
68+
[name] => Author
5069
[amount] => 900
5170
[ratio] => 0.9
5271
)
5372
)
5473
```
5574

56-
## 核心概念
75+
---
76+
77+
## 🧩 Core Concepts
78+
79+
| Concept | Description |
80+
| --------------------- | ------------------------------------------------------------------------------- |
81+
| **Splitter** | The main entry point — selects the strategy and performs the split |
82+
| **StrategyInterface** | Contract every strategy must implement:`split(SplitContext $context): array`|
83+
| **SplitContext** | Contains the split context (`total`and`participants`) |
84+
| **Allocation** | Represents a split result with`name`,`amount`, and`ratio`; provides`toArray()`|
5785

58-
- **Splitter**: 对外统一入口,负责选择策略并执行分账。
59-
- **StrategyInterface**: 策略接口,所有策略需实现 `split(SplitContext $context): array`
60-
- **SplitContext**: 分账上下文,包含 `total``participants`
61-
- **Allocation**: 分账结果对象,包含 `name``amount``ratio`,并提供 `toArray()` 方法。
86+
---
6287

63-
## 内置策略
88+
## 🧮 Built-in Strategies
6489

65-
### 1) 百分比分账 `percentage`
90+
### Percentage Split `percentage`
6691

67-
- 参与者参数:`name`, `rate`(0~1),所有 `rate` 之和必须等于 1.0。
68-
- 示例:
92+
Distributes amounts by percentage. The sum of all `rate` values must equal `1.0`.
6993

7094
```php
7195
$result = Splitter::split(1000, [
72-
['name' => '平台', 'rate' => 0.1],
73-
['name' => '作者', 'rate' => 0.9],
96+
['name' => 'Platform', 'rate' => 0.1],
97+
['name' => 'Author', 'rate' => 0.9],
7498
], 'percentage');
99+
100+
echo json_encode(array_map(fn($r) => $r->toArray(), $result), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL;
101+
102+
// Output:
103+
// [
104+
// {
105+
// "name": "Platform",
106+
// "amount": 100,
107+
// "ratio": 0.1
108+
// },
109+
// {
110+
// "name": "Author",
111+
// "amount": 900,
112+
// "ratio": 0.9
113+
// }
114+
// ]
75115
```
76116

77-
### 2) 固定金额分账 `fixed`
117+
---
78118

79-
- 参与者参数:`name`, `amount`(固定金额)。所有固定金额之和不得超过 `total`
80-
- 示例:
119+
### Fixed Amount Split `fixed`
120+
121+
Splits by fixed amounts. The total of all fixed values must not exceed the total amount.
81122

82123
```php
83124
$result = Splitter::split(3000, [
84-
['name' => '代理A', 'amount' => 200],
85-
['name' => '代理B', 'amount' => 300],
125+
['name' => 'Agent A', 'amount' => 200],
126+
['name' => 'Agent B', 'amount' => 300],
86127
], 'fixed');
128+
129+
echo json_encode(array_map(fn($r) => $r->toArray(), $result), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL;
130+
131+
// Output:
132+
// [
133+
// {
134+
// "name": "Agent A",
135+
// "amount": 200,
136+
// "ratio": 0.0667
137+
// },
138+
// {
139+
// "name": "Agent B",
140+
// "amount": 300,
141+
// "ratio": 0.1
142+
// }
143+
// ]
87144
```
88145

89-
> 说明:若固定金额总和超出总额,将抛出异常。
146+
---
90147

91-
### 3) 阶梯分账 `ladder`
148+
### Ladder Split `ladder`
92149

93-
- 参与者可设置阶梯规则 `ladders`(按不超过 `max` 的比例分成,`max` 为 null 表示无上限),或直接使用固定 `rate`
94-
- 将根据总金额匹配命中区间的 `rate` 进行计算。
95-
- 示例:
150+
Applies different percentage rates based on amount tiers.
151+
Use `null` for no upper limit.
96152

97153
```php
98154
$result = Splitter::split(5000, [
99155
[
100-
'name' => '代理A',
156+
'name' => 'Agent A',
101157
'ladders' => [
102158
['max' => 1000, 'rate' => 0.05],
103159
['max' => 5000, 'rate' => 0.10],
104160
['max' => null, 'rate' => 0.15],
105161
],
106162
],
107163
[
108-
'name' => '平台',
164+
'name' => 'Platform',
109165
'rate' => 0.05,
110166
],
111167
], 'ladder');
168+
169+
echo json_encode(array_map(fn($r) => $r->toArray(), $result), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL;
170+
171+
// Output:
172+
// [
173+
// {
174+
// "name": "Agent A",
175+
// "amount": 500,
176+
// "ratio": 0.1
177+
// },
178+
// {
179+
// "name": "Platform",
180+
// "amount": 250,
181+
// "ratio": 0.05
182+
// }
183+
// ]
112184
```
113185

114-
### 4) 递归分账 `recursive`
186+
---
187+
188+
### Recursive Split `recursive`
115189

116-
- 每一层的金额都基于上一层的分润金额计算,随后转换为“净收益”(自己所得减去下级从自己处分走的金额)。
117-
- 参与者参数:`name`, `rate`(0~1)。
118-
- 示例:
190+
Each level’s share is calculated based on the remaining amount from the previous level.
191+
Useful for multi-tier agent or channel commission systems.
119192

120193
```php
121194
$result = Splitter::split(10000, [
122-
['name' => '一级', 'rate' => 0.2],
123-
['name' => '二级', 'rate' => 0.2],
124-
['name' => '三级', 'rate' => 0.2],
195+
['name' => 'Level 1', 'rate' => 0.2],
196+
['name' => 'Level 2', 'rate' => 0.2],
197+
['name' => 'Level 3', 'rate' => 0.2],
125198
], 'recursive');
199+
200+
echo json_encode(array_map(fn($r) => $r->toArray(), $result), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL;
201+
202+
// Output:
203+
// [
204+
// {
205+
// "name": "Level 1",
206+
// "amount": 1600,
207+
// "ratio": 0.2
208+
// },
209+
// {
210+
// "name": "Level 2",
211+
// "amount": 320,
212+
// "ratio": 0.2
213+
// },
214+
// {
215+
// "name": "Level 3",
216+
// "amount": 80,
217+
// "ratio": 0.2
218+
// }
219+
// ]
126220
```
127221

128-
## 自定义策略
222+
---
223+
224+
## 🧠 Custom Strategy
129225

130-
实现 `Hejunjie\TradeSplitter\Contracts\StrategyInterface` 并注册:
226+
Implement the `Hejunjie\TradeSplitter\Contracts\StrategyInterface` and register it:
131227

132228
```php
133229
use Hejunjie\TradeSplitter\Contracts\StrategyInterface;
@@ -139,21 +235,48 @@ class MyStrategy implements StrategyInterface
139235
{
140236
public function split(SplitContext $context): array
141237
{
142-
// 自定义分账逻辑
143-
return [new Allocation('someone', $context->total, 1.0)];
238+
// Custom split logic
239+
// total: $context->total
240+
// participants: $context->participants
241+
return [
242+
new Allocation('someone', $context->total, 1.0)
243+
];
144244
}
145245
}
146246

147247
Splitter::registerStrategy('my_strategy', MyStrategy::class);
148-
$result = Splitter::split(123, [['name' => 'someone']], 'my_strategy');
248+
249+
// Usage
250+
$total = 1000;
251+
$participants = [];
252+
$result = Splitter::split($total, $participants, 'my_strategy');
149253
```
150254

151-
## 运行示例
255+
---
256+
257+
## 🧪 Run the Demo
152258

153-
仓库内提供 `tests/demo.php`
259+
A demo script is included in the repository:
154260

155261
```bash
156262
php tests/demo.php
157263
```
158264

159-
示例脚本包含对四种内置策略的演示。
265+
This script demonstrates all four built-in strategies.
266+
267+
---
268+
269+
## 💡 Motivation
270+
271+
This component was born from my frustration with rigid, hard-coded profit-sharing logic in various projects.
272+
I wanted a **clear, pluggable, and reusable** solution that could be integrated into any project — without reinventing the wheel each time.
273+
274+
If you’ve encountered strange or complex split scenarios in your business,
275+
feel free to collaborate and help make this tool even more powerful!
276+
277+
---
278+
279+
## 🤝 Contributing
280+
281+
Got ideas, feedback, or found a bug? PRs and Issues are always welcome 🙌
282+
If you find this project helpful, please consider giving it a ⭐ Star — that’s the biggest motivation for me to keep improving it!

0 commit comments

Comments
 (0)