| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ElePHPant\Cookie\Storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Class CookieStorage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Please report bugs on https://github.com/wilderamorim/cookie/issues | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @author Wilder Amorim <https://github.com/wilderamorim> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @link https://www.linkedin.com/in/wilderamorim/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class CookieStorage implements CookieStorageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** @var array  The cookie options array. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private array $cookieOptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** @var string The expiration time unit. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private string $expiration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** Default options for the cookie. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private const DEFAULT_VALUES = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         'path' => '/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         'domain' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         'secure' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         'httponly' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param array $options The options array for the cookie storage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function __construct(array $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->expiration = $options['expiration']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->cookieOptions = array_intersect_key($options, array_flip(array_keys(self::DEFAULT_VALUES))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->cookieOptions = array_replace(self::DEFAULT_VALUES, $this->cookieOptions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Set a cookie. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param string      $name   The name of the cookie. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param string|null $value  The value of the cookie. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param int         $expire The expiration time of the cookie as a Unix timestamp. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return bool               True if the cookie is set successfully, false otherwise. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function set(string $name, ?string $value, int $expire): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->cookieOptions['expires'] = strtotime("+$expire $this->expiration"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return setcookie($name, $value, $this->cookieOptions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Get the value of a cookie or all cookies. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param string|null $name The name of the cookie. If null, returns an array of all cookies. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @return mixed            The value of the cookie or an array of all cookies. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function get(?string $name = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if (!$name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             return filter_input_array(INPUT_COOKIE, FILTER_DEFAULT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return filter_input(INPUT_COOKIE, $name, FILTER_DEFAULT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * Delete a cookie or all cookies. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param string|null $name The name of the cookie. If null, deletes all cookies. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return bool             True if the cookie(s) is deleted successfully, false otherwise. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     public function delete(?string $name = null): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         $options = array_merge($this->cookieOptions, ['expires' => -1]); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         if (!$name) { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             array_map(fn($name) => setcookie($name, null, $options), array_keys($_COOKIE)); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |             return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         return setcookie($name, null, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |  |