Skip to content

Commit 8f308f6

Browse files
committed
wip
1 parent 98b327f commit 8f308f6

19 files changed

Lines changed: 576 additions & 94 deletions

src/Data/DOIData.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function __construct(
9393
public int $versionCount,
9494
public int $versionOfCount,
9595
public DateTimeImmutable $created,
96-
public DateTimeImmutable $registered,
96+
public ?DateTimeImmutable $registered,
9797
public ?string $published,
9898
public DateTimeImmutable $updated,
9999
public RelationshipData $relationships,
@@ -204,10 +204,13 @@ public static function fromArray(array $data): self
204204
$publisherData = PublisherData::fromArray($publisherArray);
205205
}
206206

207-
$creators = array_map(
208-
fn (array $item): Creator => Creator::fromArray($item),
209-
$creatorsData
210-
);
207+
$creators = array_values(array_filter(
208+
array_map(
209+
fn (array $item): ?Creator => Creator::fromArray($item),
210+
$creatorsData
211+
),
212+
fn (?Creator $creator): bool => $creator instanceof \VincentAuger\DataCiteSdk\Data\Metadata\Creator
213+
));
211214
assert($creators !== [], 'At least one creator is required');
212215

213216
$titles = array_map(
@@ -239,10 +242,13 @@ public static function fromArray(array $data): self
239242
fn (array $item): Subject => Subject::fromArray($item),
240243
$subjectsData
241244
),
242-
contributors: array_map(
243-
fn (array $item): Contributor => Contributor::fromArray($item),
244-
$contributorsData
245-
),
245+
contributors: array_values(array_filter(
246+
array_map(
247+
fn (array $item): ?Contributor => Contributor::fromArray($item),
248+
$contributorsData
249+
),
250+
fn (?Contributor $contributor): bool => $contributor instanceof \VincentAuger\DataCiteSdk\Data\Metadata\Contributor
251+
)),
246252
dates: array_map(
247253
fn (array $item): Date => Date::fromArray($item),
248254
$datesData
@@ -264,10 +270,13 @@ public static function fromArray(array $data): self
264270
fn (array $item): RightsList => RightsList::fromArray($item),
265271
$rightsListData
266272
),
267-
descriptions: array_map(
268-
fn (array $item): Description => Description::fromArray($item),
269-
$descriptionsData
270-
),
273+
descriptions: array_values(array_filter(
274+
array_map(
275+
fn (array $item): ?Description => Description::fromArray($item),
276+
$descriptionsData
277+
),
278+
fn (?Description $description): bool => $description instanceof \VincentAuger\DataCiteSdk\Data\Metadata\Description
279+
)),
271280
geoLocations: array_map(
272281
fn (array $item): GeoLocation => GeoLocation::fromArray($item),
273282
$geoLocationsData
@@ -297,7 +306,9 @@ public static function fromArray(array $data): self
297306
versionCount: (int) $attributes['versionCount'],
298307
versionOfCount: (int) $attributes['versionOfCount'],
299308
created: new DateTimeImmutable($attributes['created']),
300-
registered: new DateTimeImmutable($attributes['registered']),
309+
registered: empty($attributes['registered'])
310+
? null
311+
: new DateTimeImmutable($attributes['registered']),
301312
published: $attributes['published'] ?? null,
302313
updated: new DateTimeImmutable($attributes['updated']),
303314
relationships: RelationshipData::fromArray($relationshipsData),

src/Data/Metadata/Contributor.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,23 @@ public function __construct(
3333
) {}
3434

3535
/**
36+
* Create from array, lenient parsing for API responses.
37+
* Returns null if required fields are missing (name or contributorType).
38+
*
3639
* @param array<string, mixed> $data
3740
*/
38-
public static function fromArray(array $data): self
41+
public static function fromArray(array $data): ?self
3942
{
40-
assert(is_string($data['name']));
43+
// Skip invalid contributors from API responses (legacy/incomplete data)
44+
if (! isset($data['name']) || ! is_string($data['name']) || $data['name'] === '') {
45+
return null;
46+
}
47+
48+
if (! isset($data['contributorType']) || ! is_string($data['contributorType']) || $data['contributorType'] === '') {
49+
return null;
50+
}
51+
4152
assert(is_array($data['affiliation']));
42-
assert(is_string($data['contributorType']));
4353
assert(is_array($data['nameIdentifiers']));
4454

4555
/** @var array<array<string, mixed>> $nameIdentifiersData */

src/Data/Metadata/Creator.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,18 @@ public function __construct(
2424
) {}
2525

2626
/**
27+
* Create from array, lenient parsing for API responses.
28+
* Returns null if required name field is missing.
29+
*
2730
* @param array<string, mixed> $data
2831
*/
29-
public static function fromArray(array $data): self
32+
public static function fromArray(array $data): ?self
3033
{
31-
assert(is_string($data['name']));
34+
// Skip invalid creators from API responses (legacy/incomplete data)
35+
if (! isset($data['name']) || ! is_string($data['name']) || $data['name'] === '') {
36+
return null;
37+
}
38+
3239
assert(is_array($data['affiliation']));
3340
assert(is_array($data['nameIdentifiers']));
3441

src/Data/Metadata/Description.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,21 @@ public function __construct(
3030
) {}
3131

3232
/**
33+
* Create from array, lenient parsing for API responses.
34+
* Returns null if required fields are missing (description or descriptionType).
35+
*
3336
* @param array<string, mixed> $data
3437
*/
35-
public static function fromArray(array $data): self
38+
public static function fromArray(array $data): ?self
3639
{
37-
assert(is_string($data['description']));
38-
assert(is_string($data['descriptionType']));
40+
// Skip invalid descriptions from API responses (legacy/incomplete data)
41+
if (! isset($data['description']) || ! is_string($data['description']) || $data['description'] === '') {
42+
return null;
43+
}
44+
45+
if (! isset($data['descriptionType']) || ! is_string($data['descriptionType']) || $data['descriptionType'] === '') {
46+
return null;
47+
}
3948

4049
return new self(
4150
lang: isset($data['lang']) && is_string($data['lang']) ? $data['lang'] : null,

0 commit comments

Comments
 (0)