Skip to content

Commit

Permalink
Port helper to the publication type class
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Dec 18, 2022
1 parent 525638c commit 4a0afa4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
protected ?OutputStyle $output = null,
) {
$canonicalFieldName = $this->pubType->canonicalField;
$canonicalFieldDefinition = $this->getCanonicalFieldDefinition($canonicalFieldName);
$canonicalFieldDefinition = $this->pubType->getCanonicalFieldDefinition();
$canonicalValue = $this->getCanonicalValue($canonicalFieldDefinition, $canonicalFieldName);
$canonicalStr = Str::of($canonicalValue)->substr(0, 64);

Expand Down Expand Up @@ -75,24 +75,6 @@ protected function handleCreate(): void
$this->save($output);
}

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

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

/** @deprecated This should be handled in the pubType */
protected function getCanonicalFieldDefinition(string $canonicalFieldName): PublicationFieldType
{
return $this->pubType->getFields()->filter(fn(PublicationFieldType $field): bool => $field->name === $canonicalFieldName)->first()
?? $this->handleMissingCanonicalField($canonicalFieldName);
}

protected function getCanonicalValue(PublicationFieldType $canonicalFieldDefinition, string $canonicalFieldName): string
{
if ($canonicalFieldDefinition->type === PublicationFieldTypes::Array) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public function getFieldRules(bool $reload = false): Collection
}), false);
}

public function getCanonicalFieldDefinition(): PublicationFieldType
{
return $this->getFields()->filter(fn(PublicationFieldType $field): bool => $field->name === $this->canonicalField)->first();
}

public function save(?string $path = null): void
{
$path ??= $this->getSchemaFile();
Expand Down

0 comments on commit 4a0afa4

Please sign in to comment.