Skip to content

Commit 02e3c04

Browse files
committed
Fixed issue with date handling
1 parent af38a2a commit 02e3c04

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

examples/C07_Misc/SearchCriteria/run.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,11 @@ public function execute() {
7474
```php
7575
<?php
7676
function segment(string $data) : Search {
77-
return (new StructuredOutput)->with(
78-
messages: [[
79-
"role" => "user",
80-
"content" => "Consider the data below: '\n$data' and segment it into multiple search queries",
81-
]],
82-
responseModel: Search::class,
83-
)->get();
77+
return (new StructuredOutput)
78+
//->withDebugPreset('on')
79+
->withMessages("Consider the data below: '\n$data' and segment it into multiple search queries")
80+
->withResponseClass(Search::class)
81+
->get();
8482
}
8583

8684
$search = segment("Find a picture of a cat and a video of a dog");

packages/instructor/src/Deserialization/Deserializers/FlexibleDateDenormalizer.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function denormalize($data, string $type, ?string $format = null, array $
1515
throw new NotNormalizableValueException('The data is not a string, it cannot be converted to a DateTime.');
1616
}
1717

18-
$output = $this->parseDate($data);
18+
$output = $this->parseDate($data, $type);
1919

2020
if ($output === false) {
2121
throw new NotNormalizableValueException('The date string could not be parsed.');
@@ -40,7 +40,7 @@ public function supportsDenormalization($data, string $type, ?string $format = n
4040
]);
4141
}
4242

43-
private function parseDate(string $dateString) : DateTimeImmutable|DateTime|false {
43+
private function parseDate(string $dateString, string $targetType) : DateTimeImmutable|DateTime|false {
4444
$formats = [
4545
'Y-m-d\TH:i:s.uP', // ISO8601 with microseconds
4646
'Y-m-d\TH:i:sP', // ISO8601
@@ -54,11 +54,24 @@ private function parseDate(string $dateString) : DateTimeImmutable|DateTime|fals
5454
foreach ($formats as $format) {
5555
$date = DateTime::createFromFormat($format, $dateString);
5656
if ($date !== false) {
57-
return $date;
57+
return $this->convertToTargetType($date, $targetType);
5858
}
5959
}
6060

6161
// If none of the formats work, try a more flexible approach
62-
return date_create($dateString);
62+
$date = date_create($dateString);
63+
if ($date !== false) {
64+
return $this->convertToTargetType($date, $targetType);
65+
}
66+
67+
return false;
68+
}
69+
70+
private function convertToTargetType(DateTime $date, string $targetType): DateTimeImmutable|DateTime {
71+
if ($targetType === DateTimeImmutable::class || $targetType === DateTimeInterface::class) {
72+
return DateTimeImmutable::createFromMutable($date);
73+
}
74+
75+
return $date;
6376
}
6477
}

packages/schema/src/Factories/SchemaFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function schema(string|object $anyType) : Schema
8080
typeName: $typeString,
8181
schema: $this->makeSchema($type));
8282
}
83+
8384
return $this->schemaMap->get($anyType);
8485
}
8586

packages/schema/src/Factories/Traits/SchemaFactory/HandlesClassInfo.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public function fromPropertyInfo(PropertyInfo $propertyInfo) : Schema {
3535
*/
3636
protected function getPropertySchemas(ClassInfo $classInfo) : array {
3737
$properties = $classInfo->getProperties();
38-
3938
$propertySchemas = [];
4039
foreach ($properties as $propertyName => $propertyInfo) {
4140
if (!$propertyInfo->isDeserializable()) {

packages/schema/src/Utils/Compat/PropertyInfoV6Adapter.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public function isPropertyNullable(): bool {
6363
private function typesToUnionString(array $types): string {
6464
$result = [];
6565
foreach ($types as $type) {
66-
if (in_array($type->getBuiltinType(), TypeDetails::PHP_SCALAR_TYPES)) { $result[] = $type->getBuiltinType(); }
6766
if ($type->isCollection()) { $result[] = $this->getCollectionOrArrayType($type); }
67+
if (in_array($type->getBuiltinType(), TypeDetails::PHP_SCALAR_TYPES)) { $result[] = $type->getBuiltinType(); }
6868
if ($type->getBuiltinType() === Type::BUILTIN_TYPE_ARRAY) { $result[] = 'array'; }
6969
if ($type->getClassName()) { $result[] = $type->getClassName() ?? 'object'; }
7070
//if ($type->isNullable()) { $result[] = 'null'; }
@@ -74,19 +74,16 @@ private function typesToUnionString(array $types): string {
7474

7575
private function getCollectionOrArrayType(Type $parentType): string {
7676
$valueTypes = $parentType->getCollectionValueTypes();
77-
if (is_null($valueTypes)) {
78-
return 'array';
79-
}
8077

8178
$result = [];
8279
foreach ($valueTypes as $valueType) {
8380
if (in_array($valueType->getBuiltinType(), TypeDetails::PHP_SCALAR_TYPES)) { $result[] = $valueType->getBuiltinType() . '[]'; }
84-
if ($valueType->isCollection()) { $result[] = 'array'; } // collection of collections is considered an array
85-
if ($valueType->getBuiltinType() === Type::BUILTIN_TYPE_ARRAY) { $result[] = 'array'; }
86-
if ($valueType->getClassName()) { $result[] = $parentType->getClassName()
87-
? ($parentType->getClassName() . '[]')
81+
if (!empty($valueType->getClassName())) { $result[] = $valueType->getClassName()
82+
? ($valueType->getClassName() . '[]')
8883
: 'array'; // collection of unspecified objects is considered an array
8984
}
85+
if ($valueType->isCollection()) { $result[] = 'array'; } // collection of collections is considered an array
86+
if ($valueType->getBuiltinType() === Type::BUILTIN_TYPE_ARRAY) { $result[] = 'array'; }
9087
//if ($valueType->isNullable()) { $result[] = 'null'; }
9188
}
9289
return implode('|', $result);

0 commit comments

Comments
 (0)