Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: popular names #7

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 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));

Check failure on line 134 in app/Http/ViewModels/Names/NameViewModel.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $string of function str_split expects string, float|int<10, max> given.
}

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