xref: /plugin/struct/meta/ValueValidator.php (revision 0549dcc5bc88d4f9d923acdd09931d8d51be7097)
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    {
23        $this->hlp = plugin_load('helper', 'struct_db');
24        $this->errors = array();
25    }
26
27    /**
28     * Validate a single value
29     *
30     * @param Column $col the column of that value
31     * @param mixed &$rawvalue the value, will be fixed according to the type
32     * @return bool
33     */
34    public function validateValue(Column $col, &$rawvalue)
35    {
36        // fix multi value types
37        $type = $col->getType();
38        $trans = $type->getTranslatedLabel();
39        if ($type->isMulti() && !is_array($rawvalue)) {
40            $rawvalue = $type->splitValues($rawvalue);
41        }
42        // strip empty fields from multi vals
43        // but keep at least one so we can properly delete multivalues on update
44        if (is_array($rawvalue) && count($rawvalue) > 1) {
45            $rawvalue = array_filter($rawvalue, array($this, 'filter'));
46            $rawvalue = array_values($rawvalue); // reset the array keys
47        }
48
49        // validate data
50        return $this->validateField($type, $trans, $rawvalue);
51    }
52
53    /**
54     * The errors that occured during validation
55     *
56     * @return string[] already translated error messages
57     */
58    public function getErrors()
59    {
60        return $this->errors;
61    }
62
63    /**
64     * Validate the given data for a single field
65     *
66     * Catches the Validation exceptions and transforms them into proper error messages.
67     *
68     * Blank values are not validated and always pass
69     *
70     * @param AbstractBaseType $type
71     * @param string $label
72     * @param array|string|int &$data may be modified by the validation function
73     * @return bool true if the data validates, otherwise false
74     */
75    protected function validateField(AbstractBaseType $type, $label, &$data)
76    {
77        $prefix = sprintf($this->hlp->getLang('validation_prefix'), $label);
78
79        $ok = true;
80        if (is_array($data)) {
81            foreach ($data as &$value) {
82                if (!blank($value)) {
83                    try {
84                        $value = $type->validate($value);
85                    } catch (ValidationException $e) {
86                        $this->errors[] = $prefix . $e->getMessage();
87                        $ok = false;
88                    }
89                }
90            }
91            return $ok;
92        }
93
94        if (!blank($data)) {
95            try {
96                $data = $type->validate($data);
97            } catch (ValidationException $e) {
98                $this->errors[] = $prefix . $e->getMessage();
99                $ok = false;
100            }
101        }
102        return $ok;
103    }
104
105    /**
106     * Simple filter to remove blank values
107     *
108     * @param string $val
109     * @return bool
110     */
111    public function filter($val)
112    {
113        return !blank($val);
114    }
115}
116