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