gnbit / alabra-entity
实体类
0.1
2021-12-30 21:10 UTC
Requires
- php: ^7 || ^8
Requires (Dev)
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^7.5
README
Alabra Entity 是一个PHP包,提供了一种灵活且可扩展的方式来处理对象集合。它包含一个实现了常见数组接口的 EntityCollection
类,如 ArrayAccess
、IteratorAggregate
和 Countable
。这允许轻松地操作和遍历对象集合。
安装
您可以通过 Composer 安装此包
composer require alabra/alabra-entity
用法
基本用法
创建实体
class UserProfile implements EntityInterface { use EntityTrait; private string $username; private PersonalInfo $personalInfo; public function __construct( private string $firstName, private string $lastName, private string $email, private int $age, private bool $isSubscribed ) { } public function setUsername(string $username): void { $this->username = $username; } public function setPersonalInfo(PersonalInfo $personalInfo): void { $this->personalInfo = $personalInfo; } }
$personalInfo = new PersonalInfo('123 Main St', '555-1234'); $this->userProfile = new UserProfile('Jhon', 'Smith', 'test@example.com', 40, true); $this->userProfile->setUsername('other'); $this->userProfile->setPersonalInfo($personalInfo); //Methods $this->userProfile->toArray(); $this->userProfile->toJson(); $this->userProfile->getIterator();
创建集合
use Alabra\Entity\EntityCollection; // Create an instance of EntityCollection $collection = new EntityCollection(); // Add objects to the collection $object1 = new YourEntityClass(); $object2 = new YourEntityClass(); $collection[] = $object1; $collection[] = $object2; // Access objects by key or iterate through the collection foreach ($collection as $key => $object) { // Do something with each object } // Check if an offset exists if (isset($collection['some_key'])) { // Object with key 'some_key' exists } // Remove an object by key unset($collection['some_key']); // Get the number of objects in the collection $count = count($collection);
高级用法
使用键属性
如果您的对象具有应作为集合键使用的特定属性,您可以使用 setKeyPropertyName 方法来设置它
use Alabra\Entity\EntityCollection; // Create an instance of EntityCollection $collection = new EntityCollection(); // Set the key property name $collection->setKeyPropertyName('id'); // Add objects to the collection $object1 = new YourEntityClass('A', 'B', 1, true, 1.5, 'key1'); $object2 = new YourEntityClass('C', 'D', 2, false, 2.5, 'key2'); $collection[] = $object1; $collection[] = $object2; // Access objects by their key property $objectByKey = $collection['key1']; // Returns $object1
合并集合
您可以通过键属性或简单合并来合并两个集合
use Alabra\Entity\EntityCollection; // Create two instances of EntityCollection $collection1 = new EntityCollection(); $collection2 = new EntityCollection(); // Merge collections $collection1->merge($collection2->toArray());
将集合转换为数组
您可以将集合及其对象轻松转换为关联数组
use Alabra\Entity\EntityCollection; // Create an instance of EntityCollection $collection = new EntityCollection(); // Add objects to the collection $object1 = new YourEntityClass('A', 'B', 1, true, 1.5); $object2 = new YourEntityClass('C', 'D', 2, false, 2.5); $collection[] = $object1; $collection[] = $object2; // Convert the collection to an array $arrayRepresentation = $collection->toArray();
许可证
此包是开源软件,许可协议为MIT。