PHP Classes

PHP Strong Types: Scalar types with stronger validation rules

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 28 All time: 11,158 This week: 560Up
Version License PHP version Categories
strongtype-php 1.0MIT/X Consortium ...7Data types, PHP 7
Description 

Author

This package implements scalar types with stricter validation rules.

It provides classes implementing types that can work like scalar types like integers with specific validation rules.

Currently, it implements types like:

- Integer

- Negative integer

- Non-negative integer

- Non-positive integer

- Non-zero integer

- Positive integer

Picture of Smoren  Freelight
  Performance   Level  
Name: Smoren Freelight <contact>
Classes: 38 packages by
Country: Russian Federation Russian Federation
Age: 35
All time rank: 280978 in Russian Federation Russian Federation
Week rank: 47 Up6 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 16x

Documentation

StrongType PHP

Strong types for your PHP code.

Quick Reference

Integers

| StrongType | Description | Details | | ----------- | ----------- | ----------- | | NegativeInt | Negative integer| < 0 | | NonnegativeInt | Nonnegative integer| >= 0 | | NonpositiveInt | Nonpositive integer| <= 0 | | NonzeroInt | Nonzero integer| < 0 or > 0 | | PositiveInt | Positive integer| > 0 |

Quick Overview

PHP has basic scalar types. But even with them, you often find yourself writing repetitive validations on them.

class Person
{
    private string $name;
    private int    $age;
    private array  $hobbies;

    public function __construct(string $name, int $age, array $hobbies)
    {
        if (strlen($name) === 0) {
            throw new \RuntimeException('Name cannot be empty');
        }
        if ($age < 0) {
            throw new \RuntimeException('Age cannot be negative');
        }
        foreach ($hobbies as $hobby) {
            if (!is_string($hobby)) {
                throw new \RuntimeException('Hobbies must be strings');
            }
        }
        $this->name    = $name;
        $this->age     = $age;
        $this->hobbies = $hobbies;
    }
}

StrongTypes allow you to write cleaner, safer, self-documenting code with built-in validations.

class Person
{
    private string $name;
    private int    $age;
    private array  $hobbies;

    public function __construct(NonemptyString $name, NonnegativeInt $age, ArrayOfStrings $hobbies)
    {
        $this->name    = $name->getValue();
        $this->age     = $age->getValue();
        $this->hobbies = $hobbies->getValue();
    }
}

Setup

Add the library to your composer.json file in your project:

{
  "require": {
      "markrogoyski/strongtype-php": "0.*"
  }
}

Use composer to install the library:

$ php composer.phar install

Composer will install StrongType inside your vendor folder. Then you can add the following to your .php files to use the library with Autoloading.

require_once __DIR__ . '/vendor/autoload.php';

Alternatively, use composer on the command line to require and install StrongType:

$ php composer.phar require markrogoyski/strongtype-php:0.*

Minimum Requirements

* PHP 7.4

Usage

Integer

use StrongType\Integer\{NegativeInt, NonnegativeInt, NonpositiveInt, NonzeroInt, PositiveInt};

$positiveInt = new PositiveInt(5);
$negativeInt = new NegativeInt(-5);

$nonnegativeInt = new NonnegativeInt(4);
$nonpositiveInt = new NonpositiveInt(0);

$nonzeroInt = new NonzeroInt(5);

Standards

StrongType PHP conforms to the following standards:

* PSR-1 - Basic coding standard (http://www.php-fig.org/psr/psr-1/) * PSR-4 - Autoloader (http://www.php-fig.org/psr/psr-4/) * PSR-12 - Extended coding style guide (http://www.php-fig.org/psr/psr-12/)

License

StrongType PHP is licensed under the MIT License.


  Files folder image Files (19)  
File Role Description
Files folder imagesrc (2 directories)
Files folder imagetests (3 files, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file Makefile Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (19)  /  src  
File Role Description
Files folder imageException (1 file)
Files folder imageInteger (6 files)

  Files folder image Files (19)  /  src  /  Exception  
File Role Description
  Plain text file StrongTypeException.php Class Class source

  Files folder image Files (19)  /  src  /  Integer  
File Role Description
  Plain text file Integer.php Class Class source
  Plain text file NegativeInt.php Class Class source
  Plain text file NonnegativeInt.php Class Class source
  Plain text file NonpositiveInt.php Class Class source
  Plain text file NonzeroInt.php Class Class source
  Plain text file PositiveInt.php Class Class source

  Files folder image Files (19)  /  tests  
File Role Description
Files folder imageInteger (5 files)
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Accessible without login Plain text file coding_standard.xml Data Auxiliary data
  Accessible without login Plain text file phpunit.xml Data Auxiliary data

  Files folder image Files (19)  /  tests  /  Integer  
File Role Description
  Plain text file NegativeIntTest.php Class Class source
  Plain text file NonnegativeIntTest.php Class Class source
  Plain text file NonpositiveIntTest.php Class Class source
  Plain text file NonzeroIntTest.php Class Class source
  Plain text file PositiveIntTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:28
This week:0
All time:11,158
This week:560Up