Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
25.00% covered (danger)
25.00%
1 / 4
CRAP
93.44% covered (success)
93.44%
57 / 61
FormConfig
0.00% covered (danger)
0.00%
0 / 1
25.00% covered (danger)
25.00%
1 / 4
32.29
93.44% covered (success)
93.44%
57 / 61
 createFromJson
0.00% covered (danger)
0.00%
0 / 1
11.10
90.48% covered (success)
90.48%
19 / 21
 jsonSerialize
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 filterConfig
0.00% covered (danger)
0.00%
0 / 1
9.02
94.12% covered (success)
94.12%
16 / 17
 filterFieldsetConfig
0.00% covered (danger)
0.00%
0 / 1
10.02
94.44% covered (success)
94.44%
17 / 18
<?php
/**
 * Pop PHP Framework (http://www.popphp.org/)
 *
 * @link       https://github.com/popphp/popphp-framework
 * @author     Nick Sagona, III <dev@nolainteractive.com>
 * @copyright  Copyright (c) 2009-2020 NOLA Interactive, LLC. (http://www.nolainteractive.com)
 * @license    http://www.popphp.org/license     New BSD License
 */
/**
 * @namespace
 */
namespace Pop\Form;
use Pop\Utils;
use Pop\Validator\ValidatorInterface;
/**
 * Form config class
 *
 * @category   Pop
 * @package    Pop\Form
 * @author     Nick Sagona, III <dev@nolainteractive.com>
 * @copyright  Copyright (c) 2009-2020 NOLA Interactive, LLC. (http://www.nolainteractive.com)
 * @license    http://www.popphp.org/license     New BSD License
 * @version    3.5.0
 */
class FormConfig extends Utils\ArrayObject
{
    /**
     * Create array object from JSON string
     *
     * @param  string  $jsonString
     * @param  int     $depth
     * @param  int     $options
     * @return FormConfig
     */
    public static function createFromJson($jsonString, $depth = 512, $options = 0)
    {
        $formConfig = parent::createFromJson($jsonString, $depth, $options)->toArray();
        $first      = reset($formConfig);
        if (!isset($first['type'])) {
            foreach ($formConfig as $key => $value) {
                foreach ($value as $ky => $vl) {
                    if (!empty($vl['validators'])) {
                        foreach ($vl['validators'] as $k => $v) {
                            $class = 'Pop\\Validator\\' . $v['type'];
                            $validator = new $class($v['value'], $v['message']);
                            if (!empty($v['input'])) {
                                $validator->setInput($v['input']);
                            }
                            $formConfig[$key][$ky]['validators'][$k] = $validator;
                        }
                    }
                }
            }
        } else {
            foreach ($formConfig as $key => $value) {
                if (!empty($value['validators'])) {
                    foreach ($value['validators'] as $k => $v) {
                        $class = 'Pop\\Validator\\' . $v['type'];
                        $validator = new $class($v['value'], $v['message']);
                        if (!empty($v['input'])) {
                            $validator->setInput($v['input']);
                        }
                        $formConfig[$key]['validators'][$k] = $validator;
                    }
                }
            }
        }
        return new self($formConfig);
    }
    /**
     * JSON serialize the array object
     *
     * @param  int $options
     * @param  int $depth
     * @return string
     */
    public function jsonSerialize($options = 0, $depth = 512)
    {
        $first = reset($this->data);
        if (!isset($first['type'])) {
            $this->filterFieldsetConfig();
        } else {
            $this->filterConfig();
        }
        return parent::jsonSerialize($options, $depth);
    }
    /**
     * Filter config validators
     *
     * @return FormConfig
     */
    public function filterConfig()
    {
        foreach ($this->data as $key => $value) {
            if (!empty($value['validator']) || !empty($value['validators'])) {
                $validators = (!empty($value['validator'])) ? $value['validator'] : $value['validators'];
                if (!is_array($validators)) {
                    $validators = [$validators];
                }
                foreach ($validators as $k => $validator) {
                    if ($validator instanceof ValidatorInterface) {
                        $validators[$k] = [
                            'type'    => str_replace('Pop\\Validator\\', '', get_class($validator)),
                            'input'   => $validator->getInput(),
                            'value'   => $validator->getValue(),
                            'message' => $validator->getMessage()
                        ];
                    } else {
                        unset($validators[$k]);
                    }
                }
                $this->data[$key]['validators'] = array_values($validators);
                if (isset($this->data[$key]['validator'])) {
                    unset($this->data[$key]['validator']);
                }
            }
        }
        return $this;
    }
    /**
     * Filter fieldset config validators
     *
     * @return FormConfig
     */
    public function filterFieldsetConfig()
    {
        foreach ($this->data as $key => $value) {
            foreach ($value as $ky => $vl) {
                if (!empty($vl['validator']) || !empty($vl['validators'])) {
                    $validators = (!empty($vl['validator'])) ? $vl['validator'] : $vl['validators'];
                    if (!is_array($validators)) {
                        $validators = [$validators];
                    }
                    foreach ($validators as $k => $validator) {
                        if ($validator instanceof ValidatorInterface) {
                            $validators[$k] = [
                                'type'    => str_replace('Pop\\Validator\\', '', get_class($validator)),
                                'input'   => $validator->getInput(),
                                'value'   => $validator->getValue(),
                                'message' => $validator->getMessage()
                            ];
                        } else {
                            unset($validators[$k]);
                        }
                    }
                    $this->data[$key][$ky]['validators'] = array_values($validators);
                    if (isset($this->data[$key][$k]['validator'])) {
                        unset($this->data[$key][$k]['validator']);
                    }
                }
            }
        }
        return $this;
    }
}