Skip to content

Commit

Permalink
Refactor to use suggest() prompt.
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperTey committed Mar 25, 2024
1 parent 7d81198 commit 2e311dc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
35 changes: 35 additions & 0 deletions src/Commands/Concerns/CanPromptForDomain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands\Concerns;

use Illuminate\Support\Str;
use Lunarstorm\LaravelDDD\Support\DomainResolver;

use function Laravel\Prompts\suggest;

trait CanPromptForDomain
{
protected function promptForDomainName(): string
{
$choices = collect(DomainResolver::domainChoices())
->mapWithKeys(fn ($name) => [Str::lower($name) => $name]);

// Prompt for the domain
$domainName = suggest(
label: 'What is the domain?',
options: fn ($value) => collect($choices)
->filter(fn ($name) => Str::contains($name, $value, ignoreCase: true))
->toArray(),
placeholder: 'Start typing to search...',
required: true
);

// Normalize the case of the domain name
// if it is an existing domain.
if ($match = $choices->get(Str::lower($domainName))) {
$domainName = $match;
}

return $domainName;
}
}
6 changes: 3 additions & 3 deletions src/Commands/Concerns/ResolvesDomainFromInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

trait ResolvesDomainFromInput
{
use CanPromptForDomain;

protected ?Domain $domain = null;

protected function getOptions()
Expand Down Expand Up @@ -81,9 +83,7 @@ public function handle()

// If the domain is not set, prompt for it
if (! $this->domain) {
$this->domain = new Domain(
$this->anticipate('What is the domain?', DomainResolver::domainChoices())
);
$this->domain = new Domain($this->promptForDomainName());
}

parent::handle();
Expand Down

0 comments on commit 2e311dc

Please sign in to comment.