Lädt...


🔧 How to validate constructor arguments when using constructor property promotion


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

I have written a post about the php 8.4 property hooks. Where I didn't understand what it did.

And I added validation code examples that have nothing to do with it. So I did rewrite the post. But I didn't want to lose the valid example code. And that is why this post exists.

What is the desired result?

We want to create a user by first and last name. The name parts should not be empty because we assume everyone has a first and last name.

Validation with a class method

class User
{
    public function __construct(private string $firstName, private string $lastName) {
        $this->validate($firstName, 'firstName');
        $this->validate($lastName, 'lastName');
    }

    public function validate(string $value, $argumentName) {
        if (strlen($value) === 0) {
            throw new ValueError("$argumentName must be non-empty");
        }
   }
}

This is the method I use when there are one-off validations

Validation with trait

trait Validation {
    private function notEmpty(mixed $value, $argumentName) {
        if(is_string($value) && strlen($value) === 0) {
            throw new ValueError("$argumentName must be non-empty");
        }
    }

}

class User
{
    use Validation;

    public function __construct(private string $firstName, private string $lastName) {
        $this->notEmpty($firstName, 'firstName');
        $this->notEmpty($lastName, 'lastName');
    }
}

This is the method that I use to centralize validation methods, because there are patterns that are recurring.

Validation with attributes

When your code has a lifecycle event in place that can process attributes, or you use a package like laravel-data. This is how the code might look like.

use Spatie\LaravelData\Data;

class User extends Data
{
    public function __construct(
      #[Min(1)]
      private string $firstName, 
      #[Min(1)]
      private string $lastName
    ) {}
}

When I have multiple cases with multiple repeating patterns, then this is the method I use.

...

🔧 How to validate constructor arguments when using constructor property promotion


📈 97.57 Punkte
🔧 Programmierung

🔧 PHP 8 News: Constructor Property Promotion


📈 44.95 Punkte
🔧 Programmierung

🔧 A Beginner’s Guide to Constructor Property Promotion in PHP


📈 44.95 Punkte
🔧 Programmierung

🔧 This week's API highlights round-up: Property, Schools Around Property and property details


📈 37.57 Punkte
🔧 Programmierung

📰 Practical Data Science: Promotion Effectiveness and Promotion Planning—Part 3


📈 32.88 Punkte
🔧 AI Nachrichten

🔧 Tech notes 01 - Default Constructor and User-Defined Constructor in Java


📈 31.97 Punkte
🔧 Programmierung

🕵️ Medium CVE-2021-40892: Validate color project Validate color


📈 31.71 Punkte
🕵️ Sicherheitslücken

🔧 NSW Property Price: AI Assisted Property Analysis


📈 25.05 Punkte
🔧 Programmierung

🔧 Object Property Shorthand: When the property name and variable name are the same, you can omit the value.


📈 25.05 Punkte
🔧 Programmierung

🕵️ Medium CVE-2018-7319: Os property real estate project Os property real estate


📈 25.05 Punkte
🕵️ Sicherheitslücken

🐧 Arguments that speak for using Linux and FOSS in the company


📈 20.78 Punkte
🐧 Linux Tipps

🐧 What are some compelling arguments AGAINST using Linux for personal/professional use?


📈 20.78 Punkte
🐧 Linux Tipps

🔧 Using 2 arguments in float


📈 20.78 Punkte
🔧 Programmierung

🐧 Any reasonable arguments for/against using NTFS for a data drive on Linux?


📈 20.78 Punkte
🐧 Linux Tipps

🔧 👾 Using Arguments in Bash Scripts


📈 20.78 Punkte
🔧 Programmierung

🔧 How to pass the arguments of Makefile.PL using cpanm


📈 20.78 Punkte
🔧 Programmierung

🔧 What is the purpose of using super constructor with props argument in React?


📈 19.94 Punkte
🔧 Programmierung

🎥 Ethereum Smart Contract Backdoored Using Malicious Constructor


📈 19.94 Punkte
🎥 IT Security Video

🎥 Ethereum Smart Contract Backdoored Using Malicious Constructor


📈 19.94 Punkte
🎥 IT Security Video

🔧 Validate Gender using Regular Expressions


📈 19.81 Punkte
🔧 Programmierung

🔧 Standardize and Validate Incoming Data Using Pipes in NestJS


📈 19.81 Punkte
🔧 Programmierung

🔧 Using PowerShell and Pester to validate Azure Policy syntax


📈 19.81 Punkte
🔧 Programmierung

📰 ShiftLeft helps orgs benchmark and validate the accuracy of ShiftLeft CORE using OWASP Benchmark


📈 19.81 Punkte
📰 IT Security Nachrichten

🔧 How to Validate Array of Strings using Yup


📈 19.81 Punkte
🔧 Programmierung

🎥 Migrating to SQL: Validate Migrated Objects Using SSMA (Ep. 4) | Data Exposed


📈 19.81 Punkte
🎥 Video | Youtube

🔧 How to Validate Array Data in Laravel Using Request Classes


📈 19.81 Punkte
🔧 Programmierung

🔧 How to Validate Forms in React and React Native Using Yup and Formik


📈 19.81 Punkte
🔧 Programmierung

📰 How to test and validate DNSSEC using dig command line


📈 19.81 Punkte
📰 IT Security Nachrichten

matomo