- var_dump: documentation ( source)
- preg_match_all: documentation ( source)
<?php
interface PolicyRule
{
public function isSatisfiedBy();
}
abstract class EnforcedRule implements PolicyRule
{
const TYPE_NO = 'No';
const TYPE_YES = 'Yes';
protected $enforcementType;
protected function isEnforced()
{
return ($this->enforcementType == self::TYPE_YES);
}
protected function guardEnforcementType($aType)
{
if (! in_array($aType, [self::TYPE_NO, self::TYPE_YES])) {
throw new InvalidEnforcementTypeException($aType);
}
}
}
abstract class MinimumCharacterCountRule extends EnforcedRule
{
const DEFAULT_MINIMUM = 1;
protected $regexPattern;
protected $minimumCount = self::DEFAULT_MINIMUM;
public function isSatisfiedBy($aValue)
{
if (! $this->isEnforced()) {
return true;
}
$matches = [];
preg_match_all($this->regexPattern, $aValue, $matches);
return (count($matches[0]) >= $this->minimumCount);
}
}
class NumericRule extends MinimumCharacterCountRule
{
protected $regexPattern = '/[0-9]/';
public function __construct($aMinimumCount = self::DEFAULT_MINIMUM)
{
$this->minimumCount = $aMinimumCount;
}
}
class SpecialCharacterRule extends MinimumCharacterCountRule
{
protected $regexPattern = '/[~`#\$%\^&\*\(\)\-_\+\|,\.<>]/';
public function __construct($aMinimumCount = self::DEFAULT_MINIMUM)
{
$this->minimumCount = $aMinimumCount;
}
}
$numeric = new NumericRule();
var_dump($numeric->isSatisfiedBy("for5"));