Recommend this page to a friend! |
![]() |
Info | Documentation | ![]() |
![]() |
![]() |
Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
Not enough user ratings | Total: 28 | All time: 11,197 This week: 40![]() |
Version | License | PHP version | Categories | |||
strongtype-php 1.0 | MIT/X Consortium ... | 7 | Data types, PHP 7 |
Description | Author | |
This package implements scalar types with stricter validation rules. |
|
Strong types for your PHP code.
| 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 |
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();
}
}
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.*
* PHP 7.4
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);
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/)
StrongType PHP is licensed under the MIT License.
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
![]() |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.