Skip to content

Commit

Permalink
feat: popular names (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Dec 19, 2023
1 parent de3c816 commit 2162669
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 84 deletions.
6 changes: 3 additions & 3 deletions app/Console/Commands/FetchMetaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public function handle(): void
//ProcessKlingonName::dispatch($name);
}

if (is_null($name->unisex)) {
ProcessMixte::dispatch($name);
}
// if (is_null($name->unisex)) {
// ProcessMixte::dispatch($name);
// }
}
}
}
4 changes: 3 additions & 1 deletion app/Helpers/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Helpers;

use Illuminate\Support\Str;

class StringHelper
{
public static function sanitizeNameForURL(string $name): string
Expand All @@ -18,7 +20,7 @@ public static function sanitizeNameForURL(string $name): string

public static function getProperName(string $name): string
{
$formattedName = ucwords(strtolower($name));
$formattedName = Str::ucfirst(Str::lower($name));

$separator = [' ', '-', '+', "'"];
foreach ($separator as $s) {
Expand Down
10 changes: 10 additions & 0 deletions app/Http/Controllers/NameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,20 @@ public function show(Request $request): View
return NameViewModel::relatedNames($requestedName);
});

$popularity = Cache::remember('popularity-' . $requestedName->name, 604800, function () use ($requestedName) {
return NameViewModel::popularity($requestedName);
});

$numerology = Cache::remember('numerology-' . $requestedName->name, 604800, function () use ($requestedName) {
return NameViewModel::numerology($requestedName);
});

return view('names.show', [
'name' => $name,
'popularity' => $popularity,
'relatedNames' => $relatedNames,
'jsonLdSchema' => NameViewModel::jsonLdSchema($requestedName),
'numerology' => $numerology,
]);
}

Expand Down
72 changes: 72 additions & 0 deletions app/Http/ViewModels/Names/NameViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Number;

class NameViewModel
{
Expand All @@ -33,6 +34,36 @@ public static function details(Name $name): array
];
}

public static function popularity(Name $name): array
{
$decades = range(1900, Carbon::now()->year, 10);

$decadesCollection = collect();
foreach ($decades as $decade) {
$popularity = $name->nameStatistics()
->where('year', '>=', $decade)
->where('year', '<', $decade + 9)
->sum('count');

$decadesCollection->push([
'decade' => $decade . 's',
'popularity' => $popularity,
'percentage' => 0,
]);
}

// now we need to add the percentage of popularity for each decade
$total = $decadesCollection->sum('popularity');
$decadesCollection = $decadesCollection->map(function ($decade) use ($total) {
$decade['percentage'] = Number::format(round($decade['popularity'] / $total * 100), locale: 'fr');
return $decade;
});

return [
'decades' => $decadesCollection,
];
}

public static function jsonLdSchema(Name $name): array
{
return [
Expand Down Expand Up @@ -64,4 +95,45 @@ public static function relatedNames(Name $name): Collection
]),
]);
}

public static function numerology(Name $name): int
{
// for each letter in the name, we need to get the corresponding number
// letter A is 1, letter B is 2, etc... until letter I is 9
// then we start over, letter J is 1, letter K is 2, etc... until letter R is 9
// then we start over, letter S is 1, letter T is 2, etc... until letter Z is 8
// then we add all the numbers together and we get the number for the name
// if the number is greater than 9, we add the digits together until we get a number between 1 and 9
// if the number is 11, 22 or 33, we keep it as is

$letters = str_split($name->name);
$numbers = [];
foreach ($letters as $letter) {
if ($letter === '_' || $letter === ' ' || $letter === '-' || $letter === "'") {
continue;
}

$number = match ($letter) {
'A', 'J', 'S' => 1,
'B', 'K', 'T' => 2,
'C', 'L', 'U' => 3,
'D', 'M', 'V' => 4,
'E', 'N', 'W' => 5,
'F', 'O', 'X' => 6,
'G', 'P', 'Y' => 7,
'H', 'Q', 'Z' => 8,
'I', 'R' => 9,
default => 0,
};

$numbers[] = $number;
}

$number = array_sum($numbers);
while ($number > 9) {
$number = array_sum(str_split($number));
}

return $number;
}
}
Binary file modified bun.lockb
Binary file not shown.
Loading

0 comments on commit 2162669

Please sign in to comment.