1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Ns \SpecialCharacterNames ;
6+
7+ class Foo
8+ {
9+ /**
10+ * Schema used to validate input for creating instances of this class
11+ *
12+ * @var array
13+ */
14+ private static array $ internalValidationSchema = [
15+ 'required ' => [
16+ 'foo:bar ' ,
17+ ],
18+ 'properties ' => [
19+ 'foo:bar ' => [
20+ 'type ' => 'string ' ,
21+ ],
22+ ],
23+ ];
24+
25+ /**
26+ * @var string
27+ */
28+ private string $ fooBar ;
29+
30+ /**
31+ * @param string $fooBar
32+ */
33+ public function __construct (string $ fooBar )
34+ {
35+ $ this ->fooBar = $ fooBar ;
36+ }
37+
38+ /**
39+ * @return string
40+ */
41+ public function getFooBar () : string
42+ {
43+ return $ this ->fooBar ;
44+ }
45+
46+ /**
47+ * @param string $fooBar
48+ * @return self
49+ */
50+ public function withFooBar (string $ fooBar ) : self
51+ {
52+ $ validator = new \JsonSchema \Validator ();
53+ $ validator ->validate ($ fooBar , self ::$ internalValidationSchema ['properties ' ]['foo:bar ' ]);
54+ if (!$ validator ->isValid ()) {
55+ throw new \InvalidArgumentException ($ validator ->getErrors ()[0 ]['message ' ]);
56+ }
57+
58+ $ clone = clone $ this ;
59+ $ clone ->fooBar = $ fooBar ;
60+
61+ return $ clone ;
62+ }
63+
64+ /**
65+ * Builds a new instance from an input array
66+ *
67+ * @param array|object $input Input data
68+ * @param bool $validate Set this to false to skip validation; use at own risk
69+ * @return Foo Created instance
70+ * @throws \InvalidArgumentException
71+ */
72+ public static function buildFromInput (array |object $ input , bool $ validate = true ) : Foo
73+ {
74+ $ input = is_array ($ input ) ? \JsonSchema \Validator::arrayToObjectRecursive ($ input ) : $ input ;
75+ if ($ validate ) {
76+ static ::validateInput ($ input );
77+ }
78+
79+ $ fooBar = $ input ->{'foo:bar ' };
80+
81+ $ obj = new self ($ fooBar );
82+
83+ return $ obj ;
84+ }
85+
86+ /**
87+ * Converts this object back to a simple array that can be JSON-serialized
88+ *
89+ * @return array Converted array
90+ */
91+ public function toJson () : array
92+ {
93+ $ output = [];
94+ $ output ['foo:bar ' ] = $ this ->fooBar ;
95+
96+ return $ output ;
97+ }
98+
99+ /**
100+ * Validates an input array
101+ *
102+ * @param array|object $input Input data
103+ * @param bool $return Return instead of throwing errors
104+ * @return bool Validation result
105+ * @throws \InvalidArgumentException
106+ */
107+ public static function validateInput (array |object $ input , bool $ return = false ) : bool
108+ {
109+ $ validator = new \JsonSchema \Validator ();
110+ $ input = is_array ($ input ) ? \JsonSchema \Validator::arrayToObjectRecursive ($ input ) : $ input ;
111+ $ validator ->validate ($ input , self ::$ internalValidationSchema );
112+
113+ if (!$ validator ->isValid () && !$ return ) {
114+ $ errors = array_map (function (array $ e ): string {
115+ return $ e ["property " ] . ": " . $ e ["message " ];
116+ }, $ validator ->getErrors ());
117+ throw new \InvalidArgumentException (join (", " , $ errors ));
118+ }
119+
120+ return $ validator ->isValid ();
121+ }
122+
123+ public function __clone ()
124+ {
125+ }
126+ }
0 commit comments