PHP Classes

PHP Strong Types: Scalar types with stronger validation rules

Recommend this page to a friend!

  Author Author  
Picture of Smoren  Freelight
Name: Smoren Freelight <contact>
Classes: 20 packages by
Country: Russian Federation Russian Federation
Innovation award
Innovation award
Nominee: 9x


  Detailed description   Download Download .zip .tar.gz  
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

Details

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.


  Classes of Smoren Freelight  >  PHP Strong Types  >  Download Download .zip .tar.gz  >  Support forum Support forum  >  Blog Blog  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: PHP Strong Types
Base name: strongtype-php
Description: Scalar types with stronger validation rules
Version: -
PHP version: 7
License: MIT/X Consortium License
 
  Groups   Applications   Files Files  

  Groups  
Group folder image Data types Modeling and manipulating data types View top rated classes
Group folder image PHP 7 Classes using PHP 7 specific features View top rated classes


  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  Files folder image Files  
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  /  src  
File Role Description
Files folder imageException (1 file)
Files folder imageInteger (6 files)

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

  Files folder image Files  /  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  /  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  /  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

Download Download all files: strongtype-php.tar.gz strongtype-php.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
  Files folder image Files  
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  /  src  
File Role Description
Files folder imageException (1 file)
Files folder imageInteger (6 files)

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

  Files folder image Files  /  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  /  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  /  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

Download Download all files: strongtype-php.tar.gz strongtype-php.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.