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