PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/php/VTCompose/Http/Header/SetCookie.php

https://gitlab.com/voj-tech/vtcompose
PHP | 342 lines | 164 code | 50 blank | 128 comment | 19 complexity | 999c2b398eb565ce6f08f89a5b674389 MD5 | raw file
  1. <?php
  2. namespace VTCompose\Http\Header;
  3. use VTCompose\Exception\ArgumentException;
  4. use VTCompose\Text\RegularExpressions\Match;
  5. use VTCompose\Text\RegularExpressions\Regex;
  6. /**
  7. *
  8. *
  9. *
  10. */
  11. final class SetCookie extends Header {
  12. private $name;
  13. private $value;
  14. private $domain;
  15. private $maxAge;
  16. private $path;
  17. private $secure;
  18. private static function getParseFieldValueRegex() {
  19. static $regex = NULL;
  20. if ($regex == NULL) {
  21. $regex = new Regex('[\\t ]*([^;=]*)(?<![\\t ])(?:[\\t ]*=[\\t ]*([^;]*)(?<![\\t ]))?');
  22. }
  23. return $regex;
  24. }
  25. private static function assignNameAndValue(Match $match, &$name, &$value) {
  26. $groups = $match->getGroups();
  27. $name = $groups[1]->getValue();
  28. $value = $groups->count() > 2 ? $groups[2]->getValue() : NULL;
  29. }
  30. private static function parseFieldValue($fieldValue,
  31. &$name, &$value, &$domain, &$maxAge, &$path, &$secure) {
  32. $matches = self::getParseFieldValueRegex()->matches($fieldValue);
  33. if ($matches->count() == 0 || $matches[0]->getGroups()->count() < 3) {
  34. throw new ArgumentException('Field value is not a valid Set-Cookie field value.');
  35. }
  36. self::assignNameAndValue($matches[0], $name, $value);
  37. $domain = $maxAge = $path = NULL;
  38. $secure = false;
  39. for ($i = 1; $i < $matches->count(); $i++) {
  40. self::assignNameAndValue($matches[$i], $attributeName, $attributeValue);
  41. switch ($attributeName) {
  42. case 'Domain':
  43. $domain = $attributeValue;
  44. break;
  45. case 'Max-Age':
  46. $maxAge = $attributeValue;
  47. break;
  48. case 'Path':
  49. $path = $attributeValue;
  50. break;
  51. case 'Secure':
  52. $secure = true;
  53. break;
  54. }
  55. }
  56. }
  57. private static function nameIsValid($name) {
  58. return self::getValidator()->stringIsValidToken($name);
  59. }
  60. private static function valueIsValid($value) {
  61. return self::getValidator()->stringIsValidCookieValue($value);
  62. }
  63. private static function domainIsValid($domain) {
  64. return $domain === NULL || self::getValidator()->stringIsValidSubdomain($domain);
  65. }
  66. private static function maxAgeIsValid($maxAge) {
  67. return $maxAge === NULL || self::getValidator()->valueIsValidPositiveInteger($maxAge);
  68. }
  69. private static function pathIsValid($path) {
  70. return $path === NULL || self::getValidator()->stringIsValidPath($path);
  71. }
  72. private function setAttributes($domain, $maxAge, $path, $secure) {
  73. $this->domain = self::domainIsValid($domain) ? $domain : NULL;
  74. $this->maxAge = self::maxAgeIsValid($maxAge) ? $maxAge : NULL;
  75. $this->path = self::pathIsValid($path) ? $path : NULL;
  76. $this->secure = $secure;
  77. }
  78. /**
  79. *
  80. *
  81. *
  82. *
  83. * @param string
  84. * @return SetCookie
  85. */
  86. public static function createFromFieldValue($fieldValue) {
  87. self::parseFieldValue($fieldValue, $name, $value, $domain, $maxAge, $path, $secure);
  88. $setCookie = new self($name, $value);
  89. $setCookie->setAttributes($domain, $maxAge, $path, $secure);
  90. return $setCookie;
  91. }
  92. /**
  93. *
  94. *
  95. *
  96. *
  97. * @param string
  98. * @param string
  99. * @param string
  100. * @param mixed
  101. * @param string
  102. * @param bool
  103. */
  104. public function __construct($name, $value = '',
  105. $domain = NULL, $maxAge = NULL, $path = NULL, $secure = false) {
  106. parent::__construct(FieldName::SET_COOKIE);
  107. $this->setName($name);
  108. $this->setValue($value);
  109. $this->setDomain($domain);
  110. $this->setMaxAge($maxAge);
  111. $this->setPath($path);
  112. $this->setSecure($secure);
  113. }
  114. /**
  115. *
  116. *
  117. *
  118. *
  119. * @param string
  120. */
  121. public function setFieldValue($fieldValue) {
  122. self::parseFieldValue($fieldValue, $name, $value, $domain, $maxAge, $path, $secure);
  123. $this->setName($name);
  124. $this->setValue($value);
  125. $this->setAttributes($domain, $maxAge, $path, $secure);
  126. }
  127. /**
  128. *
  129. *
  130. *
  131. *
  132. * @return string
  133. */
  134. public function getFieldValue() {
  135. $fieldValue = $this->name . '=' . $this->value;
  136. if ($this->domain !== NULL) {
  137. $fieldValue .= '; Domain=' . $this->domain;
  138. }
  139. if ($this->maxAge !== NULL) {
  140. $fieldValue .= '; Max-Age=' . $this->maxAge;
  141. }
  142. if ($this->path !== NULL) {
  143. $fieldValue .= '; Path=' . $this->path;
  144. }
  145. if ($this->secure) {
  146. $fieldValue .= '; Secure';
  147. }
  148. $fieldValue .= '; Version=1';
  149. return $fieldValue;
  150. }
  151. /**
  152. *
  153. *
  154. *
  155. *
  156. * @param string
  157. * @throws ArgumentException .
  158. */
  159. public function setName($name) {
  160. if (!self::nameIsValid($name)) {
  161. throw new ArgumentException('Name is invalid.');
  162. }
  163. $this->name = $name;
  164. }
  165. /**
  166. *
  167. *
  168. *
  169. *
  170. * @return string
  171. */
  172. public function getName() {
  173. return $this->name;
  174. }
  175. /**
  176. *
  177. *
  178. *
  179. *
  180. * @param string
  181. * @throws ArgumentException .
  182. */
  183. public function setValue($value) {
  184. if (!self::valueIsValid($value)) {
  185. throw new ArgumentException('Value is invalid.');
  186. }
  187. $this->value = $value;
  188. }
  189. /**
  190. *
  191. *
  192. *
  193. *
  194. * @return string
  195. */
  196. public function getValue() {
  197. return $this->value;
  198. }
  199. /**
  200. *
  201. *
  202. *
  203. *
  204. * @param string
  205. * @throws ArgumentException .
  206. */
  207. public function setDomain($domain) {
  208. if (!self::domainIsValid($domain)) {
  209. throw new ArgumentException('Domain is invalid.');
  210. }
  211. $this->domain = $domain;
  212. }
  213. /**
  214. *
  215. *
  216. *
  217. *
  218. * @return string
  219. */
  220. public function getDomain() {
  221. return $this->domain;
  222. }
  223. /**
  224. *
  225. *
  226. *
  227. *
  228. * @param mixed
  229. * @throws ArgumentException .
  230. */
  231. public function setMaxAge($maxAge) {
  232. if (!self::maxAgeIsValid($maxAge)) {
  233. throw new ArgumentException('Max age is not a positive integer or NULL.');
  234. }
  235. $this->maxAge = $maxAge;
  236. }
  237. /**
  238. *
  239. *
  240. *
  241. *
  242. * @return mixed
  243. */
  244. public function getMaxAge() {
  245. return $this->maxAge;
  246. }
  247. /**
  248. *
  249. *
  250. *
  251. *
  252. * @param string
  253. * @throws ArgumentException .
  254. */
  255. public function setPath($path) {
  256. if (!self::pathIsValid($path)) {
  257. throw new ArgumentException('Path is invalid.');
  258. }
  259. $this->path = $path;
  260. }
  261. /**
  262. *
  263. *
  264. *
  265. *
  266. * @return string
  267. */
  268. public function getPath() {
  269. return $this->path;
  270. }
  271. /**
  272. *
  273. *
  274. *
  275. *
  276. * @param bool
  277. */
  278. public function setSecure($secure) {
  279. $this->secure = $secure;
  280. }
  281. /**
  282. *
  283. *
  284. *
  285. *
  286. * @return bool
  287. */
  288. public function isSecure() {
  289. return $this->secure;
  290. }
  291. }
  292. ?>