Skip to content

Instantly share code, notes, and snippets.

@DaveRandom
Forked from Nessworthy/MaxLength.php
Last active October 7, 2016 09:45
Show Gist options
  • Save DaveRandom/718baae0a81c0c876d5c5ae2055cedbc to your computer and use it in GitHub Desktop.
Save DaveRandom/718baae0a81c0c876d5c5ae2055cedbc to your computer and use it in GitHub Desktop.
<?php
namespace Parts\Content;
use Parts\Primitive\StringType;
use Parts\Restrictions\MaxLength;
use Parts\Restrictions\MinLength;
use Parts\Restrictions\Pattern;
/**
* Class PostcodeText
* Holds a UK postcode.
* @package Parts\Content
*/
class PostcodeText extends StringType implements MaxLength, MinLength, Pattern
{
public function __construct($string)
{
$string = $this->convertType($string);
$this->validateMinLength($string, 1);
$this->validateMaxLength($string, 8);
$this->validateRegEx($string, '#^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}$#');
parent::__construct($string);
}
}
<?php
namespace Parts\Primitive;
use Parts\InvalidPrimitiveTypeException;
use Parts\Restrictions\Enumeration;
use Parts\Restrictions\MaxLength;
use Parts\Restrictions\MinLength;
use Parts\Restrictions\Pattern;
use Parts\ValidationRestrictionException;
/**
* The base type to represent a string.
* @package Parts\Primitive
*/
class StringType extends BaseSimpleType
{
/**
* Convert the data into a more suitable form.
* @param mixed $var
* @return string
*/
protected final function convertType($var): string
{
/**
* Possible php string conversions.
* Props to user mpen of StackOverflow
* @link http://stackoverflow.com/a/27368848/2274710
*/
if ($var === null || is_scalar($var) || is_callable([$var, '__toString'])) {
return (string)$var;
}
throw new InvalidPrimitiveTypeException(sprintf(
'%s expected a string or type which can be converted to string, %s given.',
get_class($this),
gettype($var)
));
}
protected final function validateMaxLength(string $string, int $maxLength)
{
$length = strlen($string);
if ($length <= $maxLength) {
return;
}
throw new ValidationRestrictionException(sprintf(
'%s expected a string of at most %s character%s, %s given.',
get_class($this),
$maxLength,
$maxLength === 1 ? '' : 's',
$length
));
}
protected final function validateMinLength(string $string, int $minLength)
{
$length = strlen($string);
if ($length >= $minLength) {
return;
}
throw new ValidationRestrictionException(sprintf(
'%s expected a string of at least %s character%s, %s given.',
get_class($this),
$maxLength,
$maxLength === 1 ? '' : 's',
$length
));
}
protected final function validateRegEx(string $string, string $pattern)
{
if (preg_match($pattern, $string)) {
return;
}
throw new ValidationRestrictionException(sprintf(
'%s expected a string matching the pattern ' . "\n" . '%s.',
get_class($this),
$pattern
));
}
protected final function validateEnum(string $string, string $pattern)
{
$choices = $this->getEnumerableChoices();
if (in_array($string, $choices, true)) {
return;
}
throw new ValidationRestrictionException(sprintf(
'%s expected one of the following choices: %s; "%s" given',
get_class($this),
implode(', ', $choices),
$string
));
}
/**
* StringType constructor.
* @param string $string A text string.
*/
public function __construct($string)
{
parent::__construct($this->convertType($string));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment