tdn / php-types
Php primitive wrappers lib.
Requires
- php: >=5.4.0
- danielstjules/stringy: ~2
- doctrine/inflector: ~1
- nesbot/carbon: ~1
Requires (Dev)
- bruli/php-git-hooks: ~1.3
- phpunit/phpunit: ~4.7
- satooshi/php-coveralls: ~0.6
- squizlabs/php_codesniffer: ~2.2
This package is not auto-updated.
Last update: 2021-06-10 15:51:00 UTC
README
PhpTypes
Types
- StringType
- BooleanType
- IntType*
- FloatType*
- DateTime
- Collection
* Smart use of bcmath or gmp if they are installed.
Wait, what is this?
Primitive wrappers for PHP with boxing/unboxing. (strong types even on reassignment like SPL_Types).
Uses the most popular* libs available in the PHP landscape and neatly wraps them in a single repo, providing decorators with extra features.
This is an attempt to create something close to Java core libs for PHP, unfortunately no context casting or even primitive type casting magic methods** but damn close.
* Based on opinion but backed by packagist downloads =)
** RFCs for boxing and type casting methods that never got accepted [1, 2]
Type Credits
LOL! why?!
Many reasons!
- Lack of core primitive wrappers: The underlying libraries (carbon, stringy, doctrine collections & inflector) are all extremely popular. It would be cool to have a core library built into the language with an interface like these.
- Fluent/Consistent interfaces: Our beloved PHP is infamous for flipping arguments in array and string functions.
- Because why not? Seemed fun to code.
The library is fully functional, but this is mainly a CONCEPT. Primitives will always yield higher performance than objects.
Examples/Explanation
More examples located at example.php
/** @var StringType $string */ StringType::box($string, 'foo'); echo $string; // foo $string = 'bar is fun'; //Remains boxed as instance of StringType. echo $string->dasherize(); // bar-is-fun try { //Throws "TypeError" exception. $string = false; } catch (\TypeError $e) { .... }
Documentation
Requirements
PHP 7.0 or above.
Installation
Using CLI:
php composer.phar require tdn/php-types:*@stable
In the composer.json
file:
{ "require": { "tdn/php-types": "*@stable" } }
API
Please checkout the online API or clone repo and run vendor/bin/robo documentation:build
to build local documentation.
Contributing
If you want to contribute, please read the CONTRIBUTING.
License
This library is released under the MIT license. See the complete license in the LICENSE file.