skyzyx/strong-types

This package is abandoned and no longer maintained. The author suggests using the Upgrade your PHP. package instead.

Enables strong types for PHP. This allows for tighter validation, especially when accepting input from users.

1.0.9 2015-03-03 08:00 UTC

This package is auto-updated.

Last update: 2022-02-01 12:41:42 UTC


README

Source Latest Stable Version Total Downloads Open Issues Build Status Coverage Status Code Climate Code Quality Dependency Status SensioLabsInsight HHVM Support Documentation Status License Author

Enables strong types for PHP. This allows for tighter validation, especially when accepting input from users.

Why would anyone do this? This can be useful when developing APIs, and you want to require strict types. By defining those types/shapes ahead of time as classes, you can enforce incoming/outgoing data types, but also access the native values after the vaidation step has occurred.

It intentionally rejects and avoid any kind of "type massaging". If you pass an integer to StringType, you will get an exception. All errors are thrown as Exceptions with useful error messages.

Features

  • BooleanType
  • Collection (ArrayAccess)
  • Enum
  • FloatType
  • IntegerType
  • StringType (incl. Utf8String)

DateTime is already strongly typed, so use that class for strong date/time types.

Examples

We can do simple validation enforcement, which can be valuable in PHP 5.x. (Use strict types instead in PHP 7.)

use Skyzyx\StrongTypes\StringType;

$abc = new StringType('abc');

$v123 = new StringType(123);
#=> UnexpectedValueException:
#=> The Skyzyx\StrongTypes\StringType class expects a value of type string. 
#=> Received a value of type integer instead.

$abc->getValue();
#=> 'abc'

You can also extend the base types to create more specific data types (aka, data shapes).

use Skyzyx\StrongTypes\StringType;

class FiveChars extends StringType
{
    public function __construct($s)
    {
        $this->setExactLength(5);
        parent::__construct($s);
    }
}

$abcde = new FiveChars('abcde');

$abc = new FiveChars('abc');
#=> Exception

$v12345 = new FiveChars(12345);
#=> Exception

Installation

Using Composer:

composer require skyzyx/strong-types=~2.0

And include it in your scripts:

require_once 'vendor/autoload.php';

Testing

Firstly, run composer install -o to download and install the dependencies.

You can run the tests as follows:

bin/phpunit

Contributing

Here's the process for contributing:

  1. Fork PHP Strong Types to your GitHub account.
  2. Clone your GitHub copy of the repository into your local workspace.
  3. Write code, fix bugs, and add tests with 100% code coverage.
  4. Commit your changes to your local workspace and push them up to your GitHub copy.
  5. You submit a GitHub pull request with a description of what the change is.
  6. The contribution is reviewed. Maybe there will be some banter back-and-forth in the comments.
  7. If all goes well, your pull request will be accepted and your changes are merged in.

Authors, Copyright & Licensing

See also the list of contributors who participated in this project.

Licensed for use under the terms of the MIT license.

Coding Standards

PSR-0/1/2 are a solid foundation, but are not an entire coding style by themselves. I have taken the time to document all of the nitpicky patterns and nuances of my personal coding style. It goes well-beyond brace placement and tabs vs. spaces to cover topics such as docblock annotations, ternary operations and which variation of English to use. It aims for thoroughness and pedanticism over hoping that we can all get along.

https://github.com/skyzyx/php-coding-standards