Add --json to polls list and polls show
Machine-readable output for scripts; never contains the admin token unless --link is given. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
664088e139
commit
3d6ade668f
6 files changed
+115
-2
No files matched your search
@@ -7,6 +7,7 @@ use App\Model\Entity\Poll;
|
||||
use App\Model\Table\PollsTable;
|
||||
use Cake\Command\Command;
|
||||
use Cake\Console\ConsoleIo;
|
||||
use Cake\Console\ConsoleOutput;
|
||||
use Cake\I18n\DateTime;
|
||||
use Cake\Routing\Router;
|
||||
|
||||
@@ -26,6 +27,43 @@ abstract class PollsCommand extends Command
|
||||
return $polls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Machine-readable form of a poll (for `--json`). Never contains the admin token.
|
||||
*
|
||||
* @param \App\Model\Entity\Poll $poll Poll
|
||||
* @return array<string, mixed> Dates as ISO 8601 (server time zone), null where empty
|
||||
*/
|
||||
protected function pollData(Poll $poll): array
|
||||
{
|
||||
$date = fn(?DateTime $d): ?string => $d?->format(DATE_ATOM);
|
||||
|
||||
return [
|
||||
'id' => $poll->id,
|
||||
'title' => $poll->title,
|
||||
'email' => $poll->email,
|
||||
'joinCode' => $poll->join_code,
|
||||
'joinUrl' => $this->joinLink($poll),
|
||||
'created' => $date($poll->created),
|
||||
'lastAdminAccess' => $date($poll->last_admin_access),
|
||||
'expiresAt' => $date($poll->expiresAt()),
|
||||
'expired' => $poll->isExpired(),
|
||||
'reminderSentAt' => $date($poll->expiry_reminded_at),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints data as JSON only: nothing else on stdout, no markup interpretation (titles may contain "<").
|
||||
*
|
||||
* @param \Cake\Console\ConsoleIo $io Console
|
||||
* @param mixed $data JSON serialisable data
|
||||
* @return void
|
||||
*/
|
||||
protected function printJson(ConsoleIo $io, mixed $data): void
|
||||
{
|
||||
$io->setOutputAs(ConsoleOutput::RAW);
|
||||
$io->out((string)json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a poll by numeric id or join code ("K7P2QX", "k7p-2qx").
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user