Download the PHP package zheltikov/php-type-assert without Composer

On this page you can find all versions of the php package zheltikov/php-type-assert. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-type-assert

php-type-assert

A type checking/assertion library for PHP, for more compact and secure code.

Installation

As this is a Composer package, you can install it via:

Usage

This library exposes three functions in the \Zheltikov\TypeAssert namespace:

Example

You may use these functions as follows:

Below comes a simple explanation for each function:

is_(mixed, string): bool

Parameters:

Checks whether the value in $value has the type specified in $type, and returns a boolean result.

as_(mixed, string): mixed

Parameters:

Performs the same checks as is_. However, it throws TypeAssertionException if the value has a different type. If the type matches, it returns the original value.

null_as_(mixed, string): mixed|null

Parameters:

Similar to as_, but which returns null if the type does not match.

Supported types and type interpretation

Here comes a list of the supported types and how they are checked internally:

Type Checked with Notes
'bool', 'boolean' is_bool()
'int', 'integer', 'long' is_int()
'float', 'double', 'real' is_float()
'string' is_string()
array is_array()
object is_object()
callable is_callable()
iterable is_iterable()
'resource' is_resource()
'null' $value === null
'nonnull', 'notnull' $value !== null
'countable' is_countable()
'numeric' is_numeric()
'scalar' is_scalar()
'num', 'number' is_int() or is_float() Useful for custom math functions and libraries.
'mixed', 'dynamic', 'any' always true
'void', 'nothing' always false
'arraykey' is_int() or is_string() Useful for checking whether a value is valid for indexing an array.
?<some_other_type> $value === null or is_($value, 'some_other_type') This is also called "Nullable type".
The value can be either null or of the type specified after.
For example: ?string refers to either null or a string.
!<some_other_type> when is_($value, 'some_other_type') is false This is called a "Negated type".
Useful for checking that a value is not of a certain type.
For example: !array refers to any type except array.
<class_or_interface_name> $value instanceof 'class_or_interface_name' Useful for checking that a value is an class instance that has a certain member (parent class or interface) in its hierarchy.
'classname' is_string($value) and class_exists($value) To check whether a given value is a valid class name.
'interfacename' is_string($value) and interface_exists($value) To check whether a given value is a valid interface name.
'traitname' is_string($value) and trait_exists($value) To check whether a given value is a valid trait name.
'empty' empty($value)
'nonempty', 'notempty' when empty($value) is false
'char' when is_string() and strlen($value) === 1
'Stringish' when is_string(), otherwise. see Notes. This type refers to values that are strings or are string-convertible objects; those that provide the __toString() method.
For PHP 8.0, a value is also 'Stringish' if the following is true: $value instanceof \Stringable.
'true' $value === true
'false' $value === false
'positive' $value > 0
'nonpositive', 'notpositive' $value <= 0
'negative' $value < 0
'nonnegative', 'notnegative' $value >= 0

TODO

Performance

You may ask about the performance impact of this parsing process on the overall request. You will be surprised hearing that the performance of the parser included in this library is actually pretty good.

Some tests were made, in which the following type string was being parsed:

This test was performed on PHP v7.4 and PHP v8.0 with JIT compilation enabled, and the result aren't bad at all!:

ms. taken to parse PHP v7.4 PHP v8.0 (with JIT)
type parser checks 5 0.1
equivalent checks with ifs 0.15 0.0001

Note: these performance tests were made at commit d9fedd23..., therefore they may not be accurate for the latest library version.


All versions of php-type-assert with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4
zheltikov/php-exceptions Version ^1.0
zheltikov/php-invariant Version ^1.1
myclabs/php-enum Version ^1.8
tmilos/lexer Version ^1.0
zheltikov/php-memoize Version ^2.0
ext-json Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package zheltikov/php-type-assert contains the following files

Loading the files please wait ....