PHP code example of ryanwhitman / php-values

1. Go to this page and download the library: Download ryanwhitman/php-values 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/ */

    

ryanwhitman / php-values example snippets


public function createUser(string $email)
{
    // perform sanitation and validation on email before using
}

use RyanWhitman\PhpValues\Email;

public function createUser(Email $email)
{
    // email has already been sanitized and validated and is ready for use
}



namespace App\Values;

use RyanWhitman\PhpValues\Value;
use RyanWhitman\PhpValues\Concerns\Stringable;

class Email extends Value
{
    use Stringable;

    protected function transform(string $email): string
    {
        return filter_var($email, FILTER_SANITIZE_EMAIL);
    }

    protected function validate(string $email): bool
    {
        return filter_var($email, FILTER_VALIDATE_EMAIL);
    }
}



use App\Values\Email;

// Valid email address
Email::from('[email protected]'); // instance of Email
Email::from('[email protected]')->get(); // [email protected]
Email::getFrom('[email protected]'); // [email protected]
(string) Email::from('[email protected]'); // [email protected]
Email::isValid('[email protected]'); // true

// Valid email address (with imperfections)
Email::getFrom(' email @example.com '); // [email protected]
Email::isValid(' email @example.com '); // true

// Invalid email address
Email::from('non-email'); // throws exception
Email::tryFrom('non-email'); // null
Email::isValid('non-email'); // false

protected function transform(string $email): string
{
    return filter_var($email, FILTER_SANITIZE_EMAIL);
}

protected function validate(string $email): bool
{
    return filter_var($email, FILTER_VALIDATE_EMAIL);
}



namespace App\Values;

use RyanWhitman\PhpValues\SquishedString;
use RyanWhitman\PhpValues\Value;

class Name extends Value
{
    protected array $baseValues = [
        SquishedString::class,
    ];

    // ...
}

Email::from('[email protected]'); // instance of Email
Email::from('non-email'); // throws InvalidValueException

Email::getFrom('[email protected]'); // [email protected]
Email::getFrom('non-email'); // throws InvalidValueException

Email::tryFrom('[email protected]'); // instance of Email
Email::tryFrom('non-email'); // null

Email::tryGetFrom('[email protected]'); // [email protected]
Email::tryGetFrom('non-email'); // null

Email::isValid('[email protected]'); // true
Email::isValid('non-email'); // false

Email::from('e m [email protected]')->getOrigValue(); // e m [email protected]

Email::from('e m [email protected]')->get(); // [email protected]

use RyanWhitman\PhpValues\Annotations\ShortcutMethod;

/**
 * @ShortcutMethod
 */
public function getFormatted()
{
    // ...
}

use RyanWhitman\PhpValues\Attributes\ShortcutMethod;

#[ShortcutMethod]
public function getFormatted()
{
    // ...
}

::getFormattedFrom($value)
::tryGetFormattedFrom($value)
bash
composer