decodelabs / tightrope
Reusable constraints for your PHP classes
Installs: 16 594
Dependents: 3
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-12-04 22:11:39 UTC
README
Reusable constraints for your PHP classes.
Tightrope is a middleware package that provides library developers with a set of reusable semantic constraints.
Get news and updates on the DecodeLabs blog.
Installation
composer require decodelabs/tightrope
Usage
Tightrope defines a number of reusable interfaces that represent a property of an object, such as being disableable, readable or requirable:
namespace DecodeLabs\Tightrope; interface Nullable { public function isNullable(): bool; }
It also supplies a manifest version of each interface along with a trait implementation:
namespace DecodeLabs\Tightrope\Manifest; use DecodeLabs\Tightrope\Nullable as StaticNullable; interface Nullable extends StaticNullable { public function setNullable(bool $flag): static; }
You can use these interfaces to define properties of your objects:
namespace My\Project; use DecodeLabs\Exceptional; use DecodeLabs\Tightrope\Manifest\Nullable; use DecodeLabs\Tightrope\Manifest\NullableTrait; class MyClass implements Nullable { use NullableTrait; public function doSomething(?string $value): void { if( !$this->isNullable() && $value === null ) { throw Exceptional::InvalidArgument('Value cannot be null'); } // ... } } $myObject = new MyClass(); $myObject->setNullable(true); $myObject->doSomething(null); // Fine $myObject->setNullable(false); $myObject->doSomething(null); // Not fine
Available properties
The following interfaces are currently available:
- Disableable
- Immutable
- Nullable
- Readable
- ReadOnlyable
- Requirable
- Writable
Licensing
Tightrope is licensed under the MIT License. See LICENSE for the full license text.