Skip to content

Commit 3f01022

Browse files
committed
add trat HasSended
1 parent c881751 commit 3f01022

File tree

6 files changed

+86
-16
lines changed

6 files changed

+86
-16
lines changed

src/Components/HasSended.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace AmirHossein5\LaravelComponents\Components;
4+
5+
use ReflectionClass;
6+
use ReflectionMethod;
7+
8+
trait HasSended
9+
{
10+
/**
11+
* Pass all received parameters.
12+
*
13+
*/
14+
15+
protected function setParametersToPropertiesIfPassed(array|string $except, ...$params): void
16+
{
17+
$class = $this->getParentClass();
18+
$reflectionMethod = new ReflectionMethod($class, '__construct');
19+
$methodParameters = $reflectionMethod->getParameters();
20+
$methodParameters = $this->deleteExceptParameters($except, $methodParameters);
21+
22+
foreach ($params as $key => $value) {
23+
if ($value !== null) {
24+
$this->{$methodParameters[$key]->name} = $value;
25+
}
26+
}
27+
}
28+
29+
private function deleteExceptParameters(array|string $except, $allParameters): array
30+
{
31+
foreach ($allParameters as $key => $parameter) {
32+
if (is_string($except)) {
33+
if ($parameter->name === $except) {
34+
unset($allParameters[$key]);
35+
}
36+
} else {
37+
foreach ($except as $item) {
38+
if ($parameter->name === $item) {
39+
unset($allParameters[$key]);
40+
}
41+
}
42+
}
43+
}
44+
45+
return array_values($allParameters);
46+
}
47+
48+
private function getParentClass(): string
49+
{
50+
return (new ReflectionClass($this))->getParentClass()->name;
51+
}
52+
}

src/Components/Pagination/Components/GrayCircled.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
class GrayCircled extends Pagination
88
{
9+
public bool $showDisabledButtons = true;
10+
911
public function render()
1012
{
11-
$this->showDisabledButtons = true;
12-
1313
return $this->paginateView(
1414
'pagination.gray-circled.tailwind',
1515
'pagination.gray-circled.simple-tailwind'

src/Components/Pagination/Components/LightCircled.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
class LightCircled extends Pagination
88
{
9+
public bool $showDisabledButtons = true;
10+
911
public function render()
1012
{
11-
$this->showDisabledButtons = true;
12-
1313
return $this->paginateView(
1414
'pagination.light-circled.tailwind',
1515
'pagination.light-circled.simple-tailwind'

src/Components/Pagination/Components/LightUnderlined.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
class LightUnderlined extends Pagination
88
{
9+
public bool $showDisabledButtons = true;
10+
911
public function render()
1012
{
11-
$this->showDisabledButtons = true;
12-
1313
return $this->paginateView(
1414
'pagination.light-underlined.tailwind',
1515
'pagination.light-underlined.simple-tailwind'

src/Components/Pagination/Components/RedPill.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
class RedPill extends Pagination
88
{
9+
public bool $showDisabledButtons = true;
10+
911
public function render()
1012
{
11-
$this->showDisabledButtons = true;
12-
1313
return $this->paginateView(
1414
'pagination.red-pill.tailwind',
1515
'pagination.red-pill.simple-tailwind'

src/Components/Pagination/Pagination.php

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

33
namespace AmirHossein5\LaravelComponents\Components\Pagination;
44

5+
use AmirHossein5\LaravelComponents\Components\HasSended;
56
use AmirHossein5\LaravelComponents\Components\Renderable;
67
use Illuminate\View\Component;
78
use Illuminate\Pagination\LengthAwarePaginator;
@@ -10,20 +11,37 @@
1011

1112
abstract class Pagination extends Component
1213
{
13-
use Pageable, Renderable;
14+
use Pageable, Renderable, HasSended;
1415

1516
public LengthAwarePaginator|Paginator $paginator;
17+
public string $prev = '';
18+
public string $next = '';
19+
public string $prevInResponsive = '';
20+
public string $nextInResponsive = '';
21+
public string $class = '';
22+
public bool $showDisabledButtons = false;
23+
public bool $showPaginatorDetails = true;
1624

1725
public function __construct(
1826
LengthAwarePaginator|Paginator $elems,
19-
public string $prev = '',
20-
public string $next = '',
21-
public string $prevInResponsive = '',
22-
public string $nextInResponsive = '',
23-
public string $class = '',
24-
public bool $showDisabledButtons = false,
25-
public bool $showPaginatorDetails = true,
27+
$prev = null,
28+
$next = null,
29+
$prevInResponsive = null,
30+
$nextInResponsive = null,
31+
$class = null,
32+
$showDisabledButtons = null,
33+
$showPaginatorDetails = null,
2634
) {
35+
$this->setParametersToPropertiesIfPassed(
36+
'elems',
37+
$prev,
38+
$next,
39+
$prevInResponsive,
40+
$nextInResponsive,
41+
$class,
42+
$showDisabledButtons,
43+
$showPaginatorDetails
44+
);
2745
$this->paginator = $elems;
2846
$this->isSimplePaginate() ?: $this->setPageNumbers();
2947
}

0 commit comments

Comments
 (0)