PHP code example of smoren / containers-transactional
1. Go to this page and download the library: Download smoren/containers-transactional 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/ */
smoren / containers-transactional example snippets
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;
}
}