Overview
  • Namespace
  • Class
  • Tree

Namespaces

  • Modulework
    • Modules
      • Http
        • Exceptions
        • Utilities
  • PHP

Classes

  • Cookie
  • JsonResponse
  • RedirectResponse
  • Request
  • Response
  1 <?php namespace Modulework\Modules\Http;
  2 /*
  3  * (c) Christian Gärtner <christiangaertner.film@googlemail.com>
  4  * This file is part of the Modulework Framework
  5  * License: View distributed LICENSE file
  6  */
  7 
  8 use DateTime;
  9 use InvalidArgumentException;
 10 
 11 /**
 12 * Cookie
 13 * Cookie is an OOP way of handling a cookie
 14 */
 15 class Cookie {
 16 
 17     /**
 18      * The name of the cookie
 19      * @var string
 20      */
 21     protected $name;
 22 
 23     /**
 24      * The value of the cookie
 25      * @var mixed
 26      */
 27     protected $value;
 28 
 29     /**
 30      * The domain attribute of the cookie
 31      * @var string|null
 32      */
 33     protected $domain;
 34 
 35     /**
 36      * The expire date of the cookie
 37      * @var int
 38      */
 39     protected $expire;
 40 
 41     /**
 42      * The path of where the cookie should be avaible
 43      * @var string
 44      */
 45     protected $path;
 46 
 47     /**
 48      * Whether the cookie is SSL only
 49      * @var bool
 50      */
 51     protected $secure;
 52 
 53     /**
 54      * Whether the cookie is HTTP only
 55      * @var bool
 56      */
 57     protected $httpOnly;
 58 
 59 
 60     /**
 61      * Factory for a cookie
 62      * @param  string  $name     The name of the cookie
 63      * @param  mixed   $value    The value of the cookie
 64      * @param  integer $expire   The expire date of the cookie (DateTime object possible as well) UNIX timestamp
 65      * @param  string  $path     The path of where the cookie should be avaible
 66      * @param  string  $domain   The domain attribute of the cookie
 67      * @param  boolean $secure   Whether the cookie is SSL only
 68      * @param  boolean $httpOnly Whether the cookie is HTTP only
 69      *
 70      * @return \Modulework\Modules\Http\Cookie   A new Cookie instance
 71      *
 72      * @throws \InvalidArgumentException (from Constructor)
 73      */
 74     public static function make($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true)
 75     {
 76         return new static($name, $value, $expire, $path, $domain, $secure, $httpOnly);
 77     }
 78 
 79     /**
 80      * Constructor.
 81      * @param  string  $name     The name of the cookie
 82      * @param  mixed   $value    The value of the cookie
 83      * @param  integer $expire   The expire date of the cookie (DateTime object possible as well) UNIX timestamp
 84      * @param  string  $path     The path of where the cookie should be avaible
 85      * @param  string  $domain   The domain attribute of the cookie
 86      * @param  boolean $secure   Whether the cookie is SSL only
 87      * @param  boolean $httpOnly Whether the cookie is HTTP only
 88      *
 89      * @throws \InvalidArgumentException
 90      */
 91     public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true)
 92     {
 93         if ($name == '') throw new InvalidArgumentException('Cookie name cannot be empty');
 94 
 95         if ($expire instanceof DateTime) {
 96             $expire = $expire->format('U');
 97         } elseif (!is_numeric($expire)) {
 98             $expire = strtotime($expire);   
 99         }
100 
101         $this->name         = $name;
102         $this->value        = $value;
103         $this->domain       = $domain;
104         $this->expire       = $expire;
105         $this->path         = ($path == '') ? '/' : $path;
106         $this->secure       = (Boolean) $secure;
107         $this->httpOnly     = (Boolean) $httpOnly;
108     }
109 
110     /**
111      * Set the cookie' s name
112      * @param string $name The name
113      *
114      * @return \Modulework\Modules\Http\Cookie  THIS
115      */
116     public function setName($name)
117     {
118         $this->name = $name;
119         return $this;
120     }
121 
122     /**
123      * Returns the name of the cookie
124      * @return string The name
125      */
126     public function getName()
127     {
128         return $this->name;
129     }
130 
131     /**
132      * Set the cookie' s value
133      * @param mixed $value The value
134      *
135      * @return \Modulework\Modules\Http\Cookie  THIS
136      */
137     public function setValue($value)
138     {
139         $this->value = $value;
140         return $this;
141     }
142 
143     /**
144      * Returns the value of the cookie
145      * @return mixed The value
146      */
147     public function getValue()
148     {
149         return $this->value;
150     }
151 
152     /**
153      * Set the cookie' s expire time
154      * @param string $expire The expire time
155      *
156      * @return \Modulework\Modules\Http\Cookie  THIS
157      */
158     public function setExpiresTime($expire)
159     {
160         $this->expire = $expire;
161         return $this;
162     }
163 
164     /**
165      * Returns the expire timestamp (UNIX)
166      * @return int Unix Timestamp
167      */
168     public function getExpiresTime()
169     {
170         return $this->expire;
171     }
172 
173     /**
174      * Set the cookie' s path
175      * @param string $path The path
176      *
177      * @return \Modulework\Modules\Http\Cookie  THIS
178      */
179     public function setPath($path)
180     {
181         $this->path = $path;
182         return $this;
183     }
184 
185     /**
186      * Returns the path of the cookie
187      * @return string The path
188      */
189     public function getPath()
190     {
191         return $this->path;
192     }
193 
194     /**
195      * Set the cookie' s domain
196      * @param string $domain The domain
197      *
198      * @return \Modulework\Modules\Http\Cookie  THIS
199      */
200     public function setDomain($domain)
201     {
202         $this->domain = $domain;
203         return $this;
204     }
205 
206     /**
207      * Returns the domain of the cookie
208      * @return string The domain
209      */
210     public function getDomain()
211     {
212         return $this->domain;
213     }
214 
215     /**
216      * Set the cookie secure only
217      * @param bool $secure Whether the cookie is SSL only
218      *
219      * @return \Modulework\Modules\Http\Cookie  THIS
220      */
221     public function setSecure($secure)
222     {
223         $this->secure = $secure;
224         return $this;
225     }
226 
227     /**
228      * Checks whether the cookie is SSL only
229      * @return boolean Whether the cookie is "secure"
230      */
231     public function isSecure()
232     {
233         return $this->secure;
234     }
235 
236     /**
237      * Set the cookie HTTP only
238      * @param bool $httpOnly Whether the cookie is HTTP only
239      */
240     public function setHttpOnly($httpOnly)
241     {
242         $this->httpOnly = $httpOnly;
243         return $this;
244     }
245 
246     /**
247      * Checks whether the cookie is HTTP only
248      * @return boolean Whether the cookie is HTTP only
249      */
250     public function isHttpOnly()
251     {
252         return $this->httpOnly;
253     }
254 
255     /**
256      * Checks if the cookie still exists on the client side
257      * (Only checks timestamp, not whether the client deleted the cookie)
258      * @return bool Whether the cookie "could" exists on the client side
259      */
260     public function stillExists()
261     {
262         return ($this->expire > time());
263     }
264 }
API documentation generated by ApiGen 2.8.0