Skip to content

Commit

Permalink
Support for dynamic option field in structures (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Dec 30, 2024
1 parent 16effee commit f0a955c
Show file tree
Hide file tree
Showing 10 changed files with 419 additions and 530 deletions.
4 changes: 3 additions & 1 deletion examples/A02_Advanced/Structures/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ enum Role : string {
Field::int('age', 'Age of the person')->validIf(
fn($value) => $value > 0, "Age has to be positive number"
),
Field::option('gender', ['male', 'female'], 'Gender of the person')->optional(),
Field::structure('address', [
Field::string('street', 'Street name')->optional(),
Field::string('city', 'City name'),
Expand All @@ -57,7 +58,7 @@ enum Role : string {

$text = <<<TEXT
Jane Doe lives in Springfield, 50210. She is 25 years old and works as manager at McDonald's.
McDonald's in Ney York is located at 456 Elm St, NYC, 12345. His favourite books are "The Lord
McDonald's in Ney York is located at 456 Elm St, NYC, 12345. Her favourite books are "The Lord
of the Rings" and "The Hobbit" by JRR Tolkien.
TEXT;

Expand All @@ -70,6 +71,7 @@ enum Role : string {
print("OUTPUT:\n");
print("Name: " . $person->name . "\n");
print("Age: " . $person->age . "\n");
print("Gender: " . $person->gender . "\n");
print("Address / city: " . $person->address->city . "\n");
print("Address / ZIP: " . $person->address->zip . "\n");
print("Role: " . $person->role->value . "\n");
Expand Down
6 changes: 6 additions & 0 deletions src/Extras/Structure/Traits/Field/HandlesFieldDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ static public function enum(string $name, string $enumClass, string $description
return new Field($name, $description, $type);
}

static public function option(string $name, array $values, string $description = '') : self {
$factory = new TypeDetailsFactory();
$type = $factory->optionType($values);
return new Field($name, $description, $type);
}

static public function object(string $name, string $class, string $description = '') : self {
$factory = new TypeDetailsFactory();
$type = $factory->objectType($class);
Expand Down
2 changes: 2 additions & 0 deletions src/Features/Schema/Contracts/CanVisitSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Cognesy\Instructor\Features\Schema\Data\Schema\EnumSchema;
use Cognesy\Instructor\Features\Schema\Data\Schema\ObjectRefSchema;
use Cognesy\Instructor\Features\Schema\Data\Schema\ObjectSchema;
use Cognesy\Instructor\Features\Schema\Data\Schema\OptionSchema;
use Cognesy\Instructor\Features\Schema\Data\Schema\ScalarSchema;
use Cognesy\Instructor\Features\Schema\Data\Schema\Schema;

Expand All @@ -22,4 +23,5 @@ public function visitObjectSchema(ObjectSchema $schema): void;
public function visitEnumSchema(EnumSchema $schema): void;
public function visitScalarSchema(ScalarSchema $schema): void;
public function visitObjectRefSchema(ObjectRefSchema $schema): void;
public function visitOptionSchema(OptionSchema $param) : void;
}
12 changes: 12 additions & 0 deletions src/Features/Schema/Data/Schema/OptionSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Cognesy\Instructor\Features\Schema\Data\Schema;

use Cognesy\Instructor\Features\Schema\Contracts\CanVisitSchema;

class OptionSchema extends Schema
{
public function accept(CanVisitSchema $visitor): void {
$visitor->visitOptionSchema($this);
}
}
1 change: 1 addition & 0 deletions src/Features/Schema/Data/TypeDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class TypeDetails
* @param TypeDetails|null $nestedType for arrays OR null
* @param string|null $enumType for enums OR null
* @param array|null $enumValues for enums OR null
* @param string|null $docString
*/
public function __construct(
public string $type,
Expand Down
Loading

0 comments on commit f0a955c

Please sign in to comment.