xref: /plugin/struct/meta/ValueValidator.php (revision 025cb9da4274aac00be48202c8eb49058f2dd283)
1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4
5use dokuwiki\plugin\struct\types\AbstractBaseType;
6
7/**
8 * Validator to validate a single value
9 */
10class ValueValidator {
11
12    /** @var  \helper_plugin_struct_db */
13    protected $hlp;
14
15    /** @var  array list of validation errors */
16    protected $errors;
17
18    /**
19     * ValueValidator constructor.
20     */
21    public function __construct() {
22        $this->hlp = plugin_load('helper', 'struct_db');
23        $this->errors = array();
24    }
25
26    /**
27     * Validate a single value
28     *
29     * @param Column $col the column of that value
30     * @param mixed &$rawvalue the value, will be fixed according to the type
31     * @return bool
32     */
33    public function validateValue(Column $col, &$rawvalue) {
34        // fix multi value types
35        $type = $col->getType();
36        $trans = $type->getTranslatedLabel();
37        if($type->isMulti() && !is_array($rawvalue)) {
38            $rawvalue = $type->splitValues($rawvalue);
39        }
40        // strip empty fields from multi vals
41        if(is_array($rawvalue)) {
42            $rawvalue = array_filter($rawvalue, array($this, 'filter'));
43            $rawvalue = array_values($rawvalue); // reset the array keys
44        }
45
46        // validate data
47        return $this->validateField($type, $trans, $rawvalue);
48    }
49
50    /**
51     * The errors that occured during validation
52     *
53     * @return string[] already translated error messages
54     */
55    public function getErrors() {
56        return $this->errors;
57    }
58
59    /**
60     * Validate the given data for a single field
61     *
62     * Catches the Validation exceptions and transforms them into proper error messages.
63     *
64     * Blank values are not validated and always pass
65     *
66     * @param AbstractBaseType $type
67     * @param string $label
68     * @param array|string|int &$data may be modified by the validation function
69     * @return bool true if the data validates, otherwise false
70     */
71    protected function validateField(AbstractBaseType $type, $label, &$data) {
72        $prefix = sprintf($this->hlp->getLang('validation_prefix'), $label);
73
74        $ok = true;
75        if(is_array($data)) {
76            foreach($data as &$value) {
77                if(!blank($value)) {
78                    try {
79                        $value = $type->validate($value);
80                    } catch(ValidationException $e) {
81                        $this->errors[] = $prefix . $e->getMessage();
82                        $ok = false;
83                    }
84                }
85            }
86            return $ok;
87        }
88
89        if(!blank($data)) {
90            try {
91                $data = $type->validate($data);
92            } catch(ValidationException $e) {
93                $this->errors[] = $prefix . $e->getMessage();
94                $ok = false;
95            }
96        }
97        return $ok;
98    }
99
100    /**
101     * Simple filter to remove blank values
102     *
103     * @param string $val
104     * @return bool
105     */
106    public function filter($val) {
107        return !blank($val);
108    }
109}
110