Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
63 / 63
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
FormConfig
100.00% covered (success)
100.00%
63 / 63
100.00% covered (success)
100.00%
4 / 4
32
100.00% covered (success)
100.00%
1 / 1
 createFromJson
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
11
 jsonSerialize
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 filterConfig
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
9
 filterFieldsetConfig
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
10
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Form;
16
17use Pop\Utils;
18use Pop\Validator\ValidatorInterface;
19
20/**
21 * Form config class
22 *
23 * @category   Pop
24 * @package    Pop\Form
25 * @author     Nick Sagona, III <nick@popphp.org>
26 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
27 * @license    https://www.popphp.org/license     New BSD License
28 * @version    5.0.0
29 */
30
31class FormConfig extends Utils\ArrayObject
32{
33
34    /**
35     * Create array object from JSON string
36     *
37     * @param  string  $jsonString
38     * @param  int     $depth
39     * @param  int     $options
40     * @return FormConfig
41     */
42    public static function createFromJson(string $jsonString, int $depth = 512, int $options = 0): FormConfig
43    {
44        $formConfig = parent::createFromJson($jsonString, $depth, $options)->toArray();
45        $first      = reset($formConfig);
46
47        if (!isset($first['type'])) {
48            foreach ($formConfig as $key => $value) {
49                foreach ($value as $ky => $vl) {
50                    if (!empty($vl['validators'])) {
51                        foreach ($vl['validators'] as $k => $v) {
52                            $class = 'Pop\\Validator\\' . $v['type'];
53                            $validator = new $class($v['value'], $v['message']);
54                            if (!empty($v['input'])) {
55                                $validator->setInput($v['input']);
56                            }
57                            $formConfig[$key][$ky]['validators'][$k] = $validator;
58                        }
59                    }
60                }
61            }
62        } else {
63            foreach ($formConfig as $key => $value) {
64                if (!empty($value['validators'])) {
65                    foreach ($value['validators'] as $k => $v) {
66                        $class = 'Pop\\Validator\\' . $v['type'];
67                        $validator = new $class($v['value'], $v['message']);
68                        if (!empty($v['input'])) {
69                            $validator->setInput($v['input']);
70                        }
71                        $formConfig[$key]['validators'][$k] = $validator;
72                    }
73                }
74            }
75        }
76
77
78        return new self($formConfig);
79    }
80
81    /**
82     * JSON serialize the array object
83     *
84     * @param  int $options
85     * @param  int $depth
86     * @return string
87     */
88    public function jsonSerialize(int $options = 0, int $depth = 512): string
89    {
90        $first = reset($this->data);
91        if (!isset($first['type'])) {
92            $this->filterFieldsetConfig();
93        } else {
94            $this->filterConfig();
95        }
96        return parent::jsonSerialize($options, $depth);
97    }
98
99    /**
100     * Filter config validators
101     *
102     * @return FormConfig
103     */
104    public function filterConfig(): FormConfig
105    {
106        foreach ($this->data as $key => $value) {
107            if (!empty($value['validator']) || !empty($value['validators'])) {
108                $validators = (!empty($value['validator'])) ? $value['validator'] : $value['validators'];
109                if (!is_array($validators)) {
110                    $validators = [$validators];
111                }
112                foreach ($validators as $k => $validator) {
113                    if ($validator instanceof ValidatorInterface) {
114                        $validators[$k] = [
115                            'type'    => str_replace('Pop\\Validator\\', '', get_class($validator)),
116                            'input'   => $validator->getInput(),
117                            'value'   => $validator->getValue(),
118                            'message' => $validator->getMessage()
119                        ];
120                    } else {
121                        unset($validators[$k]);
122                    }
123                }
124                $this->data[$key]['validators'] = array_values($validators);
125                if (isset($this->data[$key]['validator'])) {
126                    unset($this->data[$key]['validator']);
127                }
128            }
129        }
130
131        return $this;
132    }
133
134    /**
135     * Filter fieldset config validators
136     *
137     * @return FormConfig
138     */
139    public function filterFieldsetConfig(): FormConfig
140    {
141        foreach ($this->data as $key => $value) {
142            foreach ($value as $ky => $vl) {
143                if (!empty($vl['validator']) || !empty($vl['validators'])) {
144                    $validators = (!empty($vl['validator'])) ? $vl['validator'] : $vl['validators'];
145                    if (!is_array($validators)) {
146                        $validators = [$validators];
147                    }
148                    foreach ($validators as $k => $validator) {
149                        if ($validator instanceof ValidatorInterface) {
150                            $validators[$k] = [
151                                'type'    => str_replace('Pop\\Validator\\', '', get_class($validator)),
152                                'input'   => $validator->getInput(),
153                                'value'   => $validator->getValue(),
154                                'message' => $validator->getMessage()
155                            ];
156                        } else {
157                            unset($validators[$k]);
158                        }
159                    }
160                    $this->data[$key][$ky]['validators'] = array_values($validators);
161                    if (isset($this->data[$key][$ky]['validator'])) {
162                        unset($this->data[$key][$ky]['validator']);
163                    }
164                }
165            }
166        }
167
168        return $this;
169    }
170
171}