| 1 |
<?php defined('ABSPATH') or die; |
| 2 |
|
| 3 |
/* This file is property of Pixel Grade Media. You may NOT copy, or redistribute |
| 4 |
* it. Please see the license that came with your copy for more information. |
| 5 |
*/ |
| 6 |
|
| 7 |
/** |
| 8 |
* @package pixcustomify |
| 9 |
* @category core |
| 10 |
* @author Pixel Grade Team |
| 11 |
* @copyright (c) 2013, Pixel Grade Media |
| 12 |
*/ |
| 13 |
class PixCustomifyValidatorImpl implements PixCustomifyValidator { |
| 14 |
|
| 15 |
/** @var PixCustomifyMeta plugin configuration */ |
| 16 |
protected $meta = null; |
| 17 |
|
| 18 |
/** @var PixCustomifyMeta field information */ |
| 19 |
protected $fields = null; |
| 20 |
|
| 21 |
/** |
| 22 |
* @param array config |
| 23 |
*/ |
| 24 |
static function instance($config = null, $fields = null) { |
| 25 |
$i = new self; |
| 26 |
$i->configure($config, $fields); |
| 27 |
return $i; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Apply configuration. |
| 32 |
* |
| 33 |
* Fields array is assumed to be flat. The class will not perform any field |
| 34 |
* extraction itself. |
| 35 |
*/ |
| 36 |
protected function configure($config = null, $fields = null) { |
| 37 |
$config !== null or $config = array(); |
| 38 |
$fields !== null or $fields = array(); |
| 39 |
|
| 40 |
if (is_array($config)) { |
| 41 |
$this->meta = pixcustomify::instance('PixCustomifyMeta', $config); |
| 42 |
} |
| 43 |
else { // non-array; assume meta object |
| 44 |
$this->meta = $config; |
| 45 |
} |
| 46 |
|
| 47 |
if (is_array($fields)) { |
| 48 |
$this->fields = pixcustomify::instance('PixCustomifyMeta', $fields); |
| 49 |
} |
| 50 |
else { // non-array; assume meta object |
| 51 |
$this->fields = $fields; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Validation will only be performed on input keys not on all field keys to |
| 57 |
* allow for partial input validation. |
| 58 |
* |
| 59 |
* @param array input |
| 60 |
* @return array errors (empty if no errors) |
| 61 |
*/ |
| 62 |
function validate($input) { |
| 63 |
$errors = array(); |
| 64 |
$defaults = pixcustomify::defaults(); |
| 65 |
$plugin_checks = $this->meta->get('checks', array()); |
| 66 |
|
| 67 |
foreach ($input as $key => $value) { |
| 68 |
|
| 69 |
$field = $this->fields->get($key); |
| 70 |
|
| 71 |
// Calculate validation rules |
| 72 |
// -------------------------- |
| 73 |
|
| 74 |
$rules = array(); |
| 75 |
// check pixcustomify defaults |
| 76 |
if (isset($defaults['checks'][$field['type']])) { |
| 77 |
$rules = $defaults['checks'][$field['type']]; |
| 78 |
} |
| 79 |
// check theme defaults |
| 80 |
if (isset($plugin_checks[$field['type']])) { |
| 81 |
$rules = array_merge($rules, $plugin_checks[$field['type']]); |
| 82 |
} |
| 83 |
// check field presets |
| 84 |
if (isset($field['checks'])) { |
| 85 |
$rules = array_merge($rules, $field['checks']); |
| 86 |
} |
| 87 |
|
| 88 |
// Perform validation |
| 89 |
// ------------------ |
| 90 |
|
| 91 |
foreach ($rules as $rule) { |
| 92 |
$callback = pixcustomify::callback($rule, $this->meta); |
| 93 |
$valid = call_user_func($callback, $input[$key], $field, $this); |
| 94 |
if ( ! $valid) { |
| 95 |
isset($errors[$key]) or $errors[$key] = array(); |
| 96 |
$errors[$key][$rule] = $this->error_message($rule); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
return $errors; |
| 102 |
} |
| 103 |
|
| 104 |
/** @var array error messages */ |
| 105 |
protected static $error_message_cache = null; |
| 106 |
|
| 107 |
/** |
| 108 |
* @param string rule |
| 109 |
* @return string error message |
| 110 |
*/ |
| 111 |
function error_message($rule) { |
| 112 |
if (self::$error_message_cache === null) { |
| 113 |
$defaults = pixcustomify::defaults(); |
| 114 |
$default_errors = $defaults['errors']; |
| 115 |
$plugin_errors = $this->meta->get('errors', array()); |
| 116 |
self::$error_message_cache = array_merge($default_errors, $plugin_errors); |
| 117 |
} |
| 118 |
|
| 119 |
return self::$error_message_cache[$rule]; |
| 120 |
} |
| 121 |
|
| 122 |
} # class |
| 123 |
|