1. Go to this page and download the library: Download smartemailing/types 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/ */
use SmartEmailing\Types\Emailaddress;
use SmartEmailing\Types\InvalidTypeException;
$input = [
'emailaddress' => '[email protected]',
'already_types_emailaddress' => Emailaddress::from('[email protected]'),
'invalid_data' => 'bla bla',
];
// Valid input
$emailaddress = Emailaddress::extract($input, 'emailaddress'); // returns Emailaddress object
$emailaddress = Emailaddress::extract($input, 'already_types_emailaddress'); // returns original Emailaddress object
// Invalid input
$emailaddress = Emailaddress::extract($input, 'invalid_data'); // throws InvalidTypeException
$emailaddress = Emailaddress::extract($input, 'not_existing_key'); // throws InvalidTypeException
// Nullables
$emailaddress = Emailaddress::extractOrNull($input, 'not_existing_key'); // returns null
$emailaddress = Emailaddress::extractOrNull($input, 'invalid_data'); // throws InvalidTypeException
$emailaddress = Emailaddress::extractOrNull($input, 'invalid_data', true); // returns null instead of throwing
// Default values
$emailaddress
= Emailaddress::extractOrNull($input, 'not_existing_key')
?? Emailaddress::from('[email protected]');
// uses null coalescing operator to assign default value if key not present or null
$emailaddress
= Emailaddress::extractOrNull($input, 'not_existing_key', true)
?? Emailaddress::from('[email protected]');
// uses null coalescing operator to assign default value if key not present or null or invalid
use SmartEmailing\Types\Emailaddress;
use SmartEmailing\Types\InvalidTypeException;
try {
$emailaddress = Emailaddress::extract(['emailaddress' => 'bla bla'], 'emailaddress');
} catch (InvalidTypeException $e) {
$e->getMessage(); // Problem at key emailaddress: Invalid emailaddress: bla bla
$e->getPublicMessage(); // Problem at key emailaddress: Invalid emailaddress: bla bla
$e->getKey(); // emailaddress (nested extraction: outer key first, e.g. address.country for Address::extract($data, 'address'))
$e->getInvalidValue(); // null (known for type errors and enums)
$e->getAcceptedValues(); // null (list of accepted values for enums)
}
// duplicate values will be discarted
// keys are ignored
UniqueIntArray::from(
[
1, 2, 2, 3, 3, 3, 4
]
);
// duplicate values will be discarted
// keys are ignored
UniqueStringArray::from(
[
'a',
'b',
'c',
'all work and no play makes jack a dull boy',
'all work and no play makes jack a dull boy',
'all work and no play makes jack a dull boy',
]
);