Dynamic Cheat Sheet¶
Mental Model¶
Structureis runtime state:{ data: array, schema: Schema }- Shape is always defined by
Schema(useSchemaBuilder/SchemaFactory) Structureis immutable (set()/withData()return new instances)
Quick Start¶
use Cognesy\Dynamic\Structure;
use Cognesy\Schema\SchemaBuilder;
$schema = SchemaBuilder::define('payload')
->string('name')
->bool('active', required: false)
->schema();
$payload = Structure::fromSchema($schema, ['name' => 'Ada']);
$payload = $payload->set('active', true);
$data = $payload->toArray();
Structure API¶
Create:
Structure::fromSchema(Schema $schema, array $data = []): Structure
Read:
schema(): Schemaname(): stringdescription(): stringtoSchema(): Schemadata(): arraytoArray(): arraytoJsonSchema(): arrayhas(string $name): boolget(string $name): mixed
Magic access:
isset($structure->field)is true only when runtime data contains a non-null value or the schema provides a non-null default
Mutate (immutable):
withData(array $data): Structureset(string $name, mixed $value): Structureclone(): StructurefromArray(array $data): static
Validation / transform:
validate(): ValidationResultnormalizeRecord(array $values): arraytransform(): mixedwithValidation(callable $validator): Structure— compatibility no-op, returns a clone
StructureFactory API¶
fromCallable(callable $callable, ?string $name = null, ?string $description = null): StructurefromFunctionName(string $function, ?string $name = null, ?string $description = null): StructurefromMethodName(string $class, string $method, ?string $name = null, ?string $description = null): StructurefromClass(string $class, ?string $name = null, ?string $description = null): StructurefromSchema(string $name, Schema $schema, string $description = ''): StructurefromJsonSchema(array $jsonSchema): StructurefromArrayKeyValues(string $name, array $data, string $description = ''): StructurefromString(string $name, string $typeString, string $description = ''): Structure
fromString() is a convenience shape parser for compact field lists. It supports top-level comma-separated fields, nested type strings like array{name:string}, and field descriptions in trailing parentheses.
Schema Authoring¶
Use Cognesy\Schema for shape definition:
SchemaBuilderfor fluent authoringSchemaFactoryfor type/class driven schemasCallableSchemaFactoryfor function/method signatures