Completed
Push — master ( e934ec...298fb7 )
by Nazar
04:47
created

Cookie::cookie()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\Request;
9
use
10
	cs\Config;
11
12
trait Cookie {
13
	/**
14
	 * Cookie array, similar to `$_COOKIE`, but also contains un-prefixed keys according to system configuration
15
	 *
16
	 * @var array
17
	 */
18
	public $cookie;
19
	/**
20
	 * @param array $cookie Typically `$_COOKIE`
21
	 */
22 8
	function init_cookie ($cookie = []) {
23 8
		$this->cookie = $cookie;
24
		/**
25
		 * Fill un-prefixed keys according to system configuration
26
		 */
27 8
		$prefix        = Config::instance()->core['cookie_prefix'];
28 8
		$prefix_length = strlen($prefix);
29 8
		if ($prefix_length) {
30
			foreach ($cookie as $key => $value) {
31
				if (strpos($key, $prefix) === 0) {
32
					$this->cookie[substr($key, $prefix_length)] = $value;
33
				}
34
			}
35
		}
36 8
	}
37
	/**
38
	 * Get cookie by name
39
	 *
40
	 * @param string $name
41
	 *
42
	 * @return null|string Cookie content if exists or `null` otherwise
43
	 */
44 6
	function cookie ($name) {
45 6
		return @$this->cookie[$name];
46
	}
47
}
48