Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
12 / 14
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
FormValidation
85.71% covered (warning)
85.71%
12 / 14
83.33% covered (warning)
83.33%
5 / 6
8.19
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 serialize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 jsonSerialize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 unserialize
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
3.33
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Model\Message
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Model\Message;
16
17use phpOMS\Contract\SerializableInterface;
18
19/**
20 * FormValidation class.
21 *
22 * @package phpOMS\Model\Message
23 * @license OMS License 2.0
24 * @link    https://jingga.app
25 * @since   1.0.0
26 */
27final class FormValidation implements \JsonSerializable, SerializableInterface
28{
29    /**
30     * Message type.
31     *
32     * @var string
33     * @since 1.0.0
34     */
35    public const TYPE = 'validation';
36
37    /**
38     * Form validation result.
39     *
40     * @var array
41     * @since 1.0.0
42     */
43    private array $validation = [];
44
45    /**
46     * Constructor.
47     *
48     * @param array $validation Invalid data
49     *
50     * @since 1.0.0
51     */
52    public function __construct(array $validation = [])
53    {
54        $this->validation = $validation;
55    }
56
57    /**
58     * Render message.
59     *
60     * @return string
61     *
62     * @since 1.0.0
63     */
64    public function serialize() : string
65    {
66        return $this->__toString();
67    }
68
69    /**
70     * {@inheritdoc}
71     */
72    public function jsonSerialize() : mixed
73    {
74        return $this->toArray();
75    }
76
77    /**
78     * {@inheritdoc}
79     */
80    public function unserialize(mixed $raw) : void
81    {
82        if (!\is_string($raw)) {
83            return;
84        }
85
86        $unserialized = \json_decode($raw, true);
87        if (!\is_array($unserialized)) {
88            return;
89        }
90
91        $this->validation = $unserialized['validation'] ?? [];
92    }
93
94    /**
95     * Stringify.
96     *
97     * @return string
98     *
99     * @since 1.0.0
100     */
101    public function __toString()
102    {
103        return (string) \json_encode($this->toArray());
104    }
105
106    /**
107     * Generate message array.
108     *
109     * @return array<string, mixed>
110     *
111     * @since 1.0.0
112     */
113    public function toArray() : array
114    {
115        return [
116            'type'       => self::TYPE,
117            'validation' => $this->validation,
118        ];
119    }
120}