Download the PHP package flashios09/php-union-types without Composer
On this page you can find all versions of the php package flashios09/php-union-types. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download flashios09/php-union-types
More information about flashios09/php-union-types
Files in flashios09/php-union-types
Informations about the package php-union-types
Requirements
- PHP 7.1+
- Composer
Installation
Usage
UnionTypes::assert
Throw a TypeError if the given value isn't in the passed union type.
See the full list of the valid types $types
string array, e.g. ['int', 'string', Posts::class, ...]
.
Examples:
-
✓ should pass
-
✖ will throw a TypeError "'1.2' must be of the union type
int|float
,string
given" -
✓ should pass
-
Check for
instance of
, added on1.0.2
✓ should pass
✖ will throw a TypeError "object(Time)" must be of the union type
\DateTime|string
,Time
given"
UnionTypes::is
Check if the value type is one of the passed types.
Works just like UnionTypes::assert($value, $types)
but it will return a bool(true
/false
) instead of throwing a TypeError.
See the full list of the valid types $types
string array, e.g. ['int', 'string', Posts::class, ...]
.
Examples:
-
equivalent to
is_int(1.2) || is_float(1.2)
✓ return
true
-
equivalent to
is_int('1.2') || is_float('1.2')
✖ return
false
-
equivalent to
is_int('1.2') || is_float('1.2') || is_string('1.2')
✓ return
true
-
Check for
instance of
, added on1.0.2
✓ return
true
✖ return
false
UnionTypes::assertFuncArg
Throw a TypesError if the value of the argument isn't in the union type.
See the full list of the valid types $types
string array, e.g. int
, string
, Posts::class
...
Examples:
-
✓ should pass
-
✖ will throw TypeError "Argument
a
passed toMath::add(int|float $a, ...)
must be of the union typeint|float
,string
given" -
✓ should pass
-
Check for
instance of
, added on1.0.2
✓ should
pass
✖ will throw a TypeError "object(Time)" must be of the union type
\DateTime|string
,Time
given"
Valid types:
'string'
'int'
(not'integer'
or'double'
)'float'
(not'double'
or'decimal'
)'bool'
(not'boolean'
)'null'
(not'NULL'
)'array'
- Any valid classname string, e.g
Table::class
or'Cake\ORM\Table'
resource
Configuration
-
Friendly editor file path:
By default, every thrown Error or Exception will have a called in at the end of the message
called in #{stackTraceIndex} {file}:{line}
, e.g.called in #0 /path/to/app/src/Controller/PostsController.php:126
.To have a friendly editor(VSCode, Atom, SublimeText, ...) file path relative to the workspace, you need to define a constant
UnionTypes.PATH_TO_APP
somewhere, e.g:Now the
/path/to/app/
prefix will be removed from the called in, e.g.called in #0 src/Controller/PostsController.php:126
.
Contributing
Installation
git clone [email protected]:flashios09/php-union-types.git
cd php-union-types
composer install
(will install dev dependencies likewhoops
,kahlan
,var-dumper
,cakephp-codesniffer
)
Start a local php server with livereload
yarn install
(node dependencies)yarn serve
(then add your code tophp-union-types/index.php
and open a browser window athttp://localhost:3080
)
Start a local kahlan test with livereload
yarn install
(node dependencies)yarn test
(it will watch any change in thespec
folder and re-launch the test)
Linting
composer cs-check
composer cs-fix
Running test
composer test
License
This project is licensed under the MIT License.