smoren / containers-transactional
摘要:事务性容器和数据结构
v0.1.3
2021-12-23 16:29 UTC
Requires
- php: >=7.4
- ext-json: *
- smoren/containers: ^0.1
- smoren/extended-exceptions: ^1.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-24 16:39:14 UTC
README
摘要:提供对象编辑事务接口的包装结构。
包括某些数据容器和结构的事务性包装实现。
如何将依赖项安装到您的项目中
composer require smoren/containers-transactional
单元测试
composer install
./vendor/bin/codecept build
./vendor/bin/codecept run unit tests/unit
与您的类一起使用
类定义
use Smoren\Containers\Transactional\Base\TransactionWrapper; use Smoren\Containers\Transactional\Base\Validator; use Smoren\Containers\Transactional\Interfaces\ValidationRuleInterface; /** * Your class that you want to wrap with transactional interface */ class YourClass { /** * Example attribute * @var int */ public $someAttribute = 1; // ... } /** * @see Smoren\Containers\Transactional\Structs\TLinkedList */ class TYourClass extends TransactionWrapper { /** * This overriding is needed only to specify method's return type * @inheritDoc */ public function getWrapped(): YourClass { return parent::getWrapped(); } /** * Unnecessary override if there is not default value of YourClass * @inheritDoc */ protected function getDefaultWrapped() { return new YourClass(); } /** * @inheritDoc * @return string */ protected function getWrappedType(): ?string { return YourClass::class; } /** * Unnecessary override if you are not going to use validation * @inheritDoc * @return Validator * @throws \Smoren\ExtendedExceptions\LogicException */ protected function getDefaultValidator(): ?Validator { return (new Validator()) ->addRule('validation_rule_alias', MyValidationRule::class); } } /** * This declaration is not necessary if you are not going to use validation * @see Smoren\Containers\Transactional\Tests\Unit\Utility\PositiveNumberValidationRule */ class MyValidationRule implements ValidationRuleInterface { protected array $errors = []; /** * @inheritDoc */ public function validate($data, $owner): bool { // ... return true; } /** * @inheritDoc */ public function getErrors() { return $this->errors; } }
客户端代码
use Smoren\Containers\Transactional\Exceptions\ValidationException; $yourObject = new YourClass(); $tWrapper = new TYourClass($yourObject); echo $yourObject->someAttribute; // output: 1 echo $tWrapper->getWrapped()->someAttribute; // output: 1 $tWrapper->interact(function(YourClass $tmpObjectState) { $tmpObjectState->someAttribute = 2; }); echo $yourObject->someAttribute; // output: 1 try { $tWrapper->validate(); // validate only echo $yourObject->someAttribute; // output: 1 } catch(ValidationException $e) { print_r($e->getErrors()); // output: ['validation_rule_alias' => [...]] } try { $tWrapper->apply(); // validate and apply echo $yourObject->someAttribute; // output: 2 } catch(ValidationException $e) { print_r($e->getErrors()); // output: ['validation_rule_alias' => [...]] }
可用的事务性容器
TArray
使用事务接口包装PHP数组。
use Smoren\Containers\Transactional\Structs\TArray; use Smoren\Containers\Transactional\Exceptions\ValidationException; $ta = new TArray([1, 2, 3]); print_r($ta->getWrapped()); // output: [1, 2, 3] $ta->interact(function(array &$arr) { array_push($arr, 4); array_push($arr, 5); $arr[0] = 0; }); try { print_r($ta->getWrapped()); // output: [1, 2, 3] $ta->apply(); print_r($ta->getWrapped()); // output: [0, 2, 3, 4, 5] } catch(ValidationException $e) { // if validation failed }
TLinkedList
带有事务接口的链表数据结构的经典实现。
包装LinkedList容器。
use Smoren\Containers\Transactional\Structs\TLinkedList; use Smoren\Containers\Transactional\Exceptions\ValidationException; use Smoren\Containers\Structs\LinkedList; $tll = new TLinkedList([1, 2, 3]); print_r($tll->getWrapped()->toArray()); // output: [1, 2, 3] $tll->interact(function(LinkedList $list) { $list->pushBack(4); $list->pushBack(5); }); try { print_r($tll->getWrapped()->toArray()); // output: [1, 2, 3] $tll->apply(); print_r($tll->getWrapped()->toArray()); // output: [1, 2, 3, 4, 5] } catch(ValidationException $e) { // if validation failed }
TMappedCollection
具有事务接口的类似映射的数据结构。
包装MappedCollection容器。
use Smoren\Containers\Transactional\Structs\TMappedCollection; use Smoren\Containers\Transactional\Exceptions\ValidationException; use Smoren\Containers\Structs\MappedCollection; $tmc = new TMappedCollection([ '0' => ['id' => 0], ]); print_r($tmc->getWrapped()->toArray()); // output: ['0' => ['id' => 0]] $tmc->interact(function(MappedCollection $collection) { $collection ->add('1', ['id' => 1]) ->add('2', ['id' => 2]) ->delete('2'); }); try { print_r($tmc->getWrapped()->toArray()); // output: ['0' => ['id' => 0]] $tmc->apply(); print_r($tmc->getWrapped()->toArray()); // output: ['0' => ['id' => 0], ['1' => ['id' => 1]]] } catch(ValidationException $e) { // if validation failed }
TMappedLinkedList
按ID映射并具有事务接口的链表。
包装MappedLinkedList容器。
use Smoren\Containers\Transactional\Structs\TMappedLinkedList; use Smoren\Containers\Transactional\Exceptions\ValidationException; use Smoren\Containers\Structs\MappedLinkedList; $tmll = new TMappedLinkedList( new MappedLinkedList([1 => 11]) ); print_r($tmll->getWrapped()->toArray()); // output: [1 => 11] $tmll->interact(function(MappedLinkedList $list) { $list->pushBack(2, 22); $list->pushBack(3, 33); }); try { print_r($tmll->getWrapped()->toArray()); // output: [1 => 11] $tmll->apply(); print_r($tmll->getWrapped()->toArray()); // output: [1 => 11, 2 => 22, 3 => 33] } catch(ValidationException $e) { // if validation failed }
TSortedLinkedList
带有预处理排序和事务接口的链表。
包装SortedLinkedList容器。
use Smoren\Containers\Transactional\Structs\TSortedLinkedList; use Smoren\Containers\Transactional\Exceptions\ValidationException; use Smoren\Containers\Structs\SortedLinkedList; /** * Class IntegerSortedLinkedList */ class IntegerSortedLinkedList extends SortedLinkedList { /** * @inheritDoc */ protected function getComparator(): callable { return function(int $lhs, int $rhs) { return $lhs > $rhs; }; } } $tsll = new TSortedLinkedList( new IntegerSortedLinkedList([4, 1, 2]) ); print_r($tsll->getWrapped()->toArray()); // output: [1, 2, 4] $tsll->interact(function(IntegerSortedLinkedList $list) { $list->insert(5); $list->insert(3); }); try { print_r($tsll->getWrapped()->toArray()); // output: [1, 2, 4] $tsll->apply(); print_r($tsll->getWrapped()->toArray()); // output: [1, 2, 3, 4, 5] } catch(ValidationException $e) { // if validation failed }
TSortedMappedLinkedList
具有预处理排序、映射和事务接口的链表。
use Smoren\Containers\Transactional\Structs\TSortedMappedLinkedList; use Smoren\Containers\Transactional\Exceptions\ValidationException; use Smoren\Containers\Structs\SortedMappedLinkedList; $tsmll = new TSortedMappedLinkedList( new SortedMappedLinkedList([2 => -2, 1 => -1, 4 => -4]) ); print_r($tsmll->getWrapped()->toArray()); // output: [1 => -1, 2 => -2, 4 => -4] $tsmll->interact(function(SortedMappedLinkedList $list) { $list->insert(3, 0); }); try { print_r($tsmll->getWrapped()->toArray()); // output: [1 => -1, 2 => -2, 4 => -4] $tsmll->apply(); print_r($tsmll->getWrapped()->toArray()); // output: [1 => -1, 2 => -2, 3 => 0, 4 => -4] } catch(ValidationException $e) { // if validation failed }