Download the PHP package ashallendesign/type-safe without Composer
On this page you can find all versions of the php package ashallendesign/type-safe. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ashallendesign/type-safe
More information about ashallendesign/type-safe
Files in ashallendesign/type-safe
Package type-safe
Short Description A package for ensuring variable types in PHP
License MIT
Homepage https://github.com/ash-jc-allen/type-safe
Informations about the package type-safe
Table of Contents
- Overview
- Installation
- Usage
- Simple Checks
- Advanced Checks
- Custom Checks
- Skipping Checks
- Testing
- Security
- Contribution
- Credits
- Changelog
- License
Overview
Type Safe is a lightweight package that you can use in your PHP projects to ensure your variables' types.
If you're interested in reading about why I made this package, check out this blog post.
Installation
You can install the package via Composer:
The package has been developed and tested to work with the following minimum requirements:
- PHP 8.0
Usage
Simple Checks
Validating that a property is an integer:
Validating that a property is a string:
Validating that a property is a boolean:
Validating that a property is a closure:
Validating that a property is an object:
Validating that a property is an array:
Validating that a property is an associative array:
Advanced Checks
Validating that a property is an object of a specific class:
Validating that a property is an array containing specific fields:
Validating that a property is an associative array containing specific fields:
Custom Checks
You might want to use your own custom checks that aren't provided in the package by default. To do this, you can create your own class that implements the AshAllenDesign\TypeSafe\Check
interface.
The interface enforces two methods: passes()
and message()
. The passes()
method is used to define your logic that determines if the field is the correct type. The message()
method is used to return the message that will be passed to the thrown exception if the validation fails.
For example, if we wanted to create a custom check to assert that our field was a Laravel Collection
that only contained User
models, it might look something like this:
We could then use that check like so:
`
Skipping Checks
There may be times when you don't want to run the type checks. For example, you might want to disable them in production environments and only run them in local, testing and staging environments. To skip the checks, you can simply use the skipChecks
like shown in the example below:
Helpers Methods
There are three different ways that you can use the package to add type safe checks to your code.
The first method is by using the TypeSafe
object itself like so:
Alternatively, you can use the safe()
helper function that achieves the same thing as the code above. You can use the helper function like so:
The TypeSafe
also includes helper methods that you can use for all the simple checks. The example shows how you can validate an integer field:
Testing
To run the tests for the package, you can use the following command:
Security
If you find any security related issues, please contact me directly at [email protected] to report it.
Contribution
If you wish to make any changes or improvements to the package, feel free to make a pull request.
To contribute to this library, please use the following guidelines before submitting your pull request:
- Write tests for any new functions that are added. If you are updating existing code, make sure that the existing tests pass and write more if needed.
- Follow PSR-12 coding standards.
- Make all pull requests to the branch.
Credits
Changelog
Check the CHANGELOG to get more information about the latest changes.
License
The MIT License (MIT). Please see License File for more information.