Download the PHP package smpita/typeas without Composer
On this page you can find all versions of the php package smpita/typeas. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download smpita/typeas
More information about smpita/typeas
Files in smpita/typeas
Package typeas
Short Description Guaranteed type control for PHP
License MIT
Homepage https://github.com/smpita/typeas
Informations about the package typeas
Table of Contents
- Quick Start
- Installation
- Resolving Types
- Fluent Syntax
- Array Wrapping
- Custom Exceptions
- Official Extensions
- Custom Resolvers
- Helpers
- Upgrade Guide
- Deprecations
- Testing
- Changelog
- Contributing
- Security Vulnerabilities
- Credits
- License
Quick Start
Installation
Install the package via composer:
See SIGNATURES for the full list of methods and signatures.
Resolving types
SIGNATURES#resolving
Pass a mixed variable to get a typed variable.
Defaults
To suppress throwing exceptions, provide a default.
Array Wrapping
SIGNATURES#array
By default, array() will wrap non-iterables similar to (array) $mixed instead of throwing exceptions.
Fluent Syntax
As a convenience, TypeAs supports fluent syntax.
The NonNullable and Nullable fluent classes wrap the base TypeAs methods.
In performance critical environments, the standard methods are recommended.
Basic Usage
Nullable
Chain nullable() for nullable returns.
Note: Moving between NonNullable and Nullable returns a new instance of the associated class.
Custom Resolver
Chain using() to resolve using a Custom Resolver.
Defaults
Chain default() to specify a default.
Wrapping
Copying
Accessing the config
Importing a config
Custom Exceptions
Use onError() to customize the throw message or exception.
- Exceptions must extend
Smpita\TypeAs\Exceptions\TypeAsResolutionException
Both arguments are optional.
Extensions
Extensions are created by passing a custom resolver to a function.
Official Extensions
Custom Resolvers
SIGNATURES#resolver-registration
Each base type has an associated interface located in Smpita\TypeAs\Contracts which you can implement to make your own resolvers.
Simply implement the interface, then either register the resolver or use it in the resolver method.
Interfaces
All resolvers extend the same interfaces. Non-nullable methods throw when null is returned by a resolver.
Smpita\TypeAs\Contracts\ArrayResolverSmpita\TypeAs\Contracts\BoolResolverSmpita\TypeAs\Contracts\ClassResolverSmpita\TypeAs\Contracts\FloatResolverSmpita\TypeAs\Contracts\IntResolverSmpita\TypeAs\Contracts\StringResolver
Creating Custom Resolvers
Registering Custom Resolvers
Globally
To globally register a resolver, use the associated setter method. In Laravel, it's recommended to do this in the boot method of a ServiceProvider.
Unregistering Custom Resolvers
To return to default, set the resolver to null.
Single use
Inject a resolver to use it on a per call basis.
If you registered a custom resolver then want to use a default resolver on a per call basis, pass the default resolver class.
Helpers
SIGNATURES#helpers
Helpers follow standard signature patterns.
Global
Local
Each global helper has a local counterpart you can import if the global helper collides with another global method.
Deprecations
Please see the SIGNATURES#deprecations if you encounter a breaking change.
Testing
Changelog
Please see RELEASES for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Sean Pearce
- All Contributors
License
The MIT License (MIT). Please see License File for more information.