PHP code example of hejunjie / trade-splitter
1. Go to this page and download the library: Download hejunjie/trade-splitter library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hejunjie / trade-splitter example snippets
ejunjie\TradeSplitter\Splitter;
// Percentage-based split (the sum of all rates must equal 1.0)
$allocations = Splitter::split(1000, [
['name' => 'Platform', 'rate' => 0.1],
['name' => 'Author', 'rate' => 0.9],
], 'percentage');
// Convert to array and print results
$result = array_map(fn($a) => $a->toArray(), $allocations);
print_r($result);
Array
(
[0] => Array
(
[name] => Platform
[amount] => 100
[ratio] => 0.1
)
[1] => Array
(
[name] => Author
[amount] => 900
[ratio] => 0.9
)
)
$result = Splitter::split(1000, [
['name' => 'Platform', 'rate' => 0.1],
['name' => 'Author', 'rate' => 0.9],
], 'percentage');
echo json_encode(array_map(fn($r) => $r->toArray(), $result), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL;
// Output:
// [
// {
// "name": "Platform",
// "amount": 100,
// "ratio": 0.1
// },
// {
// "name": "Author",
// "amount": 900,
// "ratio": 0.9
// }
// ]
$result = Splitter::split(3000, [
['name' => 'Agent A', 'amount' => 200],
['name' => 'Agent B', 'amount' => 300],
], 'fixed');
echo json_encode(array_map(fn($r) => $r->toArray(), $result), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL;
// Output:
// [
// {
// "name": "Agent A",
// "amount": 200,
// "ratio": 0.0667
// },
// {
// "name": "Agent B",
// "amount": 300,
// "ratio": 0.1
// }
// ]
$result = Splitter::split(5000, [
[
'name' => 'Agent A',
'ladders' => [
['max' => 1000, 'rate' => 0.05],
['max' => 5000, 'rate' => 0.10],
['max' => null, 'rate' => 0.15],
],
],
[
'name' => 'Platform',
'rate' => 0.05,
],
], 'ladder');
echo json_encode(array_map(fn($r) => $r->toArray(), $result), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL;
// Output:
// [
// {
// "name": "Agent A",
// "amount": 500,
// "ratio": 0.1
// },
// {
// "name": "Platform",
// "amount": 250,
// "ratio": 0.05
// }
// ]
$result = Splitter::split(10000, [
['name' => 'Level 1', 'rate' => 0.2],
['name' => 'Level 2', 'rate' => 0.2],
['name' => 'Level 3', 'rate' => 0.2],
], 'recursive');
echo json_encode(array_map(fn($r) => $r->toArray(), $result), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL;
// Output:
// [
// {
// "name": "Level 1",
// "amount": 1600,
// "ratio": 0.2
// },
// {
// "name": "Level 2",
// "amount": 320,
// "ratio": 0.2
// },
// {
// "name": "Level 3",
// "amount": 80,
// "ratio": 0.2
// }
// ]
use Hejunjie\TradeSplitter\Contracts\StrategyInterface;
use Hejunjie\TradeSplitter\Models\SplitContext;
use Hejunjie\TradeSplitter\Models\Allocation;
use Hejunjie\TradeSplitter\Splitter;
class MyStrategy implements StrategyInterface
{
public function split(SplitContext $context): array
{
// Custom split logic
// total: $context->total
// participants: $context->participants
return [
new Allocation('someone', $context->total, 1.0)
];
}
}
Splitter::registerStrategy('my_strategy', MyStrategy::class);
// Usage
$total = 1000;
$participants = [];
$result = Splitter::split($total, $participants, 'my_strategy');
bash
php tests/demo.php