protected function __construct($options = array(), $type = null) { static::$_handlers = array(static::RAW => function ($options) { return $options[0]; }, static::PARAMS => function ($options) { if (!empty($options[1]) && !is_array($options[1])) { $options[1] = array($options[1]); } return Query::insertParams($options[0], $options[1], isset($options[2]) ? $options[2] : true); }, static::SPRINTF => function ($options) { return call_user_func_array('sprintf', $options); }, static::GRAVES => function ($options) { return Query::graves($options[0]); }, static::VALUES => function ($options) { return 'VALUES(' . Query::graves($options[0]) . ')'; }, static::LIKE => function ($options) { if (count($options) >= 2) { return Query::graves($options[0]) . " LIKE '%" . Query::like($options[1]) . "%'"; } return "'%" . Query::like($options[0]) . "%'"; }, static::ENDS_LIKE => function ($options) { if (count($options) >= 2) { return Query::graves($options[0]) . " LIKE '%" . Query::like($options[1]) . "'"; } return "'%" . Query::like($options[0]) . "'"; }, static::STARTS_LIKE => function ($options) { if (count($options) >= 2) { return Query::graves($options[0]) . " LIKE '" . Query::like($options[1]) . "%'"; } return "'" . Query::like($options[0]) . "%'"; }, static::CALLBACK => function ($options, $query) { if (!$query || empty($options[0]) || !is_callable($options[0])) { return ''; } return call_user_func($options[0], $query); }); $type = $type ?: static::RAW; if (!isset(static::$_handlers[$type])) { throw new \Exception('Unknown expression type `$type`'); } $this->_type = $type; $this->_options = $options; }
public static function handlers($handlers = null) { if (!static::$_handlers) { static::$_handlers = array('binding' => function ($request, $options) { $model = $options['binding']; $name = $options['name']; $model = Libraries::locate('models', $model ?: Inflector::pluralize($name)); if (!$model || is_string($model) && !class_exists($model)) { $msg = "Could not find resource-mapped model class for resource `{$name}`."; throw new UnmappedResourceException($msg); } return $model; }, 'isRequired' => function ($request, $options, $result = null) { $name = $options['name']; $required = $options['required']; $model = $options['binding']; $isCountable = $result && $result instanceof Countable; $isValid = $result && !$isCountable || $isCountable && $result->valid(); if ($isValid || !$required) { return $result; } $model = is_object($model) ? get_class($model) : $model; $message = "Resource `{$name}` not found in model `{$model}`."; throw new ResourceNotFoundException($message); }, 'default' => array(function ($request, $options) { $isRequired = Resources::handlers('isRequired'); $query = (array) $options['call'] + array('all'); $call = $query[0]; unset($query[0]); return $isRequired($request, $options, $options['binding']::$call($query)); }, 'create' => function ($request, $options) { return $options['binding']::create($request->data); })); } if (is_array($handlers)) { static::$_handlers = $handlers + static::$_handlers; } if ($handlers && is_string($handlers)) { return isset(static::$_handlers[$handlers]) ? static::$_handlers[$handlers] : null; } return static::$_handlers; }
/** * Wipes out all configuration and resets the error handler to its initial state when loaded. * Mainly used for testing. * * @return void */ public static function reset() { static::$_config = array(); static::$_checks = array(); static::$_handlers = array(); static::$_exceptionHandler = null; static::__init(); }
/** * Resets the `Media` class to its default state. Mainly used for ensuring a consistent state * during testing. */ public static function reset() { static::$_handlers = array(); static::$_types = array(); static::$_scope = false; if (isset(static::$_scopes)) { static::$_scopes->reset(); } }
/** * Resets or removes all defined error messages. * * @param boolean $totaly If `true` error messages will be completly deleted and not reseted. */ public static function reset($totaly = false) { static::$_handlers = []; static::$_messages = ['_default_' => 'is invalid']; if ($totaly === true) { return; } static::messages(['accepted' => 'must be accepted', 'alphaNumeric' => 'must contain only letters a-z and/or numbers 0-9', 'boolean' => 'must be a boolean', 'creditCard' => 'must be a valid credit card number', 'date' => 'is not a valid date', 'dateAfter' => 'must be date after {:date}', 'dateBefore' => 'must be date before {:date}', 'dateFormat' => 'must be date with format {:format}', 'decimal' => 'must be decimal', 'email' => 'is not a valid email address', 'equalTo' => 'must be the equal to the field `{:key}`', 'empty' => 'must be a empty', 'not:empty' => 'must not be a empty', 'inList' => 'must contain a valid value', 'not:inList' => 'must contain a valid value', 'inRange' => 'must be inside the range', 'not:inRange' => 'must be ouside the range', 'integer' => 'must be an integer', 'ip' => 'must be an ip', 'length' => 'must be longer than {:length}', 'lengthBetween' => 'must be between {:min} and {:max} characters', 'lengthMax' => 'must contain less than {:length} characters', 'lengthMin' => 'must contain greater than {:length} characters', 'luhn' => 'must be a valid credit card number', 'max' => 'must be no more than {:max}', 'min' => 'must be at least {:min}', 'money' => 'must be a valid monetary amount', 'numeric' => 'must be numeric', 'phone' => 'must be a phone number', 'regex' => 'contains invalid characters', 'required' => 'is required', 'time' => 'must be a valid time', 'url' => 'not a URL']); static::set(['accepted' => function ($value, $options = [], &$params = []) { $bool = is_bool($value); $filter = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); return $bool || $filter !== null && $value !== null; }, 'alphaNumeric' => function ($value, $options = [], &$params = []) { $rules = ['/^[\\p{Ll}\\p{Lm}\\p{Lo}\\p{Lt}\\p{Lu}\\p{Nd}]+$/mu']; return empty($value) && $value !== '0' ? false : static::check($value, $rules, $options); }, 'boolean' => function ($value, $options = [], &$params = []) { return in_array($value, [0, 1, '0', '1', true, false], true); }, 'creditCard' => function ($value, $options = [], &$params = []) { $rules = ['amex' => '/^3[4|7]\\d{13}$/', 'bankcard' => '/^56(10\\d\\d|022[1-5])\\d{10}$/', 'diners' => '/^(?:3(0[0-5]|[68]\\d)\\d{11})|(?:5[1-5]\\d{14})$/', 'disc' => '/^(?:6011|650\\d)\\d{12}$/', 'electron' => '/^(?:417500|4917\\d{2}|4913\\d{2})\\d{10}$/', 'enroute' => '/^2(?:014|149)\\d{11}$/', 'jcb' => '/^(3\\d{4}|2100|1800)\\d{11}$/', 'maestro' => '/^(?:5020|6\\d{3})\\d{12}$/', 'mc' => '/^5[1-5]\\d{14}$/', 'solo' => '/^(6334[5-9][0-9]|6767[0-9]{2})\\d{10}(\\d{2,3})?$/', 'switch' => '/^(?:49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2})' . '\\d{10}(\\d{2,3})?)|(?:564182\\d{10}(\\d{2,3})?)|(6(3(33[0-4]' . '[0-9])|759[0-9]{2})\\d{10}(\\d{2,3})?)$/', 'visa' => '/^4\\d{12}(\\d{3})?$/', 'voyager' => '/^8699[0-9]{11}$/', 'fast' => '/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3' . '(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/']; $options += ['deep' => false]; if (strlen($value = str_replace(['-', ' '], '', $value)) < 13) { return false; } if (!static::check($value, $rules, $options)) { return false; } return $options['deep'] ? static::isLuhn($value) : true; }, 'date' => function ($value, $options = [], &$params = []) { if (is_string($value)) { return strtotime($value) !== false; } return $value instanceof DateTime; }, 'dateAfter' => function ($value, $options = [], &$params = []) { if (!isset($options['date'])) { return false; } $after = $options['date']; $vtime = $value instanceof DateTime ? $value->getTimestamp() : strtotime($value); $atime = $after instanceof DateTime ? $after->getTimestamp() : strtotime($after); $params['date'] = date('Y-m-d H:i:s', $atime); return $vtime >= $atime; }, 'dateBefore' => function ($value, $options = [], &$params = []) { if (!isset($options['date'])) { return false; } $before = $options['date']; $vtime = $value instanceof DateTime ? $value->getTimestamp() : strtotime($value); $btime = $before instanceof DateTime ? $before->getTimestamp() : strtotime($before); $params['date'] = date('Y-m-d H:i:s', $btime); return $vtime <= $btime; }, 'dateFormat' => function ($value, $options = [], &$params = []) { $defaults = ['format' => 'Y-m-d H:i:s']; $options += $defaults; $parsed = date_parse_from_format($options['format'], $value); return $parsed['error_count'] === 0 && $parsed['warning_count'] === 0 && $parsed['month'] <= 12; }, 'decimal' => function ($value, $options = [], &$params = []) { if (isset($options['precision'])) { $precision = strlen($value) - strrpos($value, '.') - 1; if ($precision !== (int) $options['precision']) { return false; } } return filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE) !== null; }, 'email' => function ($value, $options = [], &$params = []) { $defaults = ['deep' => false]; $options += $defaults; if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { return false; } if (!$options['deep']) { return true; } list($prefix, $host) = explode('@', $value); $mxhosts = []; if (getmxrr($host, $mxhosts)) { return is_array($mxhosts); } return false; }, 'empty' => '/^\\s*$/', 'equalTo' => function ($value, $options = [], &$params = []) { if (!isset($options['key'])) { return false; } $field = $options['key']; return isset($options['data'][$field]) && $value == $options['data'][$field]; }, 'inList' => function ($value, $options) { $options += ['list' => []]; $strict = is_bool($value) || $value === '' || $value === null; return in_array($value, $options['list'], $strict); }, 'inRange' => function ($value, $options = [], &$params = []) { $defaults = ['upper' => null, 'lower' => null]; $options += $defaults; if (!is_numeric($value)) { return false; } switch (true) { case $options['upper'] !== null && $options['lower'] !== null: return $value >= $options['lower'] && $value <= $options['upper']; case $options['upper'] !== null: return $value <= $options['upper']; case $options['lower'] !== null: return $value >= $options['lower']; } return is_finite($value); }, 'integer' => function ($value, $options = [], &$params = []) { $options += ['flags' => []]; return filter_var($value, FILTER_VALIDATE_INT, $options); }, 'ip' => function ($value, $options = [], &$params = []) { $options += ['flags' => []]; return (bool) filter_var($value, FILTER_VALIDATE_IP, $options); }, 'length' => function ($value, $options = [], &$params = []) { return isset($options['length']) && strlen($value) === $options['length']; }, 'lengthBetween' => function ($value, $options = [], &$params = []) { $length = strlen($value); return isset($options['min']) && isset($options['max']) && $length >= $options['min'] && $length <= $options['max']; }, 'lengthMax' => function ($value, $options = [], &$params = []) { $length = strlen($value); return isset($options['length']) && $length <= $options['length']; }, 'lengthMin' => function ($value, $options = [], &$params = []) { $length = strlen($value); return isset($options['length']) && $length >= $options['length']; }, 'luhn' => function ($value, $options = [], &$params = []) { if (empty($value) || !is_string($value)) { return false; } $sum = 0; $length = strlen($value); for ($position = 1 - $length % 2; $position < $length; $position += 2) { $sum += $value[$position]; } for ($position = $length % 2; $position < $length; $position += 2) { $number = $value[$position] * 2; $sum += $number < 10 ? $number : $number - 9; } return $sum % 10 === 0; }, 'max' => function ($value, $options = [], &$params = []) { return isset($options['max']) && $value <= $options['max']; }, 'min' => function ($value, $options = [], &$params = []) { return isset($options['min']) && $value >= $options['min']; }, 'money' => ['right' => '/^(?!0,?\\d)(?:\\d{1,3}(?:([, .])\\d{3})?(?:\\1\\d{3})*|(?:\\d+))' . '((?!\\1)[,.]\\d{2})?(?<!\\x{00a2})\\p{Sc}?$/u', 'left' => '/^(?!\\x{00a2})\\p{Sc}?(?!0,?\\d)(?:\\d{1,3}(?:([, .])\\d{3})?' . '(?:\\1\\d{3})*|(?:\\d+))((?!\\1)[,.]\\d{2})?$/u'], 'numeric' => function ($value, $options = [], &$params = []) { return is_numeric($value); }, 'phone' => '/^\\+?[0-9\\(\\)\\-]{10,20}$/', 'regex' => '/^(?:([^[:alpha:]\\\\{<\\[\\(])(.+)(?:\\1))|(?:{(.+)})|(?:<(.+)>)|' . '(?:\\[(.+)\\])|(?:\\((.+)\\))[gimsxu]*$/', 'time' => '%^((0?[1-9]|1[012])(:[0-5]\\d){0,2}([AP]M|[ap]m))$|^([01]\\d|2[0-3])(:[0-5]\\d){0,2}$%', 'url' => function ($value, $options = [], &$params = []) { $options += ['flags' => []]; return (bool) filter_var($value, FILTER_VALIDATE_URL, $options); }, 'uuid' => "/^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}\$/"]); }