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

Trim Package Name of unwanted information #219

Closed
wants to merge 2 commits into from
Closed
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
40 changes: 40 additions & 0 deletions app/Helpers/TrimPackageName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Helpers;

use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Macroable;

class TrimPackageName
{
use Macroable;

/**
* @param string $packageName
* @return string
*/
public static function trim(string $packageName): string
{
// The haystack used to check if the string contains any of the invalid substrings.
$versionHaystack = [];

// Create the version haystack for each version of Laravel Nova.
$v = 1;
while ($v <= config('novapackages.nova.latest_major_version')) {
foreach (config('novapackages.filtering.package_name') as $subject) {
// Replace ! with the version number.
$versionHaystack[] = str_replace('!', $v, $subject);
}

$v++;
}

// Filter the string to only contain strings that contain the version number case insensitive.
$filteredVersions = str_ireplace($versionHaystack, '', $packageName);

// Filter extra spaces created by the removing elements from the version haystack.
$filtered = preg_replace('!\s+!', ' ', $filteredVersions);

return $filtered;
}
}
5 changes: 2 additions & 3 deletions app/Http/Resources/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace App\Http\Resources;

use App\Exceptions\PackagistException;
use App\Http\Remotes\Packagist;
use App\Helpers\TrimPackageName;
use App\Http\Resources\Collaborator;
use App\Http\Resources\Tag;
use Illuminate\Http\Resources\Json\JsonResource;
Expand All @@ -19,7 +18,7 @@ class Package extends JsonResource
public function toArray($request)
{
return [
'name' => $this->name,
'name' => TrimPackageName::trim($this->composer_name),
'author' => new Collaborator($this->author),
'composer_name' => $this->composer_name,
'url' => $this->url,
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Resources/PackageResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Resources;

use App\Favorite;
use App\Helpers\TrimPackageName;
use App\Package;
use Illuminate\Support\Str;

Expand All @@ -16,7 +17,7 @@ public function toArray($package)
{
return [
'id' => $package->id,
'name' => $package->name,
'name' => TrimPackageName::trim($package->name),
'composer_name' => $package->composer_name,
'packagist_namespace' => $package->composer_vendor,
'packagist_name' => $package->composer_package,
Expand Down
21 changes: 21 additions & 0 deletions config/novapackages.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,25 @@
'latest_major_version' => env('NOVA_LATEST_MAJOR_VERSION', '4'),
],

'filtering' => [

/**
* Any item in the haystack that doesn't contain a string needs to be placed at the bottom
* otherwise it will leave the string before the version number.
*/
'package_name' => [
'For Nova',
'For Nova !',
'For Nova!',
'Nova !',
'Nova!',
'N!',
'V!',
'N !',
'V !',
'(!)', // Place at bottom
'!', // Place at bottom
]
]

];
4 changes: 2 additions & 2 deletions resources/views/livewire/partials/package-card.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
@include('livewire.partials.title-icon', [
'color' => $package['accent'],
'size' => 'small',
'title' => str_replace(['Laravel Nova ', 'Nova '], [], $package['name']),
'title' => $package['name'],
])
{{ str_replace(['Laravel Nova ', 'Nova '], [], $package['name']) }}
{{ $package['name'] }}

@if ($package['is_disabled'])
<span class="text-xs uppercase text-gray-400">Disabled</span>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/livewire/partials/title-icon.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
@endphp
<div class="{{ $cssClass }}">
<div class="{{ $cssClass }} rounded-full inline-block text-white text-center leading-none flex flex-row items-center justify-center" style="background-color: {{ $color ?? '#606f7b' }}">
{{ strtoupper(substr(str_replace("Nova ", "", $title), 0, 1)) }}
{{ strtoupper(substr($title, 0, 1)) }}
</div>
</div>