Skip to content

Commit

Permalink
Merge branch 'publications-feature' into refactor-the-MakePublication…
Browse files Browse the repository at this point in the history
…Command-class
  • Loading branch information
caendesilva committed Dec 18, 2022
2 parents 9ec4bea + 147427c commit a256668
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Rgasch\Collection\Collection;
use RuntimeException;

use function str_starts_with;

/**
* Scaffold a publication file.
*
Expand All @@ -29,7 +31,9 @@ public function __construct(
protected ?OutputStyle $output = null,
) {
$canonicalFieldName = $this->pubType->canonicalField;
$canonicalFieldDefinition = $this->pubType->getFields()->filter(fn (PublicationFieldType $field): bool => $field->name === $canonicalFieldName)->first() ?? throw new RuntimeException("Could not find field definition for '$canonicalFieldName' which is required for this type as it's the canonical field");
$canonicalFieldDefinition = $this->pubType->getFields()->filter(fn (PublicationFieldType $field): bool => $field->name === $canonicalFieldName)->first() ?? $this->handleMissingCanonicalField(
$canonicalFieldName
);
$canonicalValue = $canonicalFieldDefinition->type !== 'array' ? $this->fieldData->{$canonicalFieldName} : $this->fieldData->{$canonicalFieldName}[0];
$canonicalStr = Str::of($canonicalValue)->substr(0, 64);

Expand Down Expand Up @@ -72,4 +76,15 @@ protected function handleCreate(): void

$this->save($output);
}

protected function handleMissingCanonicalField(string $canonicalFieldName): string
{
if (str_starts_with($canonicalFieldName, '__')) {
return $canonicalFieldName;
}

return throw new RuntimeException(
"Could not find field definition for '$canonicalFieldName' which is required for this type as it's the canonical field"
);
}
}

0 comments on commit a256668

Please sign in to comment.