-
Notifications
You must be signed in to change notification settings - Fork 35
Adds DTO array transformation and JSON serialization #30
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
36 commits
Select commit
Hold shift + click to select a range
13aaba0
chore: simplifies type
JasonTheAdams 0b52ffd
feat: adds json serialization to DTOs
JasonTheAdams 0c0407c
refactor: moves enum trait to traits directory
JasonTheAdams 03f66ff
test: adds serialization tests
JasonTheAdams a350b57
feat: improves fromJson validation and documenting
JasonTheAdams f6a1605
refactor: clean up typing with generics
JasonTheAdams cf5b304
refactor: further cleaning up of types
JasonTheAdams 404699a
refactor: improves Message::toJson types
JasonTheAdams 18ffdb4
refactor: changes to array terminology over json
JasonTheAdams cb4406e
refactor: adds abstract DTO with json serialization
JasonTheAdams f17f662
refactor: simplifies File::fromArray
JasonTheAdams 1f77a32
refactor: simplifies Message::fromArray
JasonTheAdams 3bc7064
refactor: simplifies MessagePart::fromArray
JasonTheAdams a9629c4
refactor: simplifies GenerativeAiResult
JasonTheAdams 0f8f66d
fix: corrects incorrect types
JasonTheAdams 18afbb5
refactor: simplifies GenerativeAiResult::fromArray
JasonTheAdams a66a56b
fix: corrects TokenUsage types
JasonTheAdams 5bef6fb
refactor: simplifies FunctionCall
JasonTheAdams eac5146
refactor: simplifies Tool::fromArray
JasonTheAdams 2811597
refactor: simplifies WebSearch
JasonTheAdams 3e36f0f
refactor: simplifies Fiel and MessagePart toArray
JasonTheAdams b6890c8
chore: corrects too specific Message return type
JasonTheAdams cb985cc
fix: adds missing imports
JasonTheAdams 25f0491
feat: adds fromArray key validation
JasonTheAdams 87d9c67
fix: corrects linting issue
JasonTheAdams 30ee59c
refactor: adds schema constants
JasonTheAdams 59514c3
test: adds missing import
JasonTheAdams 59e586c
refactor: removes need for final DTOs
JasonTheAdams c4c6452
fix: checks for key existence, allowing null values
JasonTheAdams 342c07a
refacor: removes non-required key validation
JasonTheAdams 8ded94d
feat: validates that successful oepration has result
JasonTheAdams 6b48bef
refactor: removes potentially problematic schema condition
JasonTheAdams 3eb5279
fix: corrects either id or name to be required
JasonTheAdams 7d53157
fix: removes unnecessary Tool type validation
JasonTheAdams 75a8034
chore: marks mimeType required
JasonTheAdams 19b4692
chore: fixes since tags
JasonTheAdams 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
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,131 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace WordPress\AiClient\Common; | ||
|
|
||
| use InvalidArgumentException; | ||
| use JsonSerializable; | ||
| use stdClass; | ||
| use WordPress\AiClient\Common\Contracts\WithArrayTransformationInterface; | ||
| use WordPress\AiClient\Common\Contracts\WithJsonSchemaInterface; | ||
|
|
||
| /** | ||
| * Abstract base class for all Data Value Objects in the AI Client. | ||
| * | ||
| * This abstract class consolidates the common functionality needed by all | ||
| * data transfer objects: | ||
| * - Array transformation for data manipulation | ||
| * - JSON schema support for validation and documentation | ||
| * - JSON serialization with proper empty object handling | ||
| * | ||
| * All DTOs in the AI Client should extend this class to ensure | ||
| * consistent behavior across the codebase. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @template TArrayShape of array<string, mixed> | ||
| * @implements WithArrayTransformationInterface<TArrayShape> | ||
| */ | ||
| abstract class AbstractDataValueObject implements | ||
| WithArrayTransformationInterface, | ||
| WithJsonSchemaInterface, | ||
| JsonSerializable | ||
| { | ||
| /** | ||
| * Validates that required keys exist in the array data. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param TArrayShape $data The array data to validate. | ||
| * @param string[] $requiredKeys The keys that must be present. | ||
| * @throws InvalidArgumentException If any required key is missing. | ||
| */ | ||
| protected static function validateFromArrayData(array $data, array $requiredKeys): void | ||
| { | ||
| $missingKeys = []; | ||
|
|
||
| foreach ($requiredKeys as $key) { | ||
| if (!array_key_exists($key, $data)) { | ||
| $missingKeys[] = $key; | ||
| } | ||
| } | ||
|
|
||
| if (!empty($missingKeys)) { | ||
| throw new InvalidArgumentException( | ||
| sprintf( | ||
| '%s::fromArray() missing required keys: %s', | ||
| static::class, | ||
| implode(', ', $missingKeys) | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Converts the object to a JSON-serializable format. | ||
| * | ||
| * This method uses the toArray() method and then processes the result | ||
| * based on the JSON schema to ensure proper object representation for | ||
| * empty arrays. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return mixed The JSON-serializable representation. | ||
| */ | ||
| #[\ReturnTypeWillChange] | ||
| public function jsonSerialize() | ||
| { | ||
| $data = $this->toArray(); | ||
| $schema = static::getJsonSchema(); | ||
|
|
||
| return $this->convertEmptyArraysToObjects($data, $schema); | ||
| } | ||
|
|
||
| /** | ||
| * Recursively converts empty arrays to stdClass objects where the schema expects objects. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param mixed $data The data to process. | ||
| * @param array<mixed, mixed> $schema The JSON schema for the data. | ||
| * @return mixed The processed data. | ||
| */ | ||
| private function convertEmptyArraysToObjects($data, array $schema) | ||
| { | ||
| // If data is an empty array and schema expects object, convert to stdClass | ||
| if (is_array($data) && empty($data) && isset($schema['type']) && $schema['type'] === 'object') { | ||
| return new stdClass(); | ||
| } | ||
|
|
||
| // If data is an array with content, recursively process nested structures | ||
| if (is_array($data)) { | ||
| // Handle object properties | ||
| if (isset($schema['properties']) && is_array($schema['properties'])) { | ||
| foreach ($data as $key => $value) { | ||
| if (isset($schema['properties'][$key]) && is_array($schema['properties'][$key])) { | ||
| $data[$key] = $this->convertEmptyArraysToObjects($value, $schema['properties'][$key]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Handle array items | ||
| if (isset($schema['items']) && is_array($schema['items'])) { | ||
| foreach ($data as $index => $item) { | ||
| $data[$index] = $this->convertEmptyArraysToObjects($item, $schema['items']); | ||
| } | ||
| } | ||
|
|
||
| // Handle oneOf schemas - just use the first one | ||
| if (isset($schema['oneOf']) && is_array($schema['oneOf'])) { | ||
| foreach ($schema['oneOf'] as $possibleSchema) { | ||
| if (is_array($possibleSchema)) { | ||
| return $this->convertEmptyArraysToObjects($data, $possibleSchema); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $data; | ||
| } | ||
| } |
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,34 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace WordPress\AiClient\Common\Contracts; | ||
|
|
||
| /** | ||
| * Interface for objects that support array transformation. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @template TArrayShape of array<string, mixed> | ||
| */ | ||
| interface WithArrayTransformationInterface | ||
| { | ||
| /** | ||
| * Converts the object to an array representation. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return TArrayShape The array representation. | ||
| */ | ||
| public function toArray(): array; | ||
|
|
||
| /** | ||
| * Creates an instance from array data. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param TArrayShape $array The array data. | ||
| * @return self<TArrayShape> The created instance. | ||
| */ | ||
| public static function fromArray(array $array): self; | ||
| } | ||
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
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.