00001 <?php 00002 /*************************************************************************** 00003 * Copyright (C) 2008 by Evgeny V. Kokovikhin * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU Lesser General Public License as * 00007 * published by the Free Software Foundation; either version 3 of the * 00008 * License, or (at your option) any later version. * 00009 * * 00010 ***************************************************************************/ 00011 /*$id$*/ 00012 00016 final class Cookie extends CollectionItem 00017 { 00018 private $name = null; 00019 private $value = null; 00020 private $expire = 0; 00021 private $path = null; 00022 private $domain = null; 00023 private $secure = false; 00024 private $httpOnly = false; 00025 00029 public static function create($name) 00030 { 00031 return new self($name); 00032 } 00033 00034 public function __construct($name) 00035 { 00036 $this->id = $this->name = $name; 00037 } 00038 00039 public function getName() 00040 { 00041 return $this->name; 00042 } 00043 00044 public function setValue($value) 00045 { 00046 $this->value = $value; 00047 00048 return $this; 00049 } 00050 00051 public function getValue() 00052 { 00053 return $this->value; 00054 } 00055 00056 public function setMaxAge($expire) 00057 { 00058 Assert::isInteger($expire); 00059 00060 $this->expire = $expire; 00061 00062 return $this; 00063 } 00064 00065 public function getMaxAge() 00066 { 00067 return $this->expire; 00068 } 00069 00070 public function setPath($path) 00071 { 00072 Assert::isString($path); 00073 00074 $this->path = $path; 00075 00076 return $this; 00077 } 00078 00079 public function getPath() 00080 { 00081 return $this->path; 00082 } 00083 00084 public function setDomain($domain) 00085 { 00086 Assert::isString($domain); 00087 00088 $this->domain = $domain; 00089 00090 return $this; 00091 } 00092 00093 public function getDomain() 00094 { 00095 return $this->domain; 00096 } 00097 00098 public function setSecure($secure = true) 00099 { 00100 $this->secure = (boolean) $secure; 00101 00102 return $this; 00103 } 00104 00105 public function getSecure() 00106 { 00107 return $this->secure; 00108 } 00109 00110 public function setHttpOnly($httpOnly = true) 00111 { 00112 $this->httpOnly = (boolean) $httpOnly; 00113 00114 return $this; 00115 } 00116 00117 public function getHttpOnly() 00118 { 00119 return $this->httpOnly; 00120 } 00121 00122 public function httpSet() 00123 { 00124 if (headers_sent()) 00125 throw new WrongStateException('headers already send'); 00126 00127 return 00128 setcookie( 00129 $this->getName(), 00130 $this->getValue(), 00131 ($this->getMaxAge() === 0) 00132 ? 0 00133 : $this->getMaxAge() + time(), 00134 $this->getPath(), 00135 $this->getDomain(), 00136 $this->getSecure(), 00137 $this->getHttpOnly() 00138 ); 00139 } 00140 } 00141 ?>