v-dem/queasy-type

Type helper classes, part of QuEasy PHP framework

dev-main 2022-12-30 18:09 UTC

This package is auto-updated.

Last update: 2024-03-29 04:27:17 UTC


README

Codacy Badge Build Status codecov Total Downloads License

QuEasy PHP Framework - Types

Package v-dem/queasy-type

Classes supporting typed "arrays" to help keep code type-safe. For example, IntArray, not just array:

function giveMeInts(IntArray $ints)
{
    ...
}

Features

Classes allowing to use typed "arrays":

  • TypedArray - Base class, implements ArrayAccess, Iterator and Countable
  • IntArray
  • StringArray
  • FloatArray
  • ResourceArray
  • ObjectArray
  • ArrayArray

Requirements

  • PHP version 5.3 or higher

Installation

composer require v-dem/queasy-type:master-dev

Usage

For example, to create an array of integer values:

$intArray = new queasy\type\IntArray([10, 20, 30]);

$intArray[] = 40;
$intArray['key'] = 50;

unset($intArray[0]); // Will remove value 10

foreach ($intArray as $key => $value) {
    echo $key . ' => ' . $value . PHP_EOL;
}

echo 'Elements count: ' . count($intArray) . PHP_EOL;

$intArray[] = 'wrong'; // Throws InvalidArgumentException

To create a specialized class representing array of users:

class UsersArray extends TypedArray
{
    public function __construct(array $items = null)
    {
        parent::__construct(app\model\User::class, $items);
    }
}