Schema Package Cheatsheet
Purpose
Cognesy\Schema maps PHP types/classes to schema objects and renders/parses JSON Schema.
Primary classes:
Cognesy\Schema\SchemaBuilderCognesy\Schema\SchemaFactoryCognesy\Schema\CallableSchemaFactoryCognesy\Schema\TypeInfoCognesy\Schema\JsonSchemaRendererCognesy\Schema\JsonSchemaParser
Quick Start
SchemaBuilder API
Create:SchemaBuilder::define(string $name, string $description = ''): SchemaBuilderSchemaBuilder::fromSchema(Schema $schema): SchemaBuilder
name(): stringdescription(): stringproperties(): array— returnsarray<string, Schema>
SchemaBuilder):
withProperty(string $name, Schema $schema, bool $required = true): SchemaBuilderwithProperties(array $properties): SchemaBuilder— acceptsarray<string|int, Schema>string(string $name, string $description = '', bool $required = true): SchemaBuilderint(string $name, string $description = '', bool $required = true): SchemaBuilderfloat(string $name, string $description = '', bool $required = true): SchemaBuilderbool(string $name, string $description = '', bool $required = true): SchemaBuilderarray(string $name, string $description = '', bool $required = true): SchemaBuilderenum(string $name, string $enumClass, string $description = '', bool $required = true): SchemaBuilderoption(string $name, array $values, string $description = '', bool $required = true): SchemaBuilder—$valuesisarray<string|int>object(string $name, string $class, string $description = '', bool $required = true): SchemaBuildercollection(string $name, string|Type|Schema $itemType, string $description = '', bool $required = true): SchemaBuildershape(string $name, callable|self|array $shape, string $description = '', bool $required = true): SchemaBuilder
schema(): ObjectSchemabuild(): ObjectSchema— alias forschema()
SchemaBuilderreuses oneSchemaFactoryinstance per builder lifecycle.
SchemaFactory API
Create factory
Main schema entrypoint
$factory->schema(mixed $anyType): Schema
Supported inputs:
SchemaSymfony\Component\TypeInfo\Type- class-string (for example
User::class) - object instance (mapped by runtime class)
CanProvideSchema(Cognesy\Schema\Contracts\CanProvideSchema)CanProvideJsonSchema(Cognesy\Utils\JsonSchema\Contracts\CanProvideJsonSchema)
Primitive helpers
string|int|float|bool(string $name = '', string $description = ''); array(string $name = '', string $description = '').
Object / enum / collection
object(string $class, string $name = '', string $description = '', array $properties = [], array $required = []): ObjectSchemaenum(string $class, string $name = '', string $description = ''): EnumSchemacollection(string $nestedType, string $name = '', string $description = '', ?Schema $nestedTypeSchema = null): CollectionSchema
- enum rendering preserves backing values exactly (
stringandint) - int-backed enums render as JSON Schema
type: integerwith integerenumvalues
From Symfony TypeInfo Type
fromType(Type $type, string $name = '', string $description = '', bool $hasDefaultValue = false, mixed $defaultValue = null): Schema
From reflection helpers
fromClassInfo(ClassInfo $classInfo): ObjectSchemafromPropertyInfo(PropertyInfo $propertyInfo): Schema
Low-level property schema
propertySchema(Type $type, string $name, string $description, ?array $enumValues = null, ?bool $nullable = null, bool $hasDefaultValue = false, mixed $defaultValue = null): Schema
Metadata override
withMetadata(Schema $schema, ?string $name = null, ?string $description = null): Schema
Parse / render JSON Schema
toJsonSchema() and renderJsonSchema() accept an optional ?callable $onObjectRef parameter.
CallableSchemaFactory API
BuildsObjectSchema from callable signatures (functions, methods, closures).
fromCallable(callable $callable, ?string $name = null, ?string $description = null): SchemafromFunctionName(string $function, ?string $name = null, ?string $description = null): SchemafromMethodName(string $class, string $method, ?string $name = null, ?string $description = null): Schema
ObjectSchema whose properties mirror the callable’s parameters.
JsonSchemaParser API
Cognesy\Schema\JsonSchemaParser implements CanParseJsonSchema.
parse(JsonSchema $jsonSchema): ObjectSchema— parses aJsonSchemainto anObjectSchemafromJsonSchema(array $jsonSchema, string $customName = '', string $customDescription = ''): ObjectSchema— convenience wrapper that accepts a raw array
JsonSchemaRenderer API
Cognesy\Schema\JsonSchemaRenderer implements CanRenderJsonSchema.
Methods:
render(Schema $schema, ?callable $onObjectRef = null): JsonSchematoArray(Schema $schema, ?callable $refCallback = null): array— returnsarray<string, mixed>
Schema Data Objects
Base type:Cognesy\Schema\Data\Schema(readonly class)
Schema):
ObjectSchemaArrayShapeSchemaCollectionSchemaArraySchemaScalarSchemaEnumSchemaObjectRefSchema
Schema constructor and public properties
Common methods on Schema
name(): stringdescription(): stringtype(): TypeisNullable(): boolhasDefaultValue(): booldefaultValue(): mixedisScalar(): boolisObject(): boolisEnum(): boolisArray(): boolhasProperties(): boolgetPropertyNames(): string[]getPropertySchemas(): array<string, Schema>getPropertySchema(string $name): SchemahasProperty(string $name): booltoArray(): array<string, mixed>
ObjectSchema and ArrayShapeSchema
Additional constructor parameters and public properties:public array $properties—array<string, Schema>public array $required—array<string>
hasProperties(), getPropertySchemas(), getPropertyNames(), getPropertySchema(), hasProperty().
CollectionSchema
Additional constructor parameter and public property:public Schema $nestedItemSchema
ScalarSchema, ArraySchema, EnumSchema, ObjectRefSchema
No additional properties or methods beyondSchema.
Default/nullability behavior:
- reflection extraction maps constructor/property/setter defaults to schema node metadata
- JSON bridge preserves
nullableanddefaultin both directions hasDefaultValue()distinguishes “no default” fromdefaultValue() === null
TypeInfo Helpers
Class:Cognesy\Schema\TypeInfo (final)
Build and normalize
fromTypeName(?string $typeName, bool $normalize = true): TypefromValue(mixed $value): TypefromJsonSchema(JsonSchema $json): Typenormalize(Type $type, bool $throwOnUnsupportedUnion = false): Type
Classification
isScalar(Type $type): boolisBool(Type $type): boolisMixed(Type $type): boolisEnum(Type $type): boolisObject(Type $type): boolisArray(Type $type): boolisCollection(Type $type): bool
Details
collectionValueType(Type $type): ?TypeclassName(Type $type): ?stringenumValues(Type $type): arrayenumBackingType(Type $type): ?stringshortName(Type $type): stringtoJsonType(Type $type): JsonSchemaTypeisDateTimeClass(Type $type): boolcacheKey(Type $type, ?array $enumValues = null): string
public static.
Reflection Helpers
ClassInfo
Cognesy\Schema\Reflection\ClassInfo
fromString(string $class): ClassInfo(static)getClass(): stringgetShortName(): stringgetPropertyNames(): string[]getProperties(): array<string, PropertyInfo>getProperty(string $name): PropertyInfogetPropertyType(string $property): TypehasProperty(string $property): boolisPublic(string $property): boolisReadOnly(string $property): boolisNullable(?string $property = null): boolgetClassDescription(): stringgetPropertyDescription(string $property): stringgetRequiredProperties(): string[]isEnum(): boolisBacked(): boolenumBackingType(): stringimplementsInterface(string $interface): boolgetFilteredPropertyNames(array $filters): string[]getFilteredProperties(array $filters): array<string, PropertyInfo>
PropertyInfo
Cognesy\Schema\Reflection\PropertyInfo
fromName(string $class, string $property): PropertyInfo(static)fromReflection(ReflectionProperty $reflection): PropertyInfo(static)getName(): stringgetType(): TypegetDescription(): stringisNullable(): boolisPublic(): boolisReadOnly(): boolisStatic(): boolisDeserializable(): boolisRequired(): boolhasDefaultValue(): booldefaultValue(): mixedhasAttribute(string $attributeClass): boolgetAttributeValues(string $attributeClass, string $attributeProperty): arraygetClass(): string
FunctionInfo
Cognesy\Schema\Reflection\FunctionInfo
fromClosure(Closure $closure): FunctionInfo(static)fromFunctionName(string $name): FunctionInfo(static)fromMethodName(string $class, string $name): FunctionInfo(static)getName(): stringgetShortName(): stringisClassMethod(): boolgetDescription(): stringgetParameterDescription(string $argument): stringhasParameter(string $name): boolisNullable(string $name): boolisOptional(string $name): boolisVariadic(string $name): boolhasDefaultValue(string $name): boolgetDefaultValue(string $name): mixedgetParameters(): array<string, ReflectionParameter>
Attributes
Description
Cognesy\Schema\Attributes\Description
Targets: class, property, method, function, parameter. Repeatable.
__construct(public string $text = '')
Instructions
Cognesy\Schema\Attributes\Instructions
Targets: class, property, method, function, parameter. Repeatable.
__construct(public string|array $text)
Methods:
get(): arrayadd(string $instruction): voidclear(): voidtext(): string
Contracts
CanProvideSchema::toSchema(): SchemaCanRenderJsonSchema::render(Schema $schema, ?callable $onObjectRef = null): JsonSchemaCanParseJsonSchema::parse(JsonSchema $jsonSchema): Schema
Utils
AttributeUtils
Cognesy\Schema\Utils\AttributeUtils
hasAttribute(ReflectionClass|ReflectionMethod|ReflectionProperty|ReflectionParameter|ReflectionFunction $element, string $attributeClass): boolgetValues(ReflectionClass|ReflectionMethod|ReflectionProperty|ReflectionParameter|ReflectionFunction $element, string $attributeClass, string $attributeProperty): array
Descriptions
Cognesy\Schema\Utils\Descriptions
Collects descriptions from #[Description], #[Instructions] attributes and docblocks.
forClass(string $class): stringforProperty(string $class, string $propertyName): stringforFunction(string $functionName): stringforMethod(string $class, string $methodName): stringforMethodParameter(string $class, string $methodName, string $parameterName): stringforFunctionParameter(string $functionName, string $parameterName): string
DocblockInfo
Cognesy\Schema\Utils\DocblockInfo (final)
summary(string $docComment): stringparameterDescription(string $docComment, string $parameterName): string
DocstringUtils
Cognesy\Schema\Utils\DocstringUtils
descriptionsOnly(string $docComment): stringgetParameterDescription(string $name, string $docComment): string
Exceptions
Cognesy\Schema\Exceptions\ReflectionException—classNotFound(),propertyNotFound(),invalidFilter(),unsupportedCallable(),parameterNotFound()Cognesy\Schema\Exceptions\SchemaMappingException—unknownSchemaType(),missingObjectClass(),invalidCollectionNestedType()Cognesy\Schema\Exceptions\SchemaParsingException—forRootType(),forMissingCollectionItems(),forUnresolvableRootSchema()Cognesy\Schema\Exceptions\TypeResolutionException—emptyTypeSpecification(),missingObjectClass(),missingEnumClass(),unsupportedType(),unsupportedUnion()