| 1 |
<?php |
| 2 |
|
| 3 |
namespace LearnPress\MCP\Support; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
/** |
| 8 |
* Shared JSON schema builders for Phase 2 MCP tools. |
| 9 |
* |
| 10 |
* Keeps every input schema strict (`additionalProperties=false`) and reuses |
| 11 |
* common field shapes so the per-domain schema providers stay small. |
| 12 |
*/ |
| 13 |
class Schemas { |
| 14 |
|
| 15 |
/** |
| 16 |
* Allowed post statuses for Phase 2 write tools. |
| 17 |
* |
| 18 |
* @return string[] |
| 19 |
*/ |
| 20 |
public static function allowed_statuses(): array { |
| 21 |
return array( 'draft', 'publish', 'pending' ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Build a strict object schema. |
| 26 |
* |
| 27 |
* @param array $properties Property schemas keyed by name. |
| 28 |
* @param string[] $required Required property names. |
| 29 |
* |
| 30 |
* @return array |
| 31 |
*/ |
| 32 |
public static function object( array $properties, array $required = array() ): array { |
| 33 |
$schema = array( |
| 34 |
'type' => 'object', |
| 35 |
'additionalProperties' => false, |
| 36 |
'properties' => $properties, |
| 37 |
); |
| 38 |
|
| 39 |
if ( ! empty( $required ) ) { |
| 40 |
$schema['required'] = array_values( $required ); |
| 41 |
} |
| 42 |
|
| 43 |
return $schema; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Positive integer ID schema. |
| 48 |
* |
| 49 |
* @return array |
| 50 |
*/ |
| 51 |
public static function id(): array { |
| 52 |
return array( |
| 53 |
'type' => 'integer', |
| 54 |
'minimum' => 1, |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Strict object schema requiring a single positive integer ID field. |
| 60 |
* |
| 61 |
* @param string $id Field name (e.g. "course_id"). |
| 62 |
* |
| 63 |
* @return array |
| 64 |
*/ |
| 65 |
public static function required_id( string $id ): array { |
| 66 |
return self::object( array( $id => self::id() ), array( $id ) ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Post status enum schema restricted to the Phase 2 allowlist. |
| 71 |
* |
| 72 |
* @return array |
| 73 |
*/ |
| 74 |
public static function status(): array { |
| 75 |
return array( |
| 76 |
'type' => 'string', |
| 77 |
'enum' => self::allowed_statuses(), |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* String schema. |
| 83 |
* |
| 84 |
* @return array |
| 85 |
*/ |
| 86 |
public static function string(): array { |
| 87 |
return array( 'type' => 'string' ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Boolean schema. |
| 92 |
* |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
public static function boolean(): array { |
| 96 |
return array( 'type' => 'boolean' ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Number schema with optional minimum. |
| 101 |
* |
| 102 |
* @param float|null $minimum Optional minimum value. |
| 103 |
* |
| 104 |
* @return array |
| 105 |
*/ |
| 106 |
public static function number( ?float $minimum = null ): array { |
| 107 |
$schema = array( 'type' => 'number' ); |
| 108 |
if ( null !== $minimum ) { |
| 109 |
$schema['minimum'] = $minimum; |
| 110 |
} |
| 111 |
|
| 112 |
return $schema; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Integer schema with optional minimum. |
| 117 |
* |
| 118 |
* @param int|null $minimum Optional minimum value. |
| 119 |
* |
| 120 |
* @return array |
| 121 |
*/ |
| 122 |
public static function integer( ?int $minimum = null ): array { |
| 123 |
$schema = array( 'type' => 'integer' ); |
| 124 |
if ( null !== $minimum ) { |
| 125 |
$schema['minimum'] = $minimum; |
| 126 |
} |
| 127 |
|
| 128 |
return $schema; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Array-of-positive-IDs schema. |
| 133 |
* |
| 134 |
* @return array |
| 135 |
*/ |
| 136 |
public static function id_array(): array { |
| 137 |
return array( |
| 138 |
'type' => 'array', |
| 139 |
'items' => self::id(), |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Array-of-strings schema. |
| 145 |
* |
| 146 |
* @return array |
| 147 |
*/ |
| 148 |
public static function string_array(): array { |
| 149 |
return array( |
| 150 |
'type' => 'array', |
| 151 |
'items' => self::string(), |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Strict object output wrapper schema, e.g. { "course": { ... } }. |
| 157 |
* |
| 158 |
* @param string $key Wrapper key. |
| 159 |
* |
| 160 |
* @return array |
| 161 |
*/ |
| 162 |
public static function object_output( string $key ): array { |
| 163 |
return self::object( |
| 164 |
array( $key => array( 'type' => 'object' ) ), |
| 165 |
array( $key ) |
| 166 |
); |
| 167 |
} |
| 168 |
} |
| 169 |
|