selfphp/php-typecheck

Runtime type validation for arrays and structured data. in PHP

Installs: 2

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/selfphp/php-typecheck

v1.1.0 2025-05-28 07:58 UTC

This package is auto-updated.

Last update: 2025-11-28 09:02:28 UTC


README

PhpTypeCheck is a lightweight PHP library for validating runtime types in arrays and nested data structures.
It helps ensure that incoming data (e.g., from APIs, forms, or dynamic sources) matches expected scalar or object types.

๐Ÿš€ Features

  • โœ… Validate array elements against scalar or object types
  • ๐Ÿ” Support for recursive (nested) arrays
  • ๐Ÿงฉ Validate associative structures with assertStructure()
  • ๐Ÿ” Optional keys with key? syntax
  • ๐Ÿง  Descriptive error messages with full path and actual/expected types
  • ๐Ÿงพ Soft validation via checkArrayOfType() and checkStructure()
  • ๐Ÿงช Type inspection via describeType()
  • ๐Ÿ“ค Structured error output with TypeCheckException::toArray()
  • ๐Ÿงช Fully tested with PHPUnit
  • ๐ŸŽฏ Framework-agnostic (works in Symfony, Laravel, Slim, or plain PHP)

๐Ÿ“ฆ Installation

composer require selfphp/php-typecheck

Requirements

  • PHP >= 8.1
  • Composer

This library uses modern PHP 8.1 features like readonly properties.

โœจ Basic Usage

Validate flat arrays

use Selfphp\PhpTypeCheck\TypeChecker;

TypeChecker::assertArrayOfType([1, 2, 3], 'int');       // โœ… OK
TypeChecker::assertArrayOfType(['a', 'b'], 'string');   // โœ… OK

Validate arrays of objects

TypeChecker::assertArrayOfType([new User(), new User()], User::class); // โœ… OK

Recursive validation

$data = [[1, 2], [3, 4]];
TypeChecker::assertArrayOfType($data, 'int', true); // โœ… OK

Fails with meaningful error

TypeChecker::assertArrayOfType([1, 'two', 3], 'int');
// โŒ Throws TypeCheckException: Element at [1] is of type string, expected int

โœ… Soft validation (no exceptions)

checkArrayOfType()

if (!TypeChecker::checkArrayOfType([1, 'two'], 'int')) {
    echo "Invalid array values!";
}

checkStructure()

$data = ['email' => 'test@example.com'];
$schema = ['email' => 'string', 'phone?' => 'string'];

if (!TypeChecker::checkStructure($data, $schema)) {
    echo "Invalid structure!";
}

๐Ÿงฉ Validate structured arrays

TypeChecker::assertStructure(
    ['name' => 'Alice', 'age' => 30],
    ['name' => 'string', 'age' => 'int']
);

// with nested structures and optional keys
TypeChecker::assertStructure(
    ['profile' => ['city' => 'Berlin'], 'email' => 'a@example.com'],
    ['profile' => ['city' => 'string'], 'email?' => 'string']
);

๐Ÿ“ค Structured error reporting

If a TypeCheckException is thrown, you can convert it to a machine-readable format:

try {
    TypeChecker::assertArrayOfType([1, 'x'], 'int');
} catch (TypeCheckException $e) {
    echo json_encode($e->toArray(), JSON_PRETTY_PRINT);
}
{
  "message": "Element at [1] is of type string, expected int",
  "path": "1",
  "expected": "int",
  "actual": "string"
}

๐Ÿงช Describe value types

TypeChecker::describeType(42);                         // int
TypeChecker::describeType(['a', 'b']);                 // array<string>
TypeChecker::describeType([1, 'x']);                   // array<int|string>
TypeChecker::describeType([new User(), new User()]);  // array<User>

๐Ÿ›  Roadmap

  • assertStructure(array $data, array $schema) for complex key/type validation
  • checkArrayOfType() and checkStructure() for soft validation
  • Structured error reporting via toArray()
  • Type parser support for array<string, User>
  • Optional integration with static analysis tools (Psalm, PHPStan)

๐Ÿ“„ License

MIT ยฉ2025 SELFPHP