PWA Manifest Generator — PHP source
Assemble a valid Web App Manifest — name, colors, display mode, icon sizes, maskable purpose — with a stable key order and instant JSON you can copy or download.
This is the PHP implementation — the same logic the interactive tool runs, in a shareable, citable form.
<?php
/**
* pwa-manifest — build a fixed-key-order manifest.json from form fields.
* Authored snippet mirroring src/lib/pwa-manifest.ts: same fixed key order
* (name, short_name, description, start_url, display, orientation,
* background_color, theme_color, icons), blank optional fields omitted, icon
* sizes defaulting to [192, 512], hard errors suppressing emission, and the
* short_name >12 warning never blocking. Same logic as this dir's
* typescript.ts and the live Go twin at
* cli/pwa-manifest-generator/pwa-manifest-generator.go.
*/
declare(strict_types=1);
final class PwaManifest
{
/** Same contract as TS HEX = /^#[0-9a-fA-F]{6}$/. */
private const HEX = '/^#[0-9a-fA-F]{6}$/';
/**
* The PHP twin of buildManifest() in src/lib/pwa-manifest.ts. $input
* mirrors the ManifestInput interface; 'icon_sizes' null falls back to
* [192, 512] while an explicit empty array emits an empty icons array —
* the same distinction the TS lib draws with `??`. Returns the
* ManifestResult shape: ['ok' => bool, 'errors' => string[],
* 'warnings' => string[], 'json' => string].
*
* @param array<string, mixed> $input
* @return array{ok: bool, errors: list<string>, warnings: list<string>, json: string}
*/
public static function buildManifest(array $input): array
{
$errors = [];
$warnings = [];
$name = trim((string) ($input['name'] ?? ''));
$shortName = trim((string) ($input['short_name'] ?? ''));
$description = trim((string) ($input['description'] ?? ''));
$startUrl = trim((string) ($input['start_url'] ?? ''));
$backgroundColor = trim((string) ($input['background_color'] ?? ''));
$themeColor = trim((string) ($input['theme_color'] ?? ''));
/** @var list<int> $sizes */
$sizes = $input['icon_sizes'] ?? [192, 512];
if ($name === '') {
$errors[] = 'name is required.';
}
if ($backgroundColor !== '' && preg_match(self::HEX, $backgroundColor) !== 1) {
$errors[] = 'background_color must be a #rrggbb hex value.';
}
if ($themeColor !== '' && preg_match(self::HEX, $themeColor) !== 1) {
$errors[] = 'theme_color must be a #rrggbb hex value.';
}
foreach ($sizes as $s) {
if (!is_int($s) || $s <= 0) {
$errors[] = 'icon sizes must be positive integers.';
break;
}
}
$seen = [];
foreach ($sizes as $s) {
if (in_array($s, $seen, true)) {
$errors[] = "icon size {$s} is listed twice.";
}
$seen[] = $s;
}
if ($errors !== []) {
return ['ok' => false, 'errors' => $errors, 'warnings' => [], 'json' => ''];
}
if (mb_strlen($shortName) > 12) {
$len = mb_strlen($shortName);
$warnings[] = "short_name is {$len} characters — home-screen labels truncate around 12.";
}
$lines = [];
$add = function (string $key, string $value) use (&$lines): void {
$lines[] = ' ' . self::jsonString($key) . ': ' . self::jsonString($value);
};
$add('name', $name);
if ($shortName !== '') {
$add('short_name', $shortName);
}
if ($description !== '') {
$add('description', $description);
}
if ($startUrl !== '') {
$add('start_url', $startUrl);
}
if (($input['display'] ?? '') !== '') {
$add('display', (string) $input['display']);
}
if (($input['orientation'] ?? '') !== '') {
$add('orientation', (string) $input['orientation']);
}
if ($backgroundColor !== '') {
$add('background_color', $backgroundColor);
}
if ($themeColor !== '') {
$add('theme_color', $themeColor);
}
$purpose = !empty($input['maskable']) ? 'maskable' : 'any';
$iconEntries = [];
foreach ($sizes as $s) {
$iconEntries[] = " {\n"
. ' "src": ' . self::jsonString("icons/icon-{$s}x{$s}.png") . ",\n"
. ' "sizes": ' . self::jsonString("{$s}x{$s}") . ",\n"
. " \"type\": \"image/png\",\n"
. ' "purpose": ' . self::jsonString($purpose) . "\n"
. ' }';
}
// JSON.stringify renders an empty array inline.
$lines[] = $iconEntries === []
? ' "icons": []'
: " \"icons\": [\n" . implode(",\n", $iconEntries) . "\n ]";
return ['ok' => true, 'errors' => $errors, 'warnings' => $warnings, 'json' => "{\n" . implode(",\n", $lines) . "\n}\n"];
}
/**
* Double-quote $s exactly like JavaScript's JSON.stringify: escape ",
* \, and the control characters (short escapes for \b \f \n \r \t,
* \u00xx for the rest), leave everything else — including non-ASCII —
* untouched.
*/
private static function jsonString(string $s): string
{
$out = '"';
foreach (mb_str_split($s) as $ch) {
$out .= match ($ch) {
'"' => '\\"',
'\\' => '\\\\',
"\b" => '\\b',
"\f" => '\\f',
"\n" => '\\n',
"\r" => '\\r',
"\t" => '\\t',
default => (strlen($ch) === 1 && ord($ch) < 0x20)
? sprintf('\\u%04x', ord($ch))
: $ch,
};
}
return $out . '"';
}
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →