Skip to content

Commit 48c3db7

Browse files
committed
Added Str macros to use the actions and filters class names as titles and id
1 parent 2f9c2df commit 48c3db7

File tree

9 files changed

+45
-32
lines changed

9 files changed

+45
-32
lines changed

src/Actions/Action.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace LaravelViews\Actions;
44

55
use LaravelViews\Views\View;
6+
use Illuminate\Support\Str;
67

78
abstract class Action
89
{
@@ -35,12 +36,7 @@ public function isRedirect()
3536

3637
public function getId()
3738
{
38-
return $this->camelToDashCase((new \ReflectionClass($this))->getShortName());
39-
}
40-
41-
private function camelToDashCase($camelStr)
42-
{
43-
return strtolower(preg_replace('%([a-z])([A-Z])%', '\1-\2', $camelStr));
39+
return Str::camelToDash((new \ReflectionClass($this))->getShortName());
4440
}
4541

4642
public function renderIf($item, View $view)

src/Actions/Confirmable.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
trait Confirmable
66
{
7-
public function getConfirmationMessage($item = null)
7+
/**
8+
* Model instance who fired the action, it is null if it
9+
* was a bulk action
10+
*/
11+
public function getConfirmationMessage($model = null)
812
{
913
return __('Do you really want to perform this action?');
1014
}

src/Actions/WithActions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function getBulkActionsProperty()
8686
private function confirmAction($action, $modelId = null)
8787
{
8888
$actionData = [
89-
'message' => $action->getConfirmationMessage(),
89+
'message' => $action->getConfirmationMessage($modelId ? $this->getModelWhoFiredAction($modelId) : null),
9090
'id' => $action->getId()
9191
];
9292

src/Filters/BaseFilter.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace LaravelViews\Filters;
44

5-
use LaravelViews\Views\View;
6-
use Illuminate\Http\Request;
5+
use Illuminate\Support\Str;
76

87
class BaseFilter
98
{
@@ -52,28 +51,15 @@ public function value()
5251
public function getTitle()
5352
{
5453
if (!$this->title) {
55-
return $this->camelToTitle((new \ReflectionClass($this))->getShortName());
54+
return Str::classNameAsSentence((new \ReflectionClass($this))->getShortName());
5655
}
5756

5857
return $this->title;
5958
}
6059

6160
public function getId()
6261
{
63-
return $this->camelToDashCase((new \ReflectionClass($this))->getShortName());
64-
}
65-
66-
private function camelToTitle($camelStr)
67-
{
68-
$intermediate = preg_replace('/(?!^)([[:upper:]][[:lower:]]+)/', ' $0', $camelStr);
69-
$titleStr = preg_replace('/(?!^)([[:lower:]])([[:upper:]])/', '$1 $2', $intermediate);
70-
71-
return $titleStr;
72-
}
73-
74-
private function camelToDashCase($camelStr)
75-
{
76-
return strtolower(preg_replace('%([a-z])([A-Z])%', '\1-\2', $camelStr));
62+
return Str::camelToDash((new \ReflectionClass($this))->getShortName());
7763
}
7864

7965
public function passValuesFromRequestToFilter($values)

src/LaravelViewsServiceProvider.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use LaravelViews\UI\Header;
1515
use Illuminate\Support\ServiceProvider;
1616
use Illuminate\Support\Facades\Blade;
17+
use Illuminate\Support\Str;
1718
use LaravelViews\Console\GridViewMakeCommand;
1819
use LaravelViews\Console\ListViewMakeCommand;
1920
use LaravelViews\Console\MakeViewCommand;
@@ -70,7 +71,8 @@ public function boot()
7071
->publish()
7172
->bladeDirectives()
7273
->loadComponents()
73-
->configFiles();
74+
->configFiles()
75+
->macros();
7476
}
7577

7678
private function publish()
@@ -159,4 +161,20 @@ private function configFiles()
159161

160162
return $this;
161163
}
164+
165+
private function macros()
166+
{
167+
Str::macro('classNameAsSentence', function ($className) {
168+
$intermediate = preg_replace('/(?!^)([[:upper:]][[:lower:]]+)/', ' $0', $className);
169+
$titleStr = preg_replace('/(?!^)([[:lower:]])([[:upper:]])/', '$1 $2', $intermediate);
170+
171+
return $titleStr;
172+
});
173+
174+
Str::macro('camelToDash', function ($str) {
175+
return strtolower(preg_replace('%([a-z])([A-Z])%', '\1-\2', $str));
176+
});
177+
178+
return $this;
179+
}
162180
}

tests/TestCase.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace LaravelViews\Test;
44

5+
use Illuminate\Translation\TranslationServiceProvider;
6+
use LaravelViews\Facades\UI;
57
use LaravelViews\LaravelViewsServiceProvider;
6-
use Livewire\Livewire;
78
use Livewire\LivewireServiceProvider;
89
use Livewire\Testing\TestableLivewire;
910
use Orchestra\Testbench\TestCase as TestbenchTestCase;
@@ -47,8 +48,16 @@ protected function getPackageProviders($app)
4748
{
4849
return [
4950
// \Spatie\LaravelRay\RayServiceProvider::class,
51+
TranslationServiceProvider::class,
5052
LivewireServiceProvider::class,
5153
LaravelViewsServiceProvider::class,
5254
];
5355
}
56+
57+
protected function getPackageAliases($app)
58+
{
59+
return [
60+
'UI' => UI::class
61+
];
62+
}
5463
}

tests/Unit/ActionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use LaravelViews\Actions\Action;
66
use LaravelViews\Actions\Confirmable;
7-
use PHPUnit\Framework\TestCase as FrameworkTestCase;
7+
use LaravelViews\Test\TestCase;
88

9-
class ActionTest extends FrameworkTestCase
9+
class ActionTest extends TestCase
1010
{
1111
public function testShouldBeConfirmed()
1212
{

tests/Unit/FilterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace LaravelViews\Test\Unit;
44

55
use LaravelViews\Filters\Filter;
6-
use PHPUnit\Framework\TestCase as FrameworkTestCase;
6+
use LaravelViews\Test\TestCase;
77

88
class ExampleTest extends Filter
99
{
@@ -14,7 +14,7 @@ class ExampleTestWithTitle extends Filter
1414
protected $title = 'My custom title';
1515
}
1616

17-
class FilterTest extends FrameworkTestCase
17+
class FilterTest extends TestCase
1818
{
1919
/**
2020
* A basic unit test example.

tests/Unit/VariantsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testButtonVariants()
1717
{
1818
$this->assertTrue(
1919
Variants::button('primary')->class() ===
20-
'text-white bg-blue-600 hover:bg-blue-500 focus:bg-blue-500 active:bg-blue-500'
20+
'text-white bg-blue-600 hover:bg-blue-700 focus:ring-blue-500'
2121
);
2222
$this->assertEquals(
2323
Variants::button('primary-light')->class(),

0 commit comments

Comments
 (0)