• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

michalsn / codeigniter-htmx-alerts / 14001165861

21 Mar 2025 09:25PM UTC coverage: 75.0%. First build
14001165861

Pull #6

github

web-flow
Merge 8b07ee970 into 8278e45a2
Pull Request #6: feat: add `withTitle()` for custom alert title

1 of 4 new or added lines in 1 file covered. (25.0%)

33 of 44 relevant lines covered (75.0%)

2.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.29
/src/Alerts.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Michalsn\CodeIgniterHtmxAlerts;
6

7
use CodeIgniter\Session\Session;
8
use Michalsn\CodeIgniterHtmxAlerts\Config\Alerts as AlertsConfig;
9

10
class Alerts
11
{
12
    protected array $data = [];
13

14
    /**
15
     * The current custom title for alerts.
16
     * If set to null, the default title will be used.
17
     */
18
    protected ?string $currentTitle = null;
19

20
    /**
21
     * Determines whether the title should be reset after each `set()` call.
22
     * If set to `true`, the title will be cleared after every `set()`.
23
     */
24
    protected bool $resetTitleAfterSet = true;
25

26
    public function __construct(protected AlertsConfig $config, protected Session $session)
27
    {
28
    }
18✔
29

30
    /**
31
     * Set alert type and message,
32
     * optionally display time in milliseconds.
33
     */
34
    public function set(string $type, string $message, ?int $displayTime = null): static
35
    {
36
        if (! isset($this->data[$type])) {
8✔
37
            $this->data[$type] = [];
8✔
38
        }
39

40
        $this->data[$type][] = [
8✔
41
            'message'     => $message,
8✔
42
            'displayTime' => $displayTime ?? $this->config->displayTime,
8✔
43
        ];
8✔
44

45
        return $this;
8✔
46
    }
47

48
    /**
49
     * Get alerts.
50
     */
51
    public function get(?string $type = null): array
52
    {
53
        if ($type === null) {
4✔
54
            return $this->data;
3✔
55
        }
56

57
        return $this->data[$type] ?? [];
1✔
58
    }
59

60
    /**
61
     * Sets a custom title for upcoming alerts.
62
     *
63
     * @param string $title              The custom title for the alert.
64
     * @param bool   $resetTitleAfterSet Determines whether the title should be reset after each `set()` call.
65
     *                                   Defaults to `true`, meaning the title will be reset after `set()`.
66
     */
67
    public function withTitle(string $title, bool $resetTitleAfterSet = true): static
68
    {
NEW
69
        $this->currentTitle       = $title;
×
NEW
70
        $this->resetTitleAfterSet = $resetTitleAfterSet;
×
71

NEW
72
        return $this;
×
73
    }
74

75
    /**
76
     * Clear alerts.
77
     */
78
    public function clear(?string $type = null): static
79
    {
80
        if ($type === null) {
2✔
81
            $this->data = [];
1✔
82
        } else {
83
            unset($this->data[$type]);
1✔
84
        }
85

86
        return $this;
2✔
87
    }
88

89
    /**
90
     * Check if we have any alerts.
91
     */
92
    public function has(): bool
93
    {
94
        return $this->data !== [];
9✔
95
    }
96

97
    /**
98
     * Display alerts.
99
     */
100
    public function display(): string
101
    {
102
        if ($this->has()) {
3✔
103
            return view($this->config->views['display'], [$this->config->key => $this->data]);
2✔
104
        }
105

106
        return '';
1✔
107
    }
108

109
    /**
110
     * Display alerts inline.
111
     */
112
    public function inline(): string
113
    {
114
        if ($this->has()) {
2✔
115
            return view($this->config->views['inline'], [$this->config->key => $this->data]);
1✔
116
        }
117

118
        return '';
1✔
119
    }
120

121
    /**
122
     * Store alerts in the session with the flash data.
123
     */
124
    public function session(): void
125
    {
126
        if ($this->has()) {
2✔
127
            $this->session->setFlashdata($this->config->key, $this->data);
1✔
128
        }
129
    }
130

131
    /**
132
     * Display alerts container.
133
     */
134
    public function container(): string
135
    {
136
        return view($this->config->views['container']);
1✔
137
    }
138
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc