prophit / account-tree
Prophit中用于层次化账户的树实现
dev-main
2023-11-20 17:04 UTC
Requires
- php: ^8.1
- loophp/phptree: ^2.6
- prophit/core: dev-main
Requires (Dev)
- pestphp/pest: ^2.0
- phpstan/phpstan: ^1.9
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2024-09-20 18:54:14 UTC
README
Prophit中用于层次化账户的树实现。
在MIT许可证下发布[MIT License]。
安装
composer require prophit/account-tree
账户
prophit/core
为账户提供了一个Account
实体类,但它没有账户形成层次结构的概念。
<?php use Prophit\Core\Account\Account; $account = new Account( 'ID-GOES-HERE', 'NAME-GOES_HERE', );
这个库扩展了该Account
类,以支持账户之间的父子关系。为此,它添加了一个构造参数$parentId
,它应该取对应父账户的Account
实例的$id
属性值。
<?php use Prophit\AccountTree\Account; // The $parentId constructor parameter is not specified here and defaults to null, // which is used to indicate that the account does not have a parent. $parentAccount = new Account('PARENT-ID', 'Parent Account'); // The $parentId constructor parameter is specified here to indicate that this // account is a child of the above parent account. $childAccount = new Account('CHILD-ID', 'Child Account', $parentAccount->getId()); // The $parentId constructor parameter is specified here to indicate that this // account is a child of the above child account. $grandchildAccount = new Account('GRANDCHILD-ID', 'Grandchild Account', $childAccount->getId());
树
这个库提供了一个AccountTree
类,它将一个iterable
集合的Account
实例作为其唯一的构造参数。`AccountTree`是可迭代的,并在内部使用loophp/phptree
执行前序遍历。
当`AccountTree`遍历账户时,它会返回一个具有额外属性的账户实例,该属性表示账户在树中的深度。可以使用账户实例的`getDepth()`方法检索此深度。
这些功能结合在一起,对于在用户界面中可视化表示账户列表非常有用。
<?php // From the earlier example: // $parentAccount = ... // $childAccount = ... // $grandchildAccount = ... use Prophit\AccountTree\AccountTree; $tree = new AccountTree([ $parentAccount, $childAccount, $grandchildAccount, ]); foreach ($tree as $account) { echo str_repeat(' ', $account->getDepth()), $account->getName(), PHP_EOL; } // Output: // Parent Account // Child Account // Grandchild Account