Skip to content

Commit 5992b01

Browse files
authored
feat: implement Alert class and facade for alert management (#523)
* feat: implement Alert class and facade for alert management * refactor: remove unused methods from Alert class
1 parent 6899c2c commit 5992b01

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

src/Alert.php

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 defaultId(int $defaultId): self
57+
{
58+
$this->defaultId = $defaultId;
59+
60+
return $this;
61+
}
62+
63+
public function cancelId(int $cancelId): self
64+
{
65+
$this->cancelId = $cancelId;
66+
67+
return $this;
68+
}
69+
70+
public function show(string $message): int
71+
{
72+
$response = $this->client->post('alert/message', [
73+
'message' => $message,
74+
'type' => $this->type,
75+
'title' => $this->title,
76+
'detail' => $this->detail,
77+
'buttons' => $this->buttons,
78+
'defaultId' => $this->defaultId,
79+
'cancelId' => $this->cancelId
80+
]);
81+
82+
return (int) $response->json('result');
83+
}
84+
85+
public function error(string $title, string $message): bool
86+
{
87+
$response = $this->client->post('alert/error', [
88+
'title' => $title,
89+
'message' => $message,
90+
]);
91+
92+
return (bool) $response->json('result');
93+
}
94+
}

src/Facades/Alert.php

+23
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)