PHP code example of livaa / custom-datatypes

1. Go to this page and download the library: Download livaa/custom-datatypes library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

livaa / custom-datatypes example snippets


namespace Foo\Types;

use Livaa\CustomDatatypes\CustomDatatype;

class   EmailAddress
extends CustomDatatype
{
    function validate(): void{
    
        // - Write here the validation/normalization process.      
        // - Call $this->error("error_message"); in case of error
    }
}

$email = isset($_POST['email']) ? new EmailAddress($_POST["email"]) 
                                : throw new InvalidArgumentException("email_address_missing");

(new NewsletterManager)->subscribe($email);


use Foo\Types\EmailAddress;

class NewsletterManager
{   
    // Type hinting makes sure the methods receive what they expect.
    // The code is more reliable.

    function subscribe(EmailAddress $email_address):bool{ 
    
        // some code to save the email adress
    }

    function unsubscribe(EmailAddress $email_address):bool{

        // some code to remove the email address from the db
    }
}

$email  = new EmailAddress("[email protected]");

echo $email->getValue(); // output: [email protected]


// let's say we have an EmailAddress, a Rgb and an Energy custom datatypes

$email        = new EmailAddress("[email protected]"); 
$hair_color   = new Rgb([34,54,93]); // This one wants an array
$energy_left  = new Energy(12); // obviously this one wants an int

echo $email; 
output: [email protected]

echo $hair_color;
output: {34,54,93} 

echo $energy_left;
output: 12 as a string

// but don't be confuse !
$email == "[email protected]" -> true
$email === "[email protected]" -> false
$email->getValue() == "[email protected]"  -> true
$email->getValue() === "[email protected]" -> true

namespace Foo\Types;

use Livaa\CustomDatatypes\CustomDatatype;

class   Rgb
extends CustomDatatype
{
    function validate(): void{
    
        // ... verification process
    }
    
    /* 
     * DON'T DO THAT
     * You better use something like a Factory:
     * eg: 
     * $rgb  = new Rgb([174, 189, 216]);
     * $rgba = (new ColorsFactory)->rgbToRgba($rgb);
     */  
    function toRgba(){ // just don't
        
        //add a 4th channel
        $this->value[] = 1;
        
        return $this->value;
    }
}

use Livaa\CustomDatatypes\CustomDatatypeException;

try{

    $email = new EmailAddress("www.github.com");

catch(CustomDatatypeException $e){ // this will be triggered

    echo $e->getMessage(); 
}


use Livaa\CustomDatatypes\CustomDatatypeException;

try{

    $email = new EmailAddress("www.github.com", false); //$throw_exceptions to false
    
    if( !$email->isValid() ){

        $first_error = $email->getErrors()[0];

        echo "an error happened: ".$error->getMessage(); 
    }

catch(CustomDatatypeException $e){ // this won't be triggered

    echo $e->getMessage(); 
}