Skip to content
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

Refactor the make:publication command class #774

Merged
merged 150 commits into from
Dec 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
150 commits
Select commit Hold shift + click to select a range
1eb4e76
Expand abbreviated parameter
caendesilva Dec 24, 2022
7133352
Add strict types to closure
caendesilva Dec 24, 2022
b575e9d
Refactor collection mapper to foreach loop
caendesilva Dec 24, 2022
b6a01a5
Use the helper which returns already instantiated field type objects
caendesilva Dec 24, 2022
ecd0a13
Expand abbreviated parameter
caendesilva Dec 24, 2022
5534f0f
Refactor to use existing property instead of passing around variable
caendesilva Dec 24, 2022
ecec618
Move method call from argument to body
caendesilva Dec 24, 2022
59dd193
Inline local variable
caendesilva Dec 24, 2022
1acfc68
Replace 'if' blocks with single 'match' expression
caendesilva Dec 24, 2022
860c434
Normalize output style
caendesilva Dec 24, 2022
c44155f
Add a newline before each new field
caendesilva Dec 24, 2022
0713d7a
Normalize output style
caendesilva Dec 24, 2022
ddf31a8
Refactor and test the image input handler
caendesilva Dec 24, 2022
38d02b5
Support images as canonical
caendesilva Dec 24, 2022
781d65f
Apply fixes from StyleCI
StyleCIBot Dec 24, 2022
1ddb109
Create the testing directory
caendesilva Dec 24, 2022
6ebf50d
Merge branch 'refactor-MakePublicationCommand-class' of github.com:hy…
caendesilva Dec 24, 2022
209a198
Update tests to use date as canonical field
caendesilva Dec 25, 2022
d607c3a
Extract assertion helpers
caendesilva Dec 25, 2022
10dc625
Simplify tester calls
caendesilva Dec 25, 2022
ad574dd
Force string return for test helper
caendesilva Dec 25, 2022
1b99c8f
Apply fixes from StyleCI
StyleCIBot Dec 25, 2022
c257a9e
Use built in testing helper
caendesilva Dec 25, 2022
a53a51b
Merge branch 'refactor-MakePublicationCommand-class' of github.com:hy…
caendesilva Dec 25, 2022
de8853b
Test command with tag input
caendesilva Dec 25, 2022
84c35f6
Add todo
caendesilva Dec 25, 2022
4480cff
Use the getValuesForTagName method
caendesilva Dec 25, 2022
40ca68b
Use identifier instead of name for tag group
caendesilva Dec 25, 2022
c5d9660
Use method helper instead of magic property call
caendesilva Dec 25, 2022
dcdafb9
Add helper to format a tip
caendesilva Dec 25, 2022
ca15801
Update information output
caendesilva Dec 25, 2022
a842093
Encode array to JSON for test mock data
caendesilva Dec 26, 2022
b5b3529
Implement handling for when there are no media files
caendesilva Dec 26, 2022
534004f
Apply fixes from StyleCI
StyleCIBot Dec 26, 2022
1e11625
Refactor tag selection to use built-in choice helper
caendesilva Dec 26, 2022
b7c1034
Reimplement reload option
caendesilva Dec 26, 2022
3a668e3
Style reload option to make it clear it's not a tag
caendesilva Dec 26, 2022
6a3abaa
Apply fixes from StyleCI
StyleCIBot Dec 26, 2022
a749d1d
Support multiple tag selections
caendesilva Dec 26, 2022
2cfd4f5
Implode tag array
caendesilva Dec 26, 2022
76c9bbc
Display tip about how to select multiple values
caendesilva Dec 26, 2022
531cc00
Revert "Implode tag array"
caendesilva Dec 26, 2022
d39e1e9
Add 'array|string' as the function's return type
caendesilva Dec 26, 2022
fee184a
Remove unused mock
caendesilva Dec 26, 2022
e89eeec
Implement handling for when there are no tags
caendesilva Dec 26, 2022
fe0e5d7
Remove removed range logic
caendesilva Dec 26, 2022
568b79e
Remove redundant arguments in test
caendesilva Dec 26, 2022
a91db76
Extract testing method
caendesilva Dec 26, 2022
3a61446
Extract method to only test changed content without boilerplate
caendesilva Dec 26, 2022
f6b1577
Use existing assertion
caendesilva Dec 26, 2022
4695612
Convert string literal to NOWDOC
caendesilva Dec 26, 2022
abc8635
Code format
caendesilva Dec 26, 2022
d7b63ca
Output via console helper instead of Symfony output style
caendesilva Dec 26, 2022
00a3a0e
Extract helper for formatting an ISH message consistently
caendesilva Dec 26, 2022
07bf7ec
Update message
caendesilva Dec 26, 2022
ad2b3e1
Clarify helper method name
caendesilva Dec 26, 2022
6aa9e39
Add PHPDoc
caendesilva Dec 26, 2022
def6553
Test the added method
caendesilva Dec 26, 2022
f5bcc07
Merge branch 'publications-feature' into refactor-MakePublicationComm…
caendesilva Dec 26, 2022
27ca755
Refactor to return null instead of empty string when field is skipped
caendesilva Dec 26, 2022
663ac0c
Add todo
caendesilva Dec 26, 2022
4af9e3a
Revert "Support images as canonical"
caendesilva Dec 26, 2022
aa22a20
Merge tip comment into initial output message
caendesilva Dec 26, 2022
5612ddf
Merge tip into option name
caendesilva Dec 26, 2022
85b6e92
Clarify exactly what is being reloaded
caendesilva Dec 26, 2022
c044cae
Add expectations for warning outputs
caendesilva Dec 26, 2022
9980e63
Extract method
caendesilva Dec 26, 2022
034b3f2
Convert local variables to parameters
caendesilva Dec 26, 2022
4366529
Add strong types
caendesilva Dec 26, 2022
2438a14
Replace "\n" with ->newLine
caendesilva Dec 26, 2022
a51c92f
Bring out warning line from helper method
caendesilva Dec 26, 2022
39ca87c
Normalize empty state handling
caendesilva Dec 26, 2022
953de4a
Merge message back into helper method as parameter
caendesilva Dec 26, 2022
e3bcbe0
Reformat test file
caendesilva Dec 26, 2022
a3b45c1
Apply fixes from StyleCI
StyleCIBot Dec 26, 2022
f8f0664
Remove throws tag
caendesilva Dec 26, 2022
f733b53
Format PHPDocs
caendesilva Dec 26, 2022
892e0aa
Update type annotation
caendesilva Dec 26, 2022
3835f6a
Expand method name abbreviation
caendesilva Dec 26, 2022
3e9dcee
Use lowercase for output
caendesilva Dec 26, 2022
7caf556
Expect lowercase output
caendesilva Dec 26, 2022
40f5971
Return array from method that's only used once and that time as an array
caendesilva Dec 26, 2022
b7f9f21
Unwrap unnecessary type juggling
caendesilva Dec 26, 2022
25121f6
Inline simplified helper method
caendesilva Dec 26, 2022
086cd3e
Reorder class methods and reformat code
caendesilva Dec 26, 2022
fe887d8
Use the newLine helper method
caendesilva Dec 26, 2022
2a2a616
Apply fixes from StyleCI
StyleCIBot Dec 26, 2022
e74c3de
Remove unnecessary array key
caendesilva Dec 26, 2022
0353209
Reorder helper methods
caendesilva Dec 26, 2022
0aa07b3
Use lowercase for common noun in command signature documentation
caendesilva Dec 26, 2022
4011d5c
Use assertFileExists
caendesilva Dec 26, 2022
04f3ce6
Merge branch 'publications-feature' into refactor-MakePublicationComm…
caendesilva Dec 26, 2022
2e9d82d
Apply fixes from StyleCI
StyleCIBot Dec 26, 2022
4032678
Remove Copilot context comments
caendesilva Dec 26, 2022
ed7e026
Test the command with multiple tag inputs
caendesilva Dec 26, 2022
ca15d41
Merge branch 'refactor-MakePublicationCommand-class' of github.com:hy…
caendesilva Dec 26, 2022
e6428ee
Apply fixes from StyleCI
StyleCIBot Dec 26, 2022
f38bde9
Merge branch 'publications-feature' into refactor-MakePublicationComm…
caendesilva Dec 26, 2022
f884a55
Revert "Temporarily skip all tests in file"
caendesilva Dec 26, 2022
a21d4e2
Expect serialized dates to be DateTime objects
caendesilva Dec 26, 2022
4a41aad
Expect multi-line YAML to be single-quoted
caendesilva Dec 26, 2022
1a7c723
Increase expected indentation level to 4
caendesilva Dec 26, 2022
f18f0ee
Multiple tags are now properly serialized
caendesilva Dec 26, 2022
598578b
Expect values to be null when not set
caendesilva Dec 26, 2022
3cfff5a
Remove support for using arrays as canonical as that is not reasonable
caendesilva Dec 26, 2022
2c2ea99
Remove unused parameter
caendesilva Dec 26, 2022
364bbc1
Cast to string and replace ?? with ?:
caendesilva Dec 26, 2022
b404212
Add a line break
caendesilva Dec 26, 2022
ecf007e
Remove unused variable 'canonicalFieldDefinition'
caendesilva Dec 26, 2022
12c64a3
Apply fixes from StyleCI
StyleCIBot Dec 26, 2022
24ec8bc
Merge branch 'refactor-MakePublicationCommand-class' of github.com:hy…
caendesilva Dec 26, 2022
090f2cd
Inline local variable chain
caendesilva Dec 26, 2022
a25af3e
Revert "Inline local variable chain"
caendesilva Dec 26, 2022
638765c
Extract method
caendesilva Dec 26, 2022
a05111c
Inline local variable
caendesilva Dec 26, 2022
d9d93a9
Replace and inline Stringable method chains native PHP functions
caendesilva Dec 26, 2022
67df186
Unwrap duplicate string formatter
caendesilva Dec 26, 2022
e80e1c4
Inline method parameter
caendesilva Dec 26, 2022
d86b1c8
Apply fixes from StyleCI
StyleCIBot Dec 26, 2022
63941ef
Shift data normalization calls
caendesilva Dec 26, 2022
25e17ef
Inline parameter
caendesilva Dec 26, 2022
7973b9c
Inline variable
caendesilva Dec 26, 2022
7d9e711
Unwrap unnecessary formatting just to reparse the object right away
caendesilva Dec 26, 2022
2e7b9e4
Remove unnecessary parentheses
caendesilva Dec 26, 2022
1437021
Extract method
caendesilva Dec 26, 2022
1713eb2
Move up helper method
caendesilva Dec 26, 2022
7baed4b
Change case of local variable
caendesilva Dec 26, 2022
bbdb008
Remove null safe operator as all data is now from the same source
caendesilva Dec 26, 2022
3cf39ff
Apply fixes from StyleCI
StyleCIBot Dec 26, 2022
3add3fa
Import used function
caendesilva Dec 26, 2022
1814179
Convert HEREDOC to a string literal
caendesilva Dec 26, 2022
9205689
Apply fixes from StyleCI
StyleCIBot Dec 26, 2022
9b17e20
Inline local constructor variables
caendesilva Dec 26, 2022
723b6cd
Refactor to write to output from command instead of action
caendesilva Dec 26, 2022
90a8d29
Remove output style from action
caendesilva Dec 26, 2022
26987f3
Update selection check to expect an array to be returned
caendesilva Dec 26, 2022
2d03cdf
Cast result to always be array
caendesilva Dec 26, 2022
3eafcbe
Also reload if the selection contains the reload option
caendesilva Dec 26, 2022
46a2a93
Introduce local variable
caendesilva Dec 26, 2022
afba9f1
Add clarifying parentheses
caendesilva Dec 26, 2022
f570962
Apply fixes from StyleCI
StyleCIBot Dec 26, 2022
43906cd
Revert "Cast result to always be array"
caendesilva Dec 26, 2022
ee10845
Cast only haystack to array
caendesilva Dec 26, 2022
b572d07
Unwrap initial condition covered by the second block
caendesilva Dec 26, 2022
59ebb94
Inline simple helper method
caendesilva Dec 26, 2022
becab2e
Add newline
caendesilva Dec 26, 2022
c93a64c
Normalize output to match the make:page command
caendesilva Dec 26, 2022
b457742
Remove newline
caendesilva Dec 26, 2022
629a345
Apply fixes from StyleCI
StyleCIBot Dec 26, 2022
849079e
Rename helper method
caendesilva Dec 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 93 additions & 98 deletions packages/framework/src/Console/Commands/MakePublicationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Hyde\Console\Commands;

use function array_merge;
use Hyde\Console\Commands\Helpers\InputStreamHandler;
use Hyde\Console\Concerns\ValidatingCommand;
use Hyde\Framework\Actions\CreatesNewPublicationPage;
Expand All @@ -12,8 +13,8 @@
use Hyde\Framework\Features\Publications\PublicationFieldTypes;
use Hyde\Framework\Features\Publications\PublicationService;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use function implode;
use function in_array;
use InvalidArgumentException;
use LaravelZero\Framework\Commands\Command;

Expand All @@ -27,172 +28,166 @@ class MakePublicationCommand extends ValidatingCommand
{
/** @var string */
protected $signature = 'make:publication
{publicationType? : The name of the PublicationType to create a publication for}
{publicationType? : The name of the publication type to create a publication for}
{--force : Should the generated file overwrite existing publications with the same filename?}';

/** @var string */
protected $description = 'Create a new publication item';

protected PublicationType $pubType;
protected PublicationType $publicationType;

public function safeHandle(): int
{
$this->title('Creating a new Publication!');
$this->title('Creating a new publication!');

$this->pubType = $this->getPubTypeSelection($this->getPublicationTypes());
$fieldData = $this->collectFieldData($this->pubType);
$this->publicationType = $this->getPublicationTypeSelection();

$creator = new CreatesNewPublicationPage($this->pubType, $fieldData, $this->hasForceOption(), $this->output);
$fieldData = $this->collectFieldData();

$creator = new CreatesNewPublicationPage($this->publicationType, $fieldData, (bool) $this->option('force'));
if ($creator->hasFileConflict()) {
$this->error('Error: A publication already exists with the same canonical field value');
if ($this->confirm('Do you wish to overwrite the existing file?')) {
$creator->force();
} else {
$this->output->writeln('<bg=magenta;fg=white>Exiting without overwriting existing publication file!</>');
$this->info('Exiting without overwriting existing publication file!');

return ValidatingCommand::USER_EXIT;
}
}

$creator->create();

$this->info('Publication created successfully!');
$this->info("Created file {$creator->getOutputPath()}");

return Command::SUCCESS;
}

protected function captureFieldInput(PublicationField $field, PublicationType $pubType): string|array
protected function getPublicationTypeSelection(): PublicationType
{
if ($field->type === PublicationFieldTypes::Text) {
return $this->captureTextFieldInput($field);
}

if ($field->type === PublicationFieldTypes::Array) {
return $this->captureArrayFieldInput($field);
}

if ($field->type === PublicationFieldTypes::Image) {
return $this->captureImageFieldInput($field, $pubType);
}

if ($field->type === PublicationFieldTypes::Tag) {
return $this->captureTagFieldInput($field);
}

$fieldRules = $this->generateFieldRules($field);

return $this->askWithValidation($field->name, $field->name, $fieldRules->toArray());
}
$publicationTypes = $this->getPublicationTypes();

/**
* @param \Illuminate\Support\Collection<string, \Hyde\Framework\Features\Publications\Models\PublicationType> $pubTypes
* @return \Hyde\Framework\Features\Publications\Models\PublicationType
*/
protected function getPubTypeSelection(Collection $pubTypes): PublicationType
{
$pubTypeSelection = $this->argument('publicationType') ?? $pubTypes->keys()->get(
(int) $this->choice('Which publication type would you like to create a publication item for?',
$pubTypes->keys()->toArray()
$publicationTypeSelection = $this->argument('publicationType') ?? $publicationTypes->keys()->get(
(int) $this->choice(
'Which publication type would you like to create a publication item for?',
$publicationTypes->keys()->toArray()
)
);

if ($pubTypes->has($pubTypeSelection)) {
$this->line("<info>Creating a new publication of type</info> [<comment>$pubTypeSelection</comment>]");
if ($publicationTypes->has($publicationTypeSelection)) {
$this->line("<info>Creating a new publication of type</info> [<comment>$publicationTypeSelection</comment>]");

return $pubTypes->get($pubTypeSelection);
return $publicationTypes->get($publicationTypeSelection);
}

throw new InvalidArgumentException("Unable to locate publication type [$pubTypeSelection]");
throw new InvalidArgumentException("Unable to locate publication type [$publicationTypeSelection]");
}

/**
* @param \Hyde\Framework\Features\Publications\Models\PublicationType $pubType
* @return \Illuminate\Support\Collection<string, string|array>
*/
protected function collectFieldData(PublicationType $pubType): Collection
/** @return \Illuminate\Support\Collection<string, PublicationType> */
protected function getPublicationTypes(): Collection
{
$this->output->writeln("\n<bg=magenta;fg=white>Now please enter the field data:</>");
$publicationTypes = PublicationService::getPublicationTypes();
if ($publicationTypes->isEmpty()) {
throw new InvalidArgumentException('Unable to locate any publication types. Did you create any?');
}

return Collection::make($pubType->fields)->mapWithKeys(function ($field) use ($pubType) {
return [$field['name'] => $this->captureFieldInput(PublicationField::fromArray($field), $pubType)];
});
return $publicationTypes;
}

/**
* @return \Illuminate\Support\Collection<string, PublicationType>
*
* @throws \InvalidArgumentException
*/
protected function getPublicationTypes(): Collection
/** @return \Illuminate\Support\Collection<string, string|array|null> */
protected function collectFieldData(): Collection
{
$pubTypes = PublicationService::getPublicationTypes();
if ($pubTypes->isEmpty()) {
throw new InvalidArgumentException('Unable to locate any publication types. Did you create any?');
$this->newLine();
$this->info('Now please enter the field data:');
$data = new Collection();

/** @var PublicationField $field */
foreach ($this->publicationType->getFields() as $field) {
$this->newLine();
$data->put($field->name, $this->captureFieldInput($field));
}

return $pubTypes;
return $data;
}

protected function hasForceOption(): bool
protected function captureFieldInput(PublicationField $field): string|array|null
{
return (bool) $this->option('force');
return match ($field->type) {
PublicationFieldTypes::Text => $this->captureTextFieldInput($field),
PublicationFieldTypes::Array => $this->captureArrayFieldInput($field),
PublicationFieldTypes::Image => $this->captureImageFieldInput($field),
PublicationFieldTypes::Tag => $this->captureTagFieldInput($field),
default => $this->askWithValidation($field->name, $field->name, $field->type->rules()),
};
}

protected function captureTextFieldInput(PublicationField $field): string
{
$this->output->writeln($field->name.' (end with an empty line)');
$this->line(InputStreamHandler::formatMessage($field->name));

return implode("\n", InputStreamHandler::call());
}

protected function captureArrayFieldInput(PublicationField $field): array
{
$this->output->writeln($field->name.' (end with an empty line)');
$this->line(InputStreamHandler::formatMessage($field->name));

return InputStreamHandler::call();
}

protected function captureImageFieldInput(PublicationField $field, PublicationType $pubType): string
protected function captureImageFieldInput(PublicationField $field): string|null
{
$this->output->writeln($field->name.' (end with an empty line)');
do {
$offset = 0;
$mediaFiles = PublicationService::getMediaForPubType($pubType);
foreach ($mediaFiles as $index => $file) {
$offset = $index + 1;
$this->output->writeln(" $offset: $file");
}
$selected = (int) $this->askWithValidation($field->name, $field->name, ['required', 'integer', "between:1,$offset"]);
} while ($selected == 0);
$file = $mediaFiles->{$selected - 1};
$this->infoComment('Select file for image field', $field->name);

return '_media/'.Str::of($file)->after('media/')->toString();
$mediaFiles = PublicationService::getMediaForPubType($this->publicationType);
if ($mediaFiles->isEmpty()) {
return $this->handleEmptyOptionsCollection('media file', "No media files found in directory _media/{$this->publicationType->getIdentifier()}/");
}

$filesArray = $mediaFiles->toArray();
$selection = (int) $this->choice('Which file would you like to use?', $filesArray);

return $filesArray[$selection];
}

protected function captureTagFieldInput(PublicationField $field)
protected function captureTagFieldInput(PublicationField $field): array|string|null
{
$this->output->writeln($field->name.' (enter 0 to reload tag definitions)');
do {
$offset = 0;
$tagsForGroup = PublicationService::getAllTags()->{$this->pubType->name};
foreach ($tagsForGroup as $index => $value) {
$offset = $index + 1;
$this->output->writeln(" $offset: $value");
}
$selected = (int) $this->askWithValidation($field->name, $field->name, ['required', 'integer', "between:0,$offset"]);
} while ($selected == 0);
$this->infoComment('Select a tag for field', $field->name, "from the {$this->publicationType->getIdentifier()} group");

$options = PublicationService::getValuesForTagName($this->publicationType->getIdentifier());
if ($options->isEmpty()) {
return $this->handleEmptyOptionsCollection('tag', 'No tags for this publication type found in tags.json');
}

$this->tip('You can enter multiple tags separated by commas');

return $tagsForGroup->{$selected - 1};
$reloadMessage = '<fg=bright-blue>[Reload tags.json]</>';
do {
$options = PublicationService::getValuesForTagName($this->publicationType->getIdentifier());
$selection = $this->choice(
'Which tag would you like to use?',
array_merge([$reloadMessage], $options->toArray()),
multiple: true
);
} while (in_array($reloadMessage, (array) $selection));

return $selection;
}

// Get rules for fields which are not of type array, text or image
protected function generateFieldRules(PublicationField $field): Collection
/** @return null */
protected function handleEmptyOptionsCollection(string $type, string $message)
{
$fieldRules = Collection::make($field->type->rules());
if ($fieldRules->contains('between')) {
$fieldRules->forget($fieldRules->search('between'));
$this->newLine();
$this->warn("Warning: $message");
// TODO we might want to check if the field has a required rule which should jump straight to the exception
if ($this->confirm('Would you like to skip this field?', true)) {
return null;
} else {
throw new InvalidArgumentException("Unable to locate any {$type}s for this publication type");
}
}

return $fieldRules;
protected function tip(string $message): void
{
$this->line("<fg=bright-blue>Tip:</> $message");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
use function array_merge;
use Hyde\Framework\Actions\Concerns\CreateAction;
use Hyde\Framework\Actions\Contracts\CreateActionContract;
use Hyde\Framework\Features\Publications\Models\PublicationField;
use Hyde\Framework\Features\Publications\Models\PublicationType;
use Hyde\Framework\Features\Publications\PublicationFieldTypes;
use Illuminate\Console\OutputStyle;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use function rtrim;
use RuntimeException;
use function substr;
use Symfony\Component\Yaml\Yaml;
use function trim;

Expand All @@ -30,68 +29,56 @@ public function __construct(
protected PublicationType $pubType,
protected Collection $fieldData,
protected bool $force = false,
protected ?OutputStyle $output = null,
) {
$canonicalFieldName = $this->pubType->canonicalField;
$canonicalFieldDefinition = $this->pubType->getCanonicalFieldDefinition();
$canonicalValue = $this->getCanonicalValue($canonicalFieldDefinition, $canonicalFieldName);
$canonicalStr = Str::of($canonicalValue)->substr(0, 64);

$fileName = $this->formatStringForStorage($canonicalStr->slug()->toString());
$directory = $this->pubType->getDirectory();
$this->outputPath = "$directory/$fileName.md";
$this->outputPath = "{$this->pubType->getDirectory()}/{$this->getFilename()}.md";
}

protected function handleCreate(): void
{
$now = Carbon::now()->format('Y-m-d H:i:s');
$output = <<<MARKDOWN
---
{$this->createFrontMatter($now)}
---

## Write something awesome.


MARKDOWN;

$this->output?->writeln("Saving publication data to [$this->outputPath]");
$output = "---
{$this->createFrontMatter()}
---

## Write something awesome.

";

$this->save($output);
}

protected function getCanonicalValue(PublicationField $canonicalFieldDefinition, string $canonicalFieldName): string
protected function getFilename(): string
{
return $this->formatStringForStorage(substr($this->getCanonicalValue(), 0, 64));
}

protected function getCanonicalValue(): string
{
if ($canonicalFieldName === '__createdAt') {
if ($this->pubType->canonicalField === '__createdAt') {
return Carbon::now()->format('Y-m-d H:i:s');
}

// TODO: Is it reasonable to use arrays as canonical field values?
if ($canonicalFieldDefinition->type === PublicationFieldTypes::Array) {
$canonicalValue = ($this->fieldData->get($canonicalFieldName) ?? [])[0];
} else {
$canonicalValue = $this->fieldData->get($canonicalFieldName);
}
return (string) $this->fieldData->get($this->pubType->canonicalField)
?: throw new RuntimeException("Could not find field value for '{$this->pubType->canonicalField}' which is required for as it's the type's canonical field", 404);
}

return $canonicalValue ?? throw new RuntimeException("Could not find field value for '$canonicalFieldName' which is required for as it's the type's canonical field", 404);
protected function createFrontMatter(): string
{
return rtrim(Yaml::dump($this->getMergedData(), flags: YAML::DUMP_MULTI_LINE_LITERAL_BLOCK));
}

protected function createFrontMatter(string $now): string
protected function getMergedData(): array
{
return rtrim(Yaml::dump($this->normalizeData(array_merge(
['__createdAt' => $now], $this->fieldData->toArray())),
flags: YAML::DUMP_MULTI_LINE_LITERAL_BLOCK
));
return array_merge(['__createdAt' => Carbon::now()],
$this->normalizeData($this->fieldData->toArray())
);
}

protected function normalizeData(array $array): array
{
foreach ($array as $key => $value) {
$type = $this->pubType->getFields()->get($key);

if ($key === '__createdAt') {
$array[$key] = Carbon::parse($value);
} elseif ($type?->type === PublicationFieldTypes::Text) {
if ($type->type === PublicationFieldTypes::Text) {
// In order to properly store text fields as block literals,
// we need to make sure they end with a newline.
$array[$key] = trim($value)."\n";
Expand Down
Loading