1+ <?php // Auto-generated from vscode-languageserver-protocol (typescript)
2+
3+ namespace Phpactor \LanguageServerProtocol ;
4+
5+ use DTL \Invoke \Invoke ;
6+ use Exception ;
7+ use RuntimeException ;
8+
9+ class TextDocumentContentChangeIncrementalEvent
10+ {
11+ /**
12+ * The range of the document that changed.
13+ *
14+ * @var Range
15+ */
16+ public $ range ;
17+
18+ /**
19+ * The optional length of the range that got replaced.
20+ *
21+ * @var int|null
22+ */
23+ public $ rangeLength ;
24+
25+ /**
26+ * The new text for the provided range.
27+ *
28+ * @var string
29+ */
30+ public $ text ;
31+
32+ /**
33+ * @param Range $range
34+ * @param int|null $rangeLength
35+ * @param string $text
36+ */
37+ public function __construct (Range $ range , string $ text , ?int $ rangeLength = null )
38+ {
39+ $ this ->range = $ range ;
40+ $ this ->rangeLength = $ rangeLength ;
41+ $ this ->text = $ text ;
42+ }
43+
44+ /**
45+ * @param array<string,mixed> $array
46+ * @return self
47+ */
48+ public static function fromArray (array $ array , bool $ allowUnknownKeys = false )
49+ {
50+ $ map = [
51+ 'range ' => ['names ' => [Range::class], 'iterable ' => false ],
52+ 'rangeLength ' => ['names ' => [], 'iterable ' => false ],
53+ 'text ' => ['names ' => [], 'iterable ' => false ],
54+ ];
55+
56+ foreach ($ array as $ key => &$ value ) {
57+ if (!isset ($ map [$ key ])) {
58+ if ($ allowUnknownKeys ) {
59+ unset($ array [$ key ]);
60+ continue ;
61+ }
62+
63+ throw new RuntimeException (sprintf (
64+ 'Parameter "%s" on class "%s" not known, known parameters: "%s" ' ,
65+ $ key ,
66+ self ::class,
67+ implode ('", " ' , array_keys ($ map ))
68+ ));
69+ }
70+
71+ // from here we only care about arrays that can be transformed into
72+ // objects
73+ if (!is_array ($ value )) {
74+ continue ;
75+ }
76+
77+ if (empty ($ map [$ key ]['names ' ])) {
78+ continue ;
79+ }
80+
81+ if ($ map [$ key ]['iterable ' ]) {
82+ $ value = array_map (function ($ object ) use ($ map , $ key , $ allowUnknownKeys ) {
83+ if (!is_array ($ object )) {
84+ return $ object ;
85+ }
86+
87+ return self ::invokeFromNames ($ map [$ key ]['names ' ], $ object , $ allowUnknownKeys ) ?: $ object ;
88+ }, $ value );
89+ continue ;
90+ }
91+
92+ $ names = $ map [$ key ]['names ' ];
93+ $ value = self ::invokeFromNames ($ names , $ value , $ allowUnknownKeys ) ?: $ value ;
94+ }
95+
96+ return Invoke::new (self ::class, $ array );
97+ }
98+
99+ /**
100+ * @param array<string> $classNames
101+ * @param array<string,mixed> $object
102+ */
103+ private static function invokeFromNames (array $ classNames , array $ object , bool $ allowUnknownKeys ): ?object
104+ {
105+ $ lastException = null ;
106+ foreach ($ classNames as $ className ) {
107+ try {
108+ // @phpstan-ignore-next-line
109+ return call_user_func_array ($ className . '::fromArray ' , [$ object , $ allowUnknownKeys ]);
110+ } catch (Exception $ exception ) {
111+ $ lastException = $ exception ;
112+ continue ;
113+ }
114+ }
115+
116+ throw $ lastException ;
117+ }
118+
119+ }
0 commit comments