1 | <?php |
||
2 | /** |
||
3 | * |
||
4 | */ |
||
5 | |||
6 | namespace Mvc5\Cookie\Config; |
||
7 | |||
8 | use Mvc5\Cookie\Cookies; |
||
9 | |||
10 | use function is_array; |
||
11 | use function is_string; |
||
12 | use function setcookie; |
||
13 | use function setrawcookie; |
||
14 | use function strtotime; |
||
15 | |||
16 | use const Mvc5\{ DOMAIN, EXPIRES, HTTP_ONLY, NAME, OPTIONS, PATH, RAW, SAMESITE, SECURE, VALUE }; |
||
0 ignored issues
–
show
The type
Mvc5\SECURE was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths
Loading history...
The type
Mvc5\VALUE was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths
Loading history...
The type
Mvc5\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths
Loading history...
The type
Mvc5\RAW was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths
Loading history...
The type
Mvc5\EXPIRES was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths
Loading history...
The type
Mvc5\DOMAIN was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths
Loading history...
The type
Mvc5\SAMESITE was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths
Loading history...
The type
Mvc5\HTTP_ONLY was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths
Loading history...
The type
Mvc5\NAME was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths
Loading history...
|
|||
17 | |||
18 | trait PHPCookies |
||
19 | { |
||
20 | /** |
||
21 | * |
||
22 | */ |
||
23 | use HttpCookies; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | protected array $defaults = []; |
||
29 | |||
30 | /** |
||
31 | * @param array|null $cookies |
||
32 | * @param array $defaults |
||
33 | */ |
||
34 | 20 | function __construct(array $cookies = null, array $defaults = []) |
|
35 | { |
||
36 | 20 | $this->config = $cookies ?? $_COOKIE; |
|
37 | 20 | $this->defaults = $defaults; |
|
38 | 20 | } |
|
39 | |||
40 | /** |
||
41 | * @param array|string $cookie |
||
42 | * @param array $options |
||
43 | * @return bool |
||
44 | */ |
||
45 | 42 | static function delete($cookie, array $options = []) : bool |
|
46 | { |
||
47 | 42 | return static::send(expire(cookie(is_string($cookie) ? [NAME => $cookie] + $options : $cookie))); |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param array $cookie |
||
52 | * @param array $defaults |
||
53 | * @return bool |
||
54 | */ |
||
55 | 42 | static function send(array $cookie, array $defaults = []) : bool |
|
56 | { |
||
57 | 42 | return send(cookie($cookie), $defaults); |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param array|string $name |
||
62 | * @param string $value |
||
63 | * @param array $options |
||
64 | * @return mixed |
||
65 | */ |
||
66 | 14 | function set($name, $value = '', array $options = []) |
|
67 | { |
||
68 | 14 | if (is_array($name)) { |
|
69 | 8 | $this->send($name, $this->defaults); |
|
70 | 8 | return $name; |
|
71 | } |
||
72 | |||
73 | 6 | $this->send([NAME => (string) $name, VALUE => (string) $value] + $options, $this->defaults); |
|
74 | |||
75 | 6 | return $value; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param array|string $name |
||
80 | * @param string|null $value |
||
81 | * @param array $options |
||
82 | * @return Cookies|mixed |
||
83 | */ |
||
84 | 2 | function with($name, $value = null, array $options = []) : Cookies |
|
85 | { |
||
86 | 2 | $this->set($name, $value, $options); |
|
87 | 2 | return $this; |
|
0 ignored issues
–
show
|
|||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param array|string $name |
||
92 | * @param array $options |
||
93 | * @return Cookies|mixed |
||
94 | */ |
||
95 | 2 | function without($name, array $options = []) : Cookies |
|
96 | { |
||
97 | 2 | $this->remove($name, $options); |
|
98 | 2 | return $this; |
|
0 ignored issues
–
show
|
|||
99 | } |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param string $name |
||
104 | * @param string $value |
||
105 | * @param array $options |
||
106 | * @param bool $raw |
||
107 | * @return bool |
||
108 | */ |
||
109 | function emit(string $name, string $value, array $options, bool $raw = false) : bool |
||
110 | { |
||
111 | 42 | return $raw ? setrawcookie($name, $value, $options) : setcookie($name, $value, $options); |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param int|string $expires |
||
116 | * @return int |
||
117 | */ |
||
118 | function expires($expires) : int |
||
119 | { |
||
120 | 60 | return (int) (is_string($expires) ? strtotime($expires) : $expires); |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param array $option |
||
125 | * @param array $default |
||
126 | * @return array |
||
127 | */ |
||
128 | function options(array $option, array $default = []) : array |
||
129 | { |
||
130 | return [ |
||
131 | 60 | EXPIRES => (int) expires($option[EXPIRES] ?? $default[EXPIRES] ?? 0), |
|
132 | 60 | PATH => (string) ($option[PATH] ?? $default[PATH] ?? '/'), |
|
133 | 60 | DOMAIN => (string) ($option[DOMAIN] ?? $default[DOMAIN] ?? ''), |
|
134 | 60 | SECURE => (bool) ($option[SECURE] ?? $default[SECURE] ?? false), |
|
135 | 60 | HTTP_ONLY => (bool) ($option[HTTP_ONLY] ?? $default[HTTP_ONLY] ?? true), |
|
136 | 60 | SAMESITE => (string) ($option[SAMESITE] ?? $default[SAMESITE] ?? 'lax') |
|
137 | ]; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param array $cookie |
||
142 | * @param array $defaults |
||
143 | * @return bool |
||
144 | */ |
||
145 | function send(array $cookie, array $defaults = []) : bool |
||
146 | { |
||
147 | 42 | return emit((string) $cookie[NAME], (string) $cookie[VALUE], |
|
148 | 42 | options($cookie[OPTIONS] ?? $cookie, $defaults), $cookie[RAW] ?? false); |
|
149 | } |
||
150 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths