← All changes
|
vendor/wordpress/php-mcp-schema/src/Common/Traits/ValidatesRequiredFields.php
+18
-0
2.3.2
→
trunk
View file →
| @@ -184,8 +184,26 @@ | ||
| 184 | 184 | return $value === null ? null : self::asArray($value); |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | /** |
| 188 | + * Returns the entries of $data whose keys the caller does not model. | |
| 189 | + * | |
| 190 | + * Used by types the MCP schema declares open (`[key: string]: unknown`), | |
| 191 | + * so that unrecognized fields survive a fromArray()/toArray() round trip | |
| 192 | + * instead of being silently discarded. | |
| 193 | + * | |
| 194 | + * @param array<string, mixed> $data | |
| 195 | + * @param array<int, string> $known | |
| 196 | + * @return array<string, mixed>|null | |
| 197 | + */ | |
| 198 | + protected static function additionalFields(array $data, array $known): ?array | |
| 199 | + { | |
| 200 | + $additional = array_diff_key($data, array_flip($known)); | |
| 201 | + | |
| 202 | + return $additional === [] ? null : $additional; | |
| 203 | + } | |
| 204 | + | |
| 205 | + /** | |
| 188 | 206 | * Asserts a value is an object and returns it. |
| 189 | 207 | * |
| 190 | 208 | * Accepts both PHP arrays and objects, auto-converting arrays to objects. |
| 191 | 209 | * This aligns with MCP spec where JSON objects can be PHP arrays or objects. |