PluginProbe
Customify / 2.1.2
Customify v2.1.2
2.10.9 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.7.1 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.0.1 1.6.5 1.7.0 1.7.1 All 77 releases
customify / core / classes / Validator.php

Validator.php in Customify 2.1.2, at core/classes/Validator.php

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