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 Easy type control for static analysis
License MIT
Homepage https://github.com/smpita/typeas
Informations about the package typeas
Easy type control for static analysis
Do you fight mixed
signatures when performing static analysis?
Smpita/TypeAs will give you easy control of your typing.
Installation
You can install the package via composer:
Usage
Please see SIGNATURES for the list of current methods and signatures.
General Usage
SIGNATURES#resolving
Pass a $mixed
and it will throw a TypeAsResolutionException
if the $mixed
can't be cast.
If you want to suppress throwing exceptions, provide a default.
The Class Method
SIGNATURES#class
class()
has a slightly different signature because you need to specify the class you are expecting.
You can still provide a default.
Note: In versions prior to v2.0.0
the signature had a different order.
The Array Method
SIGNATURES#array
By default, array()
will wrap non-iterables similar to (array) $mixed
instead of throwing exceptions.
That might not always be appropriate, so you can turn wrapping off to get exceptions.
Or you may supply a default.
Nullables
Starting in v2.3.0
if you would prefer to receive null
instead of having an exception thrown, each type method has a nullable counterpart.
Resolvers
SIGNATURES#resolver-registration
Starting in v2.4.0
you can specify your own custom resolvers.
Each 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
Smpita\TypeAs\Contracts\ArrayResolver
Smpita\TypeAs\Contracts\BoolResolver
Smpita\TypeAs\Contracts\ClassResolver
Smpita\TypeAs\Contracts\FloatResolver
Smpita\TypeAs\Contracts\IntResolver
Smpita\TypeAs\Contracts\NullableArrayResolver
Smpita\TypeAs\Contracts\NullableClassResolver
Smpita\TypeAs\Contracts\NullableFloatResolver
Smpita\TypeAs\Contracts\NullableIntResolver
Smpita\TypeAs\Contracts\NullableStringResolver
Smpita\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
.
Single use
Unregistering Custom Resolvers
To return to default, simply set the resolver to null
.
To return all resolvers to default, you can leverage the useDefaultResolvers()
.
If you registered a custom resolver and want to use the default resolver on a single use basis, passing null
to the resolver method will not work. You must pass the default resolver.
Helpers
SIGNATURES#helpers
Starting in v2.5.0
resolver methods have an associated helper method located in the Smpita\TypeAs
namespace.
The helper method names follow the TypeAs
method names, but are prepended by as
and are camelCased.
Deprecations
SIGNATURES#deprecations
Testing
Changelog
Please see CHANGELOG 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.