tdn/php-types

This package is abandoned and no longer maintained. The author suggests using the typing/types package instead.

Php primitive wrappers lib.

2.0.1 2015-09-22 09:30 UTC

README

Dependency Status GitHub issues Total Downloads License Latest Stable Version Scrutinizer Code Quality Travis Coverage Status SensioLabsInsight

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
Doctrine Collections & Doctrine Inflector Doctrine Stringy
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.