Skip to content

Commit

Permalink
Add helper for recognising classes
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubKermes committed Jun 7, 2024
1 parent 3cae73c commit f59ec60
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Helpers/RecognizeClassHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Blumilk\BLT\Helpers;

use Illuminate\Support\Str;

class RecognizeClassHelper
{
public static function recognizeObjectClass(string $objectName): string
{
if (strpos($objectName, "\\")) {
return $objectName;
}

$objectName = Str::ucfirst(Str::singular($objectName));

return self::getObjectNamespace($objectName) . $objectName;
}

public static function getObjectNamespace(string $objectName): string
{
$type = Str::plural(self::guessType($objectName));

return "App\\$type\\";
}

public static function guessType(string $objectName): string
{
foreach (TypesEnum::cases() as $objectType) {
$slug = Str::slug($objectName);
$objectTypeName = $objectType->value;

if (Str::contains($slug, $objectTypeName)) {
return Str::ucfirst(Str::singular($objectTypeName));
}
}

return $objectName;
}
}
22 changes: 22 additions & 0 deletions src/Helpers/TypesEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Blumilk\BLT\Helpers;

enum TypesEnum: string
{
case MODEL = "model";
case JOB = "job";
case POLICY = "policy";
case REQUEST = "request";
case RESOURCE = "resource";
case SEEDER = "seeder";
case ENUM = "enum";
case EVENT = "event";
case LISTENER = "listener";
case MIDDLEWARE = "middleware";
case NOTIFICATION = "notification";
case PROVIDER = "provider";
case SERVICE = "service";
}

0 comments on commit f59ec60

Please sign in to comment.