2017 © Pedro Peláez
 

library php-type

A type wrapper for PHP that support generic syntax

image

tebru/php-type

A type wrapper for PHP that support generic syntax

  • Sunday, July 15, 2018
  • by natebrunette
  • Repository
  • 1 Watchers
  • 0 Stars
  • 24,737 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 7 Versions
  • 20 % Grown

The README.md

Php Type

Build Status Code Coverage Scrutinizer Code Quality, (*1)

This library wraps a type string and provides an API around getting information about the type. It also supports generic type syntax., (*2)

General Usage

The simplest way is to instantiate a new TypeToken and pass in the type., (*3)

new TypeToken('string');

The class also normalizes the type:, (*4)

$typeShort = new TypeToken('int');
$typeLong = new TypeToken('integer');

$typeShort->getRawType(); // 'integer'
$typeLong->getRawType(); // 'integer'

$typeShort->getPhpType(); // 'integer'
$typeLong->getPhpType(); // 'integer'

$typeShort->isInteger(); // true
$typeLong->isInteger(); // true

Any of the core php types are supported as well as ? which represents a wildcard type. This can be used if the type is unknown at the time the type is instantiated. All of the possible types are represented as constants on the class., (*5)

Classes also work the same, (*6)

$type = new TypeToken(\My\Foo::class);

$type->getRawType(); // 'My\Foo'
(string)$type; // 'My\Foo'
$type->getPhpType(); // 'object'
$type->isObject(); // true
$type->isA(\My\Foo::class); // true

->isA() checks the instantiated type's parent classes and interfaces in addition to the passed in class name., (*7)

You can also use generic syntax with angle brackets., (*8)

$type = new TypeToken('My\Foo<string, My\Foo2>');

$type->getRawType(); // 'My\Foo'
(string)$type; // 'My\Foo<string, My\Foo2>'
$type->getPhpType(); // 'object'
$type->isObject(); // true
$type->isA(\My\Foo::class); // true

$generics = $type->getGenerics();
(string)$generics[0]; // 'string'
(string)$generics[1]; // 'My\Foo2'

Calling ->getGenerics() will return an array of TypeToken objects., (*9)

Nested generics work the same way, (*10)

new TypeToken('array<string, array<int>>');

This could represent an array with string keys and all values are an array of integers., (*11)

If you have a variable, you can get the type using the static factory method, (*12)

TypeToken::createFromVariable($variable);

This uses the singleton method ::create() which will return the same instance on duplicate types., (*13)

The Versions

15/07 2018

dev-master

9999999-dev

A type wrapper for PHP that support generic syntax

  Sources   Download

MIT

The Requires

  • php >= 7.1

 

The Development Requires

by Nate Brunette

15/07 2018

v0.1.5

0.1.5.0

A type wrapper for PHP that support generic syntax

  Sources   Download

MIT

The Requires

  • php >= 7.1

 

The Development Requires

by Nate Brunette

24/03 2018

v0.1.4

0.1.4.0

A type wrapper for PHP that support generic syntax

  Sources   Download

MIT

The Requires

  • php >= 7.1

 

The Development Requires

by Nate Brunette

19/01 2018

v0.1.3

0.1.3.0

A type wrapper for PHP that support generic syntax

  Sources   Download

MIT

The Requires

  • php >= 7.1

 

The Development Requires

by Nate Brunette

24/07 2017

v0.1.2

0.1.2.0

A type wrapper for PHP that support generic syntax

  Sources   Download

MIT

The Requires

  • php >= 7.1

 

The Development Requires

by Nate Brunette

19/04 2017

v0.1.1

0.1.1.0

A type wrapper for PHP that support generic syntax

  Sources   Download

MIT

The Requires

  • php >= 7.1

 

The Development Requires

by Nate Brunette

19/04 2017

v0.1.0

0.1.0.0

A type wrapper for PHP that support generic syntax

  Sources   Download

MIT

The Requires

  • php >= 7.1

 

The Development Requires

by Nate Brunette