-
Notifications
You must be signed in to change notification settings - Fork 4
Refactor: Improve API ergonomics and simplify schema builder #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
98dc0e9
Refactor: Rename Schemas to Schema and remove config
deemonic 7fcd2b7
Simplify Struct API and add OpenAI format support
deemonic 2696ace
Add declare(strict_types=1) to all PHP files
deemonic 53ee33c
Add optional description parameter to Struct::define()
deemonic f64dc42
Reorder Struct::define() parameters for better API ergonomics
deemonic 766c5d7
Make description parameter required in Struct::define()
deemonic a3b5a8f
Rename Builder to Property for better API semantics
deemonic 5694562
Fix README to remove toJson() references and add missing descriptions
deemonic f1e7353
Fix PHPStan config to remove deleted config directory
deemonic a995aaa
Fix Laravel Pint formatting: add blank line before namespace
deemonic 5a27b42
Address CodeRabbit review feedback
deemonic 35ccefe
Address additional CodeRabbit feedback
deemonic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,5 +5,4 @@ parameters: | |
| level: 9 | ||
| paths: | ||
| - src | ||
| - config | ||
| tmpDir: build/phpstan | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Blaspsoft\Forerunner\Schema; | ||
|
|
||
| class Struct implements \JsonSerializable | ||
| { | ||
| protected Property $builder; | ||
|
|
||
| protected string $name; | ||
|
|
||
| /** @var array<string, mixed>|null */ | ||
| protected ?array $cache = null; | ||
|
|
||
| protected function __construct(string $name, Property $builder) | ||
| { | ||
| $this->name = $name; | ||
| $this->builder = $builder; | ||
| } | ||
|
|
||
| /** | ||
| * Define a new structure schema. | ||
| */ | ||
| public static function define(string $name, string $description, callable $callback): self | ||
| { | ||
| $builder = new Property($name); | ||
| $builder->description($description); | ||
|
|
||
| $callback($builder); | ||
|
|
||
| return new self($name, $builder); | ||
| } | ||
|
|
||
| /** | ||
| * Convert the schema to an array. | ||
| * If strict mode is enabled, wraps the schema in OpenAI's format. | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function toArray(): array | ||
| { | ||
| if ($this->cache === null) { | ||
| $builderArray = $this->builder->toArray(); | ||
|
|
||
| // If strict mode is enabled, wrap in OpenAI's format | ||
| if ($this->builder->isStrict()) { | ||
| $this->cache = [ | ||
| 'name' => $this->name, | ||
| 'strict' => true, | ||
| 'schema' => $builderArray, | ||
| ]; | ||
| } else { | ||
| $this->cache = $builderArray; | ||
| } | ||
| } | ||
|
|
||
| return $this->cache; | ||
| } | ||
|
|
||
| /** | ||
| * Specify data which should be serialized to JSON. | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function jsonSerialize(): array | ||
| { | ||
| return $this->toArray(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.