Skip to content

Commit 725f490

Browse files
committed
feat: implement Alert class and facade for alert management
1 parent 372614d commit 725f490

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

src/Alert.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Native\Laravel;
4+
5+
use Illuminate\Support\Traits\Conditionable;
6+
use Illuminate\Support\Traits\Macroable;
7+
use Native\Laravel\Client\Client;
8+
use Native\Laravel\Facades\Window;
9+
10+
class Alert
11+
{
12+
protected ?string $type;
13+
protected ?string $title;
14+
protected ?string $detail;
15+
protected ?array $buttons;
16+
protected ?int $defaultId;
17+
protected ?int $cancelId;
18+
19+
final public function __construct(protected Client $client)
20+
{
21+
}
22+
23+
public static function new()
24+
{
25+
return new static(new Client);
26+
}
27+
28+
public function type(string $type): self
29+
{
30+
$this->type = $type;
31+
32+
return $this;
33+
}
34+
35+
public function title(string $title): self
36+
{
37+
$this->title = $title;
38+
39+
return $this;
40+
}
41+
42+
public function detail(string $detail): self
43+
{
44+
$this->detail = $detail;
45+
46+
return $this;
47+
}
48+
49+
public function buttons(array $buttons): self
50+
{
51+
$this->buttons = $buttons;
52+
53+
return $this;
54+
}
55+
56+
public function defaultPath(string $defaultPath): self
57+
{
58+
$this->defaultPath = $defaultPath;
59+
60+
return $this;
61+
}
62+
63+
public function button(string $buttonLabel): self
64+
{
65+
$this->buttonLabel = $buttonLabel;
66+
67+
return $this;
68+
}
69+
70+
public function defaultId(int $defaultId): self
71+
{
72+
$this->defaultId = $defaultId;
73+
74+
return $this;
75+
}
76+
77+
public function cancelId(int $cancelId): self
78+
{
79+
$this->cancelId = $cancelId;
80+
81+
return $this;
82+
}
83+
84+
public function show(string $message): int
85+
{
86+
$response = $this->client->post('alert/message', [
87+
'message' => $message,
88+
'type' => $this->type,
89+
'title' => $this->title,
90+
'detail' => $this->detail,
91+
'buttons' => $this->buttons,
92+
'defaultId' => $this->defaultId,
93+
'cancelId' => $this->cancelId
94+
]);
95+
96+
return (int) $response->json('result');
97+
}
98+
99+
public function error(string $title, string $message): bool
100+
{
101+
$response = $this->client->post('alert/error', [
102+
'title' => $title,
103+
'message' => $message,
104+
]);
105+
106+
return (bool) $response->json('result');
107+
}
108+
}

src/Facades/Alert.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Native\Laravel\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* @method static static type(string $type)
9+
* @method static static title(string $title)
10+
* @method static static detail(string $detail)
11+
* @method static static buttons(string[] $buttons)
12+
* @method static static defaultId(int $defaultId)
13+
* @method static static cancelId(int $cancelId)
14+
* @method static int show(string $message)
15+
* @method static bool error(string $title, string $message)
16+
*/
17+
class Alert extends Facade
18+
{
19+
protected static function getFacadeAccessor()
20+
{
21+
return \Native\Laravel\Alert::class;
22+
}
23+
}

0 commit comments

Comments
 (0)