77use Tamedevelopers \Support \Str ;
88use Tamedevelopers \Support \Time ;
99
10- class Cookie{
10+ final class Cookie{
1111
1212 /**
1313 * Site name
@@ -57,10 +57,10 @@ protected static function init()
5757
5858 /**
5959 * Create Cookie
60- * @param string $name
60+ * @param mixed $name
6161 * - Cookie Name
6262 *
63- * @param string|null $value
63+ * @param mixed $value
6464 * - Cookie Value
6565 *
6666 * @param int|string $minutes
@@ -99,6 +99,9 @@ public static function set($name, $value = null, $minutes = 0, $path = null, $do
9999 $ path , $ value , $ domain , $ secure , $ httponly , $ force
100100 );
101101
102+ $ name = self ::getItemValue ($ name );
103+ $ value = self ::getItemValue ($ value );
104+
102105 // Prefer new setcookie signature with array options (PHP 7.3+), fallback otherwise
103106 if (!headers_sent () || $ force === true ) {
104107 $ options = [
@@ -113,28 +116,28 @@ public static function set($name, $value = null, $minutes = 0, $path = null, $do
113116 // Try modern signature; if it fails (older PHP), fallback to legacy
114117 try {
115118 if (PHP_VERSION_ID >= 70300 ) {
116- @setcookie ($ name , ( string ) $ value , $ options );
119+ @setcookie ($ name , $ value , $ options );
117120 } else {
118- @setcookie ($ name , ( string ) $ value , (int ) $ expires , (string ) $ path , (string ) $ domain , (bool ) $ secure , (bool ) $ httponly );
121+ @setcookie ($ name , $ value , (int ) $ expires , (string ) $ path , (string ) $ domain , (bool ) $ secure , (bool ) $ httponly );
119122 }
120123 } catch (\Throwable $ e ) {
121- @setcookie ($ name , ( string ) $ value , (int ) $ expires , (string ) $ path , (string ) $ domain , (bool ) $ secure , (bool ) $ httponly );
124+ @setcookie ($ name , $ value , (int ) $ expires , (string ) $ path , (string ) $ domain , (bool ) $ secure , (bool ) $ httponly );
122125 }
123126 }
124127 }
125128
126129 /**
127130 * Expire the given cookie.
128131 *
129- * @param string $name
132+ * @param mixed $name
130133 * @param string|null $path
131134 * @param string|null $domain
132135 * @return void
133136 */
134137 public static function forget ($ name , $ path = null , $ domain = null )
135138 {
136139 self ::set (
137- name: $ name ,
140+ name: self :: getItemValue ( $ name) ,
138141 minutes: 'last year ' ,
139142 path: $ path ,
140143 domain: $ domain ,
@@ -145,7 +148,7 @@ public static function forget($name, $path = null, $domain = null)
145148 /**
146149 * Expire the given cookie.
147150 *
148- * @param string $name
151+ * @param mixed $name
149152 * @param string|null $path
150153 * @param string $domain
151154 * @return void
@@ -198,26 +201,28 @@ public static function getExpire()
198201 /**
199202 * Cookie has name that exists
200203 *
201- * @param string $name
204+ * @param mixed $name
202205 * - Cookie name
203206 *
204207 * @return bool
205208 */
206209 public static function has ($ name = null )
207210 {
208- return isset ($ _COOKIE [( string ) $ name ]);
211+ return isset ($ _COOKIE [self :: getItemValue ( $ name) ]);
209212 }
210213
211214 /**
212215 * Get cookie
213- * @param string $name
216+ * @param mixed $name
214217 * - Cookie name
215218 *
216219 * @return mixed
217220 */
218221 public static function get ($ name = null )
219222 {
220- return self ::has ($ name ) ? $ _COOKIE [(string ) $ name ] : null ;
223+ $ name = self ::getItemValue ($ name );
224+
225+ return self ::has ($ name ) ? $ _COOKIE [$ name ] : null ;
221226 }
222227
223228 /**
@@ -232,6 +237,21 @@ public static function all($name = null)
232237 {
233238 return self ::get ($ name ) ?? $ _COOKIE ;
234239 }
240+
241+ /**
242+ * Item Value
243+ *
244+ * @param mixed $value
245+ * @return string
246+ */
247+ private static function getItemValue ($ value = null )
248+ {
249+ if (is_array ($ value )){
250+ $ value = Str::head ($ value );
251+ }
252+
253+ return (string ) $ value ;
254+ }
235255
236256 /**
237257 * Set minutes
0 commit comments