Validated properties in PHP8.1 and above using attribute rules

Overview

Banner

PHP Validated Properties

GitHub Build Status Scrutinizer Code Quality Code Coverage PHP Version Support

Add Rule attributes to your model properties to make sure they are valid.

Why this package?

When validating external data coming from either a Request, an import or an API, common PHP packages allow you to validate that incoming data and give you that data back in an unstructured way. With this package you can directly add validation rules to your structured models instead;

#[Url]
protected string $url;

#[Between(1, 100)]
protected int $nrOfItems;

#[Email]
protected string $email;

When a property is set to a value that doesn't adhere to these rules, a ValidationException will be thrown. (Which can be handled application specifically)

Setup

To get up and running, simply run;

composer install prinsfrank/php-validated-properties

Creating a validated model

And extend the base model in PrinsFrank\PhpStrictModels\Model;

<?php

use PrinsFrank\PhpStrictModels\Model;

class ValidatedModel extends Model {

}
If your models already extend another base model, you can also use the WithValidatedProperties trait instead
<?php

use PrinsFrank\PhpStrictModels\WithValidatedProperties;
use Illuminate\Database\Eloquent\Model;

class ValidatedModel extends Model {
    use WithValidatedProperties;
}

Adding validated properties

To add validation rules to properties, add them as attributes to protected and private properties:

<?php

use PrinsFrank\PhpStrictModels\Model;
use PrinsFrank\PhpStrictModels\Rule\Between;
use PrinsFrank\PhpStrictModels\Rule\Url;

class ValidatedModel extends Model {
    #[Between(1,5)]
    protected int $nrOfItems;
    
    #[Url]
    protected string $url;
}

Due to how PHP works internally, it is not possible to validate public properties, so models extending the validated model or using the WithValidatedProperties trait will throw an exception when they contain public properties with Rule attributes.

PHPStan support

To let PHPStan understand this package and to enable it to prevent any errors that can be detected using static analysis, a PHPStan plugin is included in this package. To add it to your phpstan.neon, simply add the following line to your includes section:

includes:
    - vendor/prinsfrank/php-validated-properties/development/PHPStan/extension.neon
You might also like...
[READ-ONLY] Properties define model metadata.

Charcoal Property Properties define object's metadata. They provide the building blocks of the Model's definition. Properties are defined globally for

Test and enforce architectural rules in your Laravel applications. Keep your app's architecture clean and consistent!
Test and enforce architectural rules in your Laravel applications. Keep your app's architecture clean and consistent!

Laravel Arkitect Laravel Arkitect lets you test and enforce your architectural rules in your Laravel applications, and it's a PHPArkitect wrapper for

Rules to detect game engines and other technologies based on Steam depot file lists

SteamDB File Detection Rule Sets This is a set of scripts that are used by SteamDB to make educated guesses about the engine(s) & technology used to b

Set of rules for PHP_CodeSniffer and PHP-CS-Fixer used by Symplify projects.

20+ Coding Standard checkers for PHP projects with focus on Clean Architecture

Rector upgrades rules for Doctrine
Rector upgrades rules for Doctrine

Rector Rules for Doctrine See available Doctrine rules Install This package is already part of rector/rector package, so it works out of the box.

A Pocketmine-MP (PMMP) plugin to help staff members enforce the rules of the server.
A Pocketmine-MP (PMMP) plugin to help staff members enforce the rules of the server.

StaffMode is an all-in-one Pocketmine-MP (PMMP) moderation plugin made to simplify the life of staff members.

Custom PHPStan rules

phpstan-rules Provides additional rules for phpstan/phpstan. Installation Run $ composer require --dev alister/phpstan-rules Usage All of the rules pr

Rector Rules for BEAR.Sunday

Rector Rules for BEAR.Sunday The rector/rector rules for BEAR.Sunday.

ExtDN PHP_CodeSniffer rules for Magento 2

ExtDN PHP_CodeSniffer rules for Magento 2 Introduction There are already many PHP CodeSniffer rules out there to aid in Magento 2 development: Magento

Comments
  • Scrutinizer fails to parse Enums

    Scrutinizer fails to parse Enums

    https://scrutinizer-ci.com/g/PrinsFrank/php-validated-properties/issues/main/files/src/Enum/Type.php?orderField=path&order=asc&honorSelectedPaths=0

    maybe it runs on lower PHP version .... https://github.com/PrinsFrank/php-validated-properties/blob/06459f83412a8b860b587805608a786724218f0f/composer.json#L7

    opened by szepeviktor 0
Releases(v0.0.2)
  • v0.0.2(Feb 14, 2022)

    What's Changed

    • Create .gitattributes by @imanghafoori1 in https://github.com/PrinsFrank/php-validated-properties/pull/2 Thanks @imanghafoori1 !
    • The PHPStan extension is now not included anymore in this package but an external package (prinsfrank/php-validated-properties-phpstan), so package size is as small as possible on non dev environments.

    New Contributors

    • @imanghafoori1 made their first contribution in https://github.com/PrinsFrank/php-validated-properties/pull/2

    Full Changelog: https://github.com/PrinsFrank/php-validated-properties/compare/v0.0.1...v0.0.2

    Source code(tar.gz)
    Source code(zip)
Owner
Hi! I'm Frank Prins, A senior PHP developer specialized in Security.
null
Envbar allows you to differentiate between environments by adding a custom colored bar above the top navigation.

Envbar Envbar allows you to differentiate between environments by adding a custom colored bar above the top navigation. This should help backend users

Magenizr 6 Oct 7, 2022
Phpcs-magento-rules - A set of PHPCS rules used by made.com when hacking Magento

Made.com PHPCS Magento Rules A set of PHPCS rules used by made.com when hacking Magento. Pre-Requisites PHPCS Installation Short Version Clone this re

Made.com Tech Team 26 Jun 3, 2020
All PHP functions, rewritten to throw exceptions instead of returning false, now for php8

A set of core PHP functions rewritten to throw exceptions instead of returning false when an error is encountered.

TheCodingMachine 106 Nov 21, 2022
DBML parser for PHP8. It's a PHP parser for DBML syntax.

DBML parser written on PHP8 DBML (database markup language) is a simple, readable DSL language designed to define database structures. This page outli

Pavel Buchnev 32 Dec 29, 2022
JSON <=> PHP8+ objects serialization / deserialization library

A simple library for JSON to PHP Objects conversions Often times, we interact with an API, or data source that returns JSON. PHP only offers the possi

Square 90 Dec 20, 2022
Joole Reflector - used to work with the properties of objects, their changes and merges

Joole Reflector allows you to change protected, as well as private properties of an object.

Ravil Sitdikov 1 May 7, 2022
Magento 2 extension. Strekoza_GoogleCategory. Add Category Attribute (field) - "Google Category"

Magento 2 extension. Strekoza_GoogleCategory Magento 2 extension. Add Category Attribute (field) - "Google Category" This exstension will add Category

Alex S 1 Feb 7, 2022
Handles ActiveRecord's attribute translations

TranslateableBehavior for Yii2 This behavior has been inspired by the great work of Mikehaertl's Translatable Behavior for Yii 1.*. It eases the trans

2amigOS! Consulting Group 67 May 21, 2022
PHPStan extension to support #[Readonly] constructor properties

icanhazstring/phpstan-readonly-property Support #[Readonly] promoted constructor properties for PHPStan. This library is used to have a full transitio

Andreas Frömer 4 Apr 5, 2022
A small package to add computed properties to any PHP class. 🐘

Computed Properties This package provides a trait and attribute that can provide computed property support. Installation This package can be installed

Ryan Chandler 22 Mar 28, 2022