getProperties() as $temp) $validDocumentAttributeNames[] = $temp->getName(); foreach((new ReflectionClass(new SchemaDocumentField("EMPTY")))->getProperties() as $temp) $validPropertyAttributeNames[] = $temp->getName(); return [$validDocumentAttributeNames, $validPropertyAttributeNames]; } /** * Checks to see if a value is "okay" for a Schema. * * @param string $key * @param mixed $value * @param array $valids * @return bool */ private static function VerifyField(string $key, mixed $value, array $valids): bool { if(!in_array(needle: $key, haystack: $valids)) return false; if($value == "") return false; return true; } /** * Just converts PascalCase to snake_case. * * @param string $input * @return string */ private static function PascalCaseToSnakeCase(string $input): string { return strtolower(preg_replace('/(?getAttributes()[0]->getArguments() as $key=>$value) if(self::VerifyField($key, $value, $validKeys[0])) $schema["@" . self::PascalCaseToSnakeCase(input: $key)] = $value; /* * Property handling from here... */ foreach ($reflection->getProperties() as $property) { $propertyName = ""; $propertySchema = []; foreach ($property->getAttributes() as $attribute) { if($attribute->getName() != SchemaDocumentField::class) continue; foreach($attribute->getArguments() as $key=>$value) { if($key == "Name") { $propertyName = $value; } elseif($key == "Child") { $propertySchema["#"] = [ "@comment" => $value->Comment, "@valid_schemas" => $value->ValidSchemas, ]; } elseif(self::VerifyField($key, $value, $validKeys[1])) { $propertySchema["@" . self::PascalCaseToSnakeCase(input: $key)] = $value; } elseif($key == "ValidSchemas") { $propertySchema["@valid_schemas"] = []; $classNameRegex = '/^(\\\\?[A-Z][a-zA-Z0-9_]*)((\\\\[A-Z][a-zA-Z0-9_]*)*)$/'; $urlRegex = '/^https?:\/\/[^\s$.?#].[^\s]*$/i'; foreach($value as $validSchema) { if (preg_match($classNameRegex, $validSchema)) $propertySchema["@valid_schemas"][] = URLHandling::GetURLForSchema($validSchema); elseif (preg_match($urlRegex, $validSchema)) $propertySchema["@valid_schemas"][] = $validSchema; } } } } if($propertyName == "") throw new SchemaDocumentFieldNameUnsetException(); $schema["$propertyName"] = $propertySchema; } return $schema; } /** * Generates the Schema using the SchemaBuilder::GenerateSchema() function, sets the http header to application/json, echos the schema as JSON, then dies. * * @param object $targetSchema The Schema class object to generate from. * @return void * @throws SchemaDocumentFieldNameUnsetException */ #[NoReturn] public static function RenderSchema(string $targetSchemaClassName): void { $result = self::GenerateSchema($targetSchemaClassName); header('Content-Type: application/json'); echo json_encode($result, JSON_PRETTY_PRINT); die(); } }