PHPackages                             stingus/phpdt - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. stingus/phpdt

AbandonedLibrary[Utility &amp; Helpers](/categories/utility)

stingus/phpdt
=============

PHP data type validation: primitives (int, double, bool, array, string) and objects

0.1.0(9y ago)047MITPHPPHP &gt;=5.6

Since Oct 7Pushed 9y ago1 watchersCompare

[ Source](https://github.com/stingus/phpdt)[ Packagist](https://packagist.org/packages/stingus/phpdt)[ Docs](https://github.com/stingus/phpdt)[ RSS](/packages/stingus-phpdt/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (3)Versions (2)Used By (0)

PHPdt
=====

[](#phpdt)

This library implements strict data type validation for PHP.

[![Build Status](https://camo.githubusercontent.com/820cb12bca047cb836cc75395635bd2a56e265887cbcf8e546fb45ef88750dc3/68747470733a2f2f7472617669732d63692e6f72672f7374696e6775732f70687064742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/stingus/phpdt)[![Code Climate](https://camo.githubusercontent.com/995a33397b84c6dbd0472cf613d0fa0254934021533be9b2f12dd6f014694ec5/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f7374696e6775732f70687064742f6261646765732f6770612e737667)](https://codeclimate.com/github/stingus/phpdt)[![Test Coverage](https://camo.githubusercontent.com/1daaecef53b72259ea62ad81cc224653f8ba7e13ad9585dd85a82b4eaab81dea/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f7374696e6775732f70687064742f6261646765732f636f7665726167652e737667)](https://codeclimate.com/github/stingus/phpdt/coverage)

Supported data types
--------------------

[](#supported-data-types)

- Primitives: integer, double, boolean, string, array
- User-defined classes and interfaces

Installation
------------

[](#installation)

Install via composer:

```
php composer.phar require stingus/phpdt
```

Tests
-----

[](#tests)

You can run the test suite using:

```
vendor/bin/phpunit
```

Documentation
-------------

[](#documentation)

You can generate [PHPDoc](https://www.phpdoc.org/), which will create a `phpdoc` directory containing HTML API documentation.

```
php phpDocumentor.phar
```

Usage
-----

[](#usage)

### Primitives

[](#primitives)

```
use PHPdt\DataType\Exceptions\InvalidDataTypeException;
use PHPdt\DataType\IntDataType;

class Foo
{
    private $dataType;

    private $data;

    public function __construct($dataType)
    {
        $this->dataType = $dataType;
    }

    // Set data after validating it has the strict type $this->dataType;
    public function setData($data)
    {
        try {
            $dataType = $this->dataType;
            $dataType::validateType($data);
            $this->data = $data;
            return true;
        } catch (InvalidDataTypeException $e) {
            return false;
        }
    }
}

// Build Foo with Integer data validation
// You can choose from 5 predefined primitives: integer, double, string, bool, array
$foo = new Foo(IntDataType::class);
$foo->setData(1); // True
$foo->setData('1'); // False
```

### User-defined objects

[](#user-defined-objects)

```
use PHPdt\DataType\DataTypeInterface;
use PHPdt\DataType\Exceptions\InvalidDataTypeException;

// User-defined class must implement DataTypeInterface
class Bar implements DataTypeInterface
{
    public static function validateType($value)
    {
        if ($value instanceof Bar) {
            return true;
        }
        return false;
    }
}

class Foo
{
    private $dataType;

    private $data;

    public function __construct($dataType)
    {
        $this->dataType = $dataType;
    }

    public function setData($data)
    {
        // $data is the validator
        if ($data instanceof DataTypeInterface) {
            try {
                $data::validateType($this->dataType);
                $this->data = $data;
                return true;
            } catch (InvalidDataTypeException $e) {
                return false;
            }
        }
        return false;
    }
}

// Build Foo with Bar data validation
$foo = new Foo(Bar::class);
$foo->setData(new Bar()); // True
$foo->setData(new stdClass()); // False
```

### Traits usage

[](#traits-usage)

There are two traits that can be used as helpers in user-defined classes:

#### ObjectValidationTrait

[](#objectvalidationtrait)

This trait implements the validateType() method from DataTypeInterface. It helps building a validation model based on parent classes and interfaces. Suppose you want to validate that an object is implementing a certain interface or is a child of a concrete or abstract class. Using this trait makes validation a breeze:

```
use PHPdt\DataType\DataTypeInterface;
use PHPdt\DataType\Exceptions\InvalidDataTypeException;
use PHPdt\DataType\ObjectValidationTrait;

interface Qux
{
}

abstract class Baz implements Qux, DataTypeInterface
{
    use ObjectValidationTrait;
}

class Bar extends Baz
{
}

class Foo
{
    private $dataType;

    private $data;

    public function __construct($dataType)
    {
        $this->dataType = $dataType;
    }

    public function setData($data)
    {
        // Now $data is the validator
        if ($data instanceof DataTypeInterface) {
            try {
                $data::validateType($this->dataType);
                $this->data = $data;
                return true;
            } catch (InvalidDataTypeException $e) {
                return false;
            }
        }
        return false;
    }
}

// Validation on interface
$foo = new Foo(Qux::class);
$foo->setData(new Bar()); // True, Bar implements Qux

// Validation on abstract
$foo = new Foo(Baz::class);
$foo->setData(new Bar()); // True, Bar extends Baz
```

#### ValidationTypeTrait

[](#validationtypetrait)

This trait helps implementing the validation method in the client objects. You don't have to worry about implementing the correct validation for primitives or used-defined classes, just use the validateType() method from this trait:

```
use PHPdt\DataType\DataTypeInterface;
use PHPdt\DataType\Exceptions\DataTypeImplementationException;
use PHPdt\DataType\Exceptions\InvalidDataTypeException;
use PHPdt\DataType\ObjectValidationTrait;
use PHPdt\DataType\ValidationTypeTrait;

class Baz
{
}

class Bar implements DataTypeInterface
{
    use ObjectValidationTrait;
}

class Foo
{
    use ValidationTypeTrait;

    private $dataType;

    private $data;

    public function __construct($dataType)
    {
        $this->dataType = $dataType;
    }

    public function setData($data)
    {
        try {
            self::validateType($data, $this->dataType);
            $this->data = $data;
            return true;
        } catch (InvalidDataTypeException $e) {
            // Data type mismatch
            return false;
        } catch (DataTypeImplementationException $e) {
            // $dataType (for primitives) or $data (for user-defined classes) is not implementing DataTypeInterface
            return false;
        }
    }
}

$foo = new Foo(Bar::class);
$foo->setData(new Bar()); // True

$foo = new Foo(Baz::class);
$foo->setData(new Baz()); // False, Baz is not implementing DataTypeInterface
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3508d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b17595d4e52dd64b26dea3bcc87bd7df72483b3fe730e2ccad91d70df12a64c3?d=identicon)[stingus](/maintainers/stingus)

---

Tags

typearraystringobjectintegerDoublestrictboolean

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/stingus-phpdt/health.svg)

```
[![Health](https://phpackages.com/badges/stingus-phpdt/health.svg)](https://phpackages.com/packages/stingus-phpdt)
```

###  Alternatives

[symfony/property-access

Provides functions to read and write from/to an object or array using a simple string notation

2.8k295.3M2.5k](/packages/symfony-property-access)[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k394.3M1.5k](/packages/nette-utils)[cuyz/valinor

Dependency free PHP library that helps to map any input into a strongly-typed structure.

1.5k9.2M108](/packages/cuyz-valinor)[consistence/consistence

Consistence - consistent approach and additions to PHP's functionality

1831.1M18](/packages/consistence-consistence)[phootwork/lang

Missing PHP language constructs

1224.8M8](/packages/phootwork-lang)[michaldudek/foundation

A set of useful PHP classes.

13111.9k13](/packages/michaldudek-foundation)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
