Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
131 / 131 |
|
100.00% |
17 / 17 |
CRAP | |
100.00% |
1 / 1 |
| HtmlFileFaker | |
100.00% |
131 / 131 |
|
100.00% |
17 / 17 |
37 | |
100.00% |
1 / 1 |
| createOriginalFilename | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createMimeType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isSupported | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createResource | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| e | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createHtml | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
3 | |||
| muted | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| card | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| navbar | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| hero | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
2 | |||
| features | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| pricing | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
4 | |||
| table | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
7 | |||
| testimonials | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| cta | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| faq | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| footer | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Faker\FileFakers; |
| 3 | |
| 4 | use Apie\Faker\Interfaces\ApieFileFaker; |
| 5 | use Apie\Faker\Tabular\RandomRecords; |
| 6 | use DateTimeImmutable; |
| 7 | use Faker\Generator; |
| 8 | use Nyholm\Psr7\Stream; |
| 9 | |
| 10 | /** |
| 11 | * Generates a landing page styled with the tailwindcss play cdn. |
| 12 | * It always has a navbar, hero and footer; other sections are picked randomly in a random order. |
| 13 | */ |
| 14 | final class HtmlFileFaker implements ApieFileFaker |
| 15 | { |
| 16 | private const COLORS = ['indigo', 'emerald', 'rose', 'sky', 'amber', 'violet', 'teal', 'orange', 'fuchsia']; |
| 17 | private const ICONS = ['⚡', '🔒', '🚀', '🎯', '💡', '🧩', '📦', '🌍', '🔧', '📈', '🎨', '🛡️']; |
| 18 | private const SECTIONS = ['features', 'pricing', 'table', 'testimonials', 'cta', 'faq']; |
| 19 | |
| 20 | public function createOriginalFilename(Generator $faker): string |
| 21 | { |
| 22 | return $faker->word() . '.html'; |
| 23 | } |
| 24 | |
| 25 | public function createMimeType(): string |
| 26 | { |
| 27 | return 'text/html'; |
| 28 | } |
| 29 | |
| 30 | public static function isSupported(): bool |
| 31 | { |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | /** @return resource */ |
| 36 | public function createResource(Generator $faker, string $originalFilename, string $mimeType): mixed |
| 37 | { |
| 38 | return Stream::create($this->createHtml($faker))->detach(); |
| 39 | } |
| 40 | |
| 41 | private function e(string $value): string |
| 42 | { |
| 43 | return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5); |
| 44 | } |
| 45 | |
| 46 | private function createHtml(Generator $faker): string |
| 47 | { |
| 48 | $color = $faker->randomElement(self::COLORS); |
| 49 | $dark = $faker->boolean(30); |
| 50 | $company = $faker->company(); |
| 51 | $body = $dark ? 'bg-slate-900 text-slate-100' : 'bg-white text-slate-800'; |
| 52 | $sections = $faker->randomElements(self::SECTIONS, $faker->numberBetween(2, 5)); |
| 53 | |
| 54 | $html = ''; |
| 55 | foreach ($sections as $section) { |
| 56 | $html .= $this->$section($faker, $color, $dark); |
| 57 | } |
| 58 | |
| 59 | return '<!DOCTYPE html>' . "\n" |
| 60 | . '<html lang="en">' . "\n" |
| 61 | . '<head>' . "\n" |
| 62 | . ' <meta charset="utf-8">' . "\n" |
| 63 | . ' <meta name="viewport" content="width=device-width, initial-scale=1">' . "\n" |
| 64 | . ' <title>' . $this->e($company . ' - ' . $faker->catchPhrase()) . '</title>' . "\n" |
| 65 | . ' <script src="https://cdn.tailwindcss.com"></script>' . "\n" |
| 66 | . '</head>' . "\n" |
| 67 | . '<body class="' . $body . ' antialiased">' . "\n" |
| 68 | . $this->navbar($faker, $company, $color, $dark) |
| 69 | . '<main>' . "\n" |
| 70 | . $this->hero($faker, $color, $dark) |
| 71 | . $html |
| 72 | . '</main>' . "\n" |
| 73 | . $this->footer($faker, $company, $dark) |
| 74 | . '</body>' . "\n" |
| 75 | . '</html>' . "\n"; |
| 76 | } |
| 77 | |
| 78 | private function muted(bool $dark): string |
| 79 | { |
| 80 | return $dark ? 'text-slate-400' : 'text-slate-600'; |
| 81 | } |
| 82 | |
| 83 | private function card(bool $dark): string |
| 84 | { |
| 85 | return $dark ? 'bg-slate-800 border-slate-700' : 'bg-white border-slate-200'; |
| 86 | } |
| 87 | |
| 88 | private function navbar(Generator $faker, string $company, string $color, bool $dark): string |
| 89 | { |
| 90 | $links = ''; |
| 91 | foreach ($faker->words($faker->numberBetween(3, 5)) as $word) { |
| 92 | $links .= '<a href="#" class="hover:text-' . $color . '-500">' . $this->e(ucfirst($word)) . '</a>'; |
| 93 | } |
| 94 | $border = $dark ? 'border-slate-700' : 'border-slate-200'; |
| 95 | |
| 96 | return '<header class="border-b ' . $border . '">' |
| 97 | . '<nav class="mx-auto flex max-w-6xl items-center justify-between px-6 py-4">' |
| 98 | . '<a href="#" class="text-xl font-bold text-' . $color . '-500">' . $this->e($company) . '</a>' |
| 99 | . '<div class="hidden gap-6 text-sm font-medium md:flex">' . $links . '</div>' |
| 100 | . '<a href="#" class="rounded-lg bg-' . $color . '-600 px-4 py-2 text-sm font-semibold text-white hover:bg-' . $color . '-500">' |
| 101 | . $this->e(ucfirst($faker->word())) . '</a>' |
| 102 | . '</nav></header>' . "\n"; |
| 103 | } |
| 104 | |
| 105 | private function hero(Generator $faker, string $color, bool $dark): string |
| 106 | { |
| 107 | $title = $this->e(rtrim($faker->sentence($faker->numberBetween(3, 7)), '.')); |
| 108 | $text = $this->e($faker->paragraph()); |
| 109 | $button = '<a href="#" class="rounded-lg bg-' . $color . '-600 px-6 py-3 font-semibold text-white shadow hover:bg-' . $color . '-500">' |
| 110 | . $this->e(ucfirst($faker->word())) . ' now</a>' |
| 111 | . '<a href="#" class="rounded-lg border px-6 py-3 font-semibold ' . $this->card($dark) . '">Learn more</a>'; |
| 112 | $gradient = 'bg-gradient-to-br from-' . $color . '-400 to-' . $faker->randomElement(self::COLORS) . '-600'; |
| 113 | |
| 114 | if ($faker->boolean()) { |
| 115 | return '<section class="mx-auto max-w-4xl px-6 py-24 text-center">' |
| 116 | . '<h1 class="text-4xl font-extrabold tracking-tight sm:text-6xl">' . $title . '</h1>' |
| 117 | . '<p class="mt-6 text-lg ' . $this->muted($dark) . '">' . $text . '</p>' |
| 118 | . '<div class="mt-10 flex justify-center gap-4">' . $button . '</div>' |
| 119 | . '</section>' . "\n"; |
| 120 | } |
| 121 | |
| 122 | return '<section class="mx-auto grid max-w-6xl items-center gap-12 px-6 py-20 md:grid-cols-2">' |
| 123 | . '<div><h1 class="text-4xl font-extrabold tracking-tight sm:text-5xl">' . $title . '</h1>' |
| 124 | . '<p class="mt-6 text-lg ' . $this->muted($dark) . '">' . $text . '</p>' |
| 125 | . '<div class="mt-8 flex gap-4">' . $button . '</div></div>' |
| 126 | . '<div class="flex h-72 items-center justify-center rounded-3xl text-7xl shadow-xl ' . $gradient . '">' |
| 127 | . $faker->randomElement(self::ICONS) . '</div>' |
| 128 | . '</section>' . "\n"; |
| 129 | } |
| 130 | |
| 131 | private function features(Generator $faker, string $color, bool $dark): string |
| 132 | { |
| 133 | $cards = ''; |
| 134 | foreach ($faker->randomElements(self::ICONS, $faker->randomElement([3, 3, 6])) as $icon) { |
| 135 | $cards .= '<div class="rounded-2xl border p-6 shadow-sm ' . $this->card($dark) . '">' |
| 136 | . '<div class="mb-4 flex h-12 w-12 items-center justify-center rounded-xl bg-' . $color . '-100 text-2xl">' . $icon . '</div>' |
| 137 | . '<h3 class="text-lg font-semibold">' . $this->e(ucfirst($faker->words(2, true))) . '</h3>' |
| 138 | . '<p class="mt-2 text-sm ' . $this->muted($dark) . '">' . $this->e($faker->sentence(12)) . '</p>' |
| 139 | . '</div>'; |
| 140 | } |
| 141 | |
| 142 | return '<section class="mx-auto max-w-6xl px-6 py-16">' |
| 143 | . '<h2 class="mb-10 text-center text-3xl font-bold">' . $this->e(ucfirst($faker->words(3, true))) . '</h2>' |
| 144 | . '<div class="grid gap-6 sm:grid-cols-2 lg:grid-cols-3">' . $cards . '</div></section>' . "\n"; |
| 145 | } |
| 146 | |
| 147 | private function pricing(Generator $faker, string $color, bool $dark): string |
| 148 | { |
| 149 | $cards = ''; |
| 150 | $highlight = $faker->numberBetween(0, 2); |
| 151 | foreach (['Basic', 'Pro', 'Enterprise'] as $index => $plan) { |
| 152 | $border = $index === $highlight ? 'border-2 border-' . $color . '-500 shadow-lg' : 'border'; |
| 153 | $items = ''; |
| 154 | for ($i = 0; $i < $faker->numberBetween(3, 5); $i++) { |
| 155 | $items .= '<li class="flex gap-2"><span class="text-' . $color . '-500">✓</span>' . $this->e(ucfirst($faker->words(3, true))) . '</li>'; |
| 156 | } |
| 157 | $cards .= '<div class="rounded-2xl p-8 ' . $border . ' ' . $this->card($dark) . '">' |
| 158 | . '<h3 class="text-xl font-semibold">' . $plan . '</h3>' |
| 159 | . '<p class="mt-4 text-4xl font-extrabold">$' . $faker->numberBetween(5, 99) * ($index + 1) . '<span class="text-base font-normal ' . $this->muted($dark) . '">/mo</span></p>' |
| 160 | . '<ul class="mt-6 space-y-3 text-sm">' . $items . '</ul>' |
| 161 | . '<a href="#" class="mt-8 block rounded-lg bg-' . $color . '-600 py-2 text-center font-semibold text-white hover:bg-' . $color . '-500">Choose</a>' |
| 162 | . '</div>'; |
| 163 | } |
| 164 | |
| 165 | return '<section class="mx-auto max-w-6xl px-6 py-16"><h2 class="mb-10 text-center text-3xl font-bold">Pricing</h2>' |
| 166 | . '<div class="grid gap-6 md:grid-cols-3">' . $cards . '</div></section>' . "\n"; |
| 167 | } |
| 168 | |
| 169 | private function table(Generator $faker, string $color, bool $dark): string |
| 170 | { |
| 171 | $records = RandomRecords::create($faker, 4, 12); |
| 172 | $head = ''; |
| 173 | foreach ($records->getColumnNames() as $name) { |
| 174 | $head .= '<th class="px-4 py-3 text-left font-semibold">' . $this->e($name) . '</th>'; |
| 175 | } |
| 176 | $rows = ''; |
| 177 | foreach ($records as $index => $record) { |
| 178 | $stripe = $index % 2 === 1 ? ($dark ? 'bg-slate-800/50' : 'bg-slate-50') : ''; |
| 179 | $rows .= '<tr class="' . $stripe . '">'; |
| 180 | foreach ($record as $value) { |
| 181 | $rows .= '<td class="px-4 py-3">' |
| 182 | . $this->e($value instanceof DateTimeImmutable ? $value->format('Y-m-d') : (string) $value) |
| 183 | . '</td>'; |
| 184 | } |
| 185 | $rows .= '</tr>'; |
| 186 | } |
| 187 | |
| 188 | return '<section class="mx-auto max-w-6xl px-6 py-16">' |
| 189 | . '<h2 class="mb-6 text-2xl font-bold">' . $this->e(ucfirst($faker->words(2, true))) . '</h2>' |
| 190 | . '<div class="overflow-x-auto rounded-xl border ' . $this->card($dark) . '">' |
| 191 | . '<table class="min-w-full text-sm"><thead class="bg-' . $color . '-600 text-white"><tr>' . $head . '</tr></thead>' |
| 192 | . '<tbody>' . $rows . '</tbody></table></div></section>' . "\n"; |
| 193 | } |
| 194 | |
| 195 | private function testimonials(Generator $faker, string $color, bool $dark): string |
| 196 | { |
| 197 | $cards = ''; |
| 198 | for ($i = 0; $i < 3; $i++) { |
| 199 | $name = $faker->name(); |
| 200 | $cards .= '<figure class="rounded-2xl border p-6 ' . $this->card($dark) . '">' |
| 201 | . '<blockquote class="italic ' . $this->muted($dark) . '">“' . $this->e($faker->sentence(14)) . '”</blockquote>' |
| 202 | . '<figcaption class="mt-4 flex items-center gap-3">' |
| 203 | . '<span class="flex h-10 w-10 items-center justify-center rounded-full bg-' . $color . '-500 font-bold text-white">' . $this->e(mb_substr($name, 0, 1)) . '</span>' |
| 204 | . '<span><span class="block text-sm font-semibold">' . $this->e($name) . '</span>' |
| 205 | . '<span class="block text-xs ' . $this->muted($dark) . '">' . $this->e($faker->jobTitle()) . '</span></span>' |
| 206 | . '</figcaption></figure>'; |
| 207 | } |
| 208 | |
| 209 | return '<section class="mx-auto max-w-6xl px-6 py-16"><div class="grid gap-6 md:grid-cols-3">' . $cards . '</div></section>' . "\n"; |
| 210 | } |
| 211 | |
| 212 | private function cta(Generator $faker, string $color, bool $dark): string |
| 213 | { |
| 214 | return '<section class="px-6 py-16"><div class="mx-auto max-w-4xl rounded-3xl bg-' . $color . '-600 px-8 py-14 text-center text-white">' |
| 215 | . '<h2 class="text-3xl font-bold">' . $this->e(rtrim($faker->sentence(5), '.')) . '</h2>' |
| 216 | . '<p class="mt-4 opacity-90">' . $this->e($faker->sentence(12)) . '</p>' |
| 217 | . '<a href="#" class="mt-8 inline-block rounded-lg bg-white px-6 py-3 font-semibold text-' . $color . '-700 hover:bg-slate-100">' |
| 218 | . $this->e(ucfirst($faker->word())) . '</a></div></section>' . "\n"; |
| 219 | } |
| 220 | |
| 221 | private function faq(Generator $faker, string $color, bool $dark): string |
| 222 | { |
| 223 | $items = ''; |
| 224 | for ($i = 0; $i < $faker->numberBetween(3, 5); $i++) { |
| 225 | $items .= '<details class="rounded-xl border p-4 ' . $this->card($dark) . '">' |
| 226 | . '<summary class="cursor-pointer font-semibold">' . $this->e(ucfirst($faker->words(4, true))) . '?</summary>' |
| 227 | . '<p class="mt-3 text-sm ' . $this->muted($dark) . '">' . $this->e($faker->paragraph()) . '</p></details>'; |
| 228 | } |
| 229 | |
| 230 | return '<section class="mx-auto max-w-3xl px-6 py-16"><h2 class="mb-8 text-center text-3xl font-bold">FAQ</h2>' |
| 231 | . '<div class="space-y-3">' . $items . '</div></section>' . "\n"; |
| 232 | } |
| 233 | |
| 234 | private function footer(Generator $faker, string $company, bool $dark): string |
| 235 | { |
| 236 | $border = $dark ? 'border-slate-700' : 'border-slate-200'; |
| 237 | |
| 238 | return '<footer class="border-t ' . $border . ' py-8 text-center text-sm ' . $this->muted($dark) . '">' |
| 239 | . '© ' . $faker->year() . ' ' . $this->e($company) . '. ' . $this->e($faker->catchPhrase()) . '.</footer>' . "\n"; |
| 240 | } |
| 241 | } |