edgaralexanderfr/php-types

Library intended to expand PHP's data types and useful data structures as well as strong types consistency in the code of target projects.

1.6.0 2024-08-22 00:40 UTC

This package is not auto-updated.

Last update: 2024-09-19 01:01:43 UTC


README

View last release PHP 8.3.0 Composer

PHP Types is a small library/framework aimed to improve and encourage strong typing across your project's codebase, consisting of basic array-types, useful data structures and more, that are not natively available by the language itself.

Table of contents 📖
  1. Requirements
  2. Installation
  3. Usage

Requirements

  1. PHP 8.3.0 or major
  2. Composer (optional)
  3. Have an initted Composer project (optional)

Installation

Install PHP Types via Composer:

composer require edgaralexanderfr/php-types

or:

You can always download the library as .zip file, decompress it, store it somewhere in your target project and include the autoload.php file from the library's project root:

curl -L -o php-types-master.zip https://github.com/edgaralexanderfr/php-types/archive/refs/heads/master.zip \
&& unzip php-types-master.zip \
&& rm php-types-master.zip

or:

You can download the packed .phar version of the library:

mkdir lib \
&& curl -L -o lib/php-types.phar https://github.com/edgaralexanderfr/php-types/raw/master/lib/php-types.phar
<?php

declare(strict_types=1);

include 'lib/php-types.phar';

use function PHPTypes\Primitive\string_array;

$message = string_array('Hello', 'world', '!');
print_r($message);

Usage

Primitive types

ObjectType

<?php

declare(strict_types=1);

include 'vendor/autoload.php';

use PHPTypes\Primitive\object_t;

use function PHPTypes\Primitive\object;

function display(object_t $object): void
{
    echo "{$object->name}:" . PHP_EOL;
    echo $object->json() . PHP_EOL;
}

$object = object([
    'name' => 'Ford Mustang GT',
    'brand' => 'Ford',
    'category' => 'Muscle Car',
    'gas' => 0.8,
    'engine' => object([
        'type' => 'V8',
        'rpm' => 750,
    ]),
    'transmission' => object([
        'type' => 'manual',
        'status' => 1,
        'gears' => ['R', 'N', '1', '2', '3', '4', '5', '6'],
    ]),
]);

display($object);
php examples/object.php
Ford Mustang GT:
{"name":"Ford Mustang GT","brand":"Ford","category":"Muscle Car","gas":0.8,"engine":{"type":"V8","rpm":750},"transmission":{"type":"manual","status":1,"gears":["R","N","1","2","3","4","5","6"]}}

BoolArray

<?php

declare(strict_types=1);

include 'vendor/autoload.php';

use PHPTypes\Primitive\bool_array_t;

use function PHPTypes\Primitive\bool_array;

function display(bool_array_t $array): void
{
    foreach ($array as $value) {
        echo $value . PHP_EOL;
    }
}

$array = bool_array(false, true, false);
display($array);
php examples/bool_array.php

1

IntArray

<?php

declare(strict_types=1);

include 'vendor/autoload.php';

use PHPTypes\Primitive\int_array_t;

use function PHPTypes\Primitive\int_array;

function display(int_array_t $array): void
{
    foreach ($array as $value) {
        echo $value . PHP_EOL;
    }
}

$array = int_array(1, 2, 3);
display($array);
php examples/int_array.php
1
2
3

FloatArray

<?php

declare(strict_types=1);

include 'vendor/autoload.php';

use PHPTypes\Primitive\float_array_t;

use function PHPTypes\Primitive\float_array;

function display(float_array_t $array): void
{
    foreach ($array as $value) {
        echo $value . PHP_EOL;
    }
}

$array = float_array(0.1, 2.3, 4.5);
display($array);
php examples/float_array.php
0.1
2.3
4.5

StringArray

<?php

declare(strict_types=1);

include 'vendor/autoload.php';

use PHPTypes\Primitive\string_array_t;

use function PHPTypes\Primitive\string_array;

function display(string_array_t $array): void
{
    foreach ($array as $value) {
        echo $value . PHP_EOL;
    }
}

$array = string_array('🍎', '🍊', '🍌');
display($array);
php examples/string_array.php
🍎
🍊
🍌

ObjectArray

<?php

declare(strict_types=1);

include 'vendor/autoload.php';

use PHPTypes\Primitive\object_array_t;

use function PHPTypes\Primitive\object;
use function PHPTypes\Primitive\object_array;

function display(object_array_t $array): void
{
    foreach ($array as $value) {
        print_r($value);
    }
}

$array = object_array(
    object([
        'id' => 1,
        'name' => 'Charles Babbage',
    ]),
    object([
        'id' => 2,
        'name' => 'Alan Turing',
    ]),
    object([
        'id' => 3,
        'name' => 'Edsger Dijkstra',
    ]),
);

display($array);
php examples/object_array.php
PHPTypes\Primitive\object_t Object
(
    [id] => 1
    [name] => Charles Babbage
)
PHPTypes\Primitive\object_t Object
(
    [id] => 2
    [name] => Alan Turing
)
PHPTypes\Primitive\object_t Object
(
    [id] => 3
    [name] => Edsger Dijkstra
)

Data structures

HashSet

<?php

declare(strict_types=1);

include 'vendor/autoload.php';

use PHPTypes\Set\HashSet;

$set = new HashSet();
$set->add(1);
$set->add('two');
$set->add(3);
$set->add(3);
$set->add('two');
$set->add('one');

foreach ($set as $value) {
    echo $value . PHP_EOL;
}
php examples/hash_set.php
1
two
3
one

IntHashSet

<?php

declare(strict_types=1);

include 'vendor/autoload.php';

use PHPTypes\Set\IntHashSet;

$set = new IntHashSet();
$set->add(1);
$set->add(2);
$set->add(3);
$set->add(3);
$set->add(2);
$set->add(1);
$set->add(0);

foreach ($set as $value) {
    echo $value . PHP_EOL;
}
php examples/int_hash_set.php
1
2
3
0

StringHashSet

<?php

declare(strict_types=1);

include 'vendor/autoload.php';

use PHPTypes\Set\StringHashSet;

$set = new StringHashSet();
$set->add('🍎');
$set->add('🍊');
$set->add('🍌');
$set->add('🍌');
$set->add('🥭');

foreach ($set as $value) {
    echo $value . PHP_EOL;
}
php examples/string_hash_set.php
🍎
🍊
🍌
🥭

Type errors

In case you attempt to assign a value with a type different than an array's type, a TypeError is thrown:

<?php

declare(strict_types=1);

include 'vendor/autoload.php';

use function PHPTypes\Primitive\int_array;

$array = int_array(1, 2, 3);

try {
    $array[2] = '🥭';
} catch (TypeError $e) {
    echo $e->getMessage() . PHP_EOL;
}

try {
    $array[] = '🥭';
} catch (TypeError $e) {
    echo $e->getMessage() . PHP_EOL;
}

$array[] = 4;

print_r($array);
php examples/type_error.php
Element must be of type integer, string given, called
Element must be of type integer, string given, called
PHPTypes\Primitive\int_array_t Object
(
    [type:protected] => integer
    [storage:ArrayIterator:private] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

)

hasAny() and isEmpty() methods

Arrays contain useful methods that help to determine whether if they're empty or contain at least 1 element in them:

<?php

declare(strict_types=1);

include 'vendor/autoload.php';

use function PHPTypes\Primitive\object_array;
use function PHPTypes\Primitive\string_array;

$fruits = string_array('🥭');

if ($fruits->hasAny()) {
    echo '`$fruits` contains at least 1 element.' . PHP_EOL;
}

$objects = object_array();

if ($objects->isEmpty()) {
    echo '`$objects` is an empty array.' . PHP_EOL;
}
php examples/has_any_is_empty.php
`$fruits` contains at least 1 element.
`$objects` is an empty array.