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

michalsn / codeigniter-htmx / 13707691824

06 Mar 2025 08:36PM UTC coverage: 74.783%. First build
13707691824

Pull #83

github

web-flow
Merge 7f79fbae7 into 2ec7f6426
Pull Request #83: chore: update workflow to support PHP 8.4

17 of 22 new or added lines in 6 files covered. (77.27%)

258 of 345 relevant lines covered (74.78%)

7.91 hits per line

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

100.0
/src/HTTP/IncomingRequest.php
1
<?php
2

3
namespace Michalsn\CodeIgniterHtmx\HTTP;
4

5
use CodeIgniter\HTTP\IncomingRequest as BaseIncomingRequest;
6

7
class IncomingRequest extends BaseIncomingRequest
8
{
9
    /**
10
     * Indicates that the request is triggered by Htmx.
11
     */
12
    public function isHtmx(): bool
13
    {
14
        return $this->getHtmxHeaderToBool('HX-Request');
36✔
15
    }
16

17
    /**
18
     * Indicates that the request is via an element using hx-boost.
19
     */
20
    public function isBoosted(): bool
21
    {
22
        return $this->getHtmxHeaderToBool('HX-Boosted');
3✔
23
    }
24

25
    /**
26
     * True if the request is for history restoration
27
     * after a miss in the local history cache.
28
     */
29
    public function isHistoryRestoreRequest(): bool
30
    {
31
        return $this->getHtmxHeaderToBool('HX-History-Restore-Request');
2✔
32
    }
33

34
    /**
35
     * The current URL of the browser.
36
     */
37
    public function getCurrentUrl(): ?string
38
    {
39
        return $this->getHtmxHeader('HX-Current-Url');
2✔
40
    }
41

42
    /**
43
     * The user response to an hx-prompt.
44
     */
45
    public function getPrompt(): ?string
46
    {
47
        return $this->getHtmxHeader('HX-Prompt');
2✔
48
    }
49

50
    /**
51
     * The id of the target element if it exists.
52
     */
53
    public function getTarget(): ?string
54
    {
55
        return $this->getHtmxHeader('HX-Target');
2✔
56
    }
57

58
    /**
59
     * The id of the triggered element if it exists.
60
     */
61
    public function getTrigger(): ?string
62
    {
63
        return $this->getHtmxHeader('HX-Trigger');
2✔
64
    }
65

66
    /**
67
     * The name of the triggered element if it exists.
68
     */
69
    public function getTriggerName(): ?string
70
    {
71
        return $this->getHtmxHeader('HX-Trigger-Name');
2✔
72
    }
73

74
    /**
75
     * The value of the header is a JSON serialized
76
     * version of the event that triggered the request.
77
     *
78
     * @see https://htmx.org/extensions/event-header/
79
     */
80
    public function getTriggeringEvent(bool $toArray = true): array|object|null
81
    {
82
        if (! $this->hasHeader('Triggering-Event')) {
2✔
83
            return null;
1✔
84
        }
85

86
        return json_decode($this->header('Triggering-Event')->getValueLine(), $toArray);
1✔
87
    }
88

89
    /**
90
     * Helper method to get the Htmx header value
91
     */
92
    private function getHtmxHeader(string $header): ?string
93
    {
94
        if (! $this->hasHeader($header)) {
10✔
95
            return null;
5✔
96
        }
97

98
        return $this->header($header)->getValueLine();
5✔
99
    }
100

101
    /**
102
     * Helper method to cast Htmx header to bool
103
     */
104
    private function getHtmxHeaderToBool(string $header): bool
105
    {
106
        return $this->hasHeader($header)
41✔
107
            && $this->header($header)->getValueLine() === 'true';
41✔
108
    }
109

110
    /**
111
     * Checks this request type.
112
     *
113
     * @param         string                                                                                     $type HTTP verb or 'json' or 'ajax' or 'htmx' or 'boosted'
114
     * @phpstan-param string|'get'|'post'|'put'|'delete'|'head'|'patch'|'options'|'json'|'ajax'|'htmx'|'boosted' $type
115
     */
116
    public function is(string $type): bool
117
    {
118
        $valueUpper = strtoupper($type);
3✔
119

120
        if ($valueUpper === 'HTMX') {
3✔
121
            return $this->isHtmx();
1✔
122
        }
123

124
        if ($valueUpper === 'BOOSTED') {
2✔
125
            return $this->isBoosted();
1✔
126
        }
127

128
        return parent::is($type);
1✔
129
    }
130
}
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