Completed
Push — master ( 5686ea...407817 )
by Nazar
05:05
created

Cookie::init_cookie()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 5
nop 1
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 5
rs 8.8571
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 44
	public function init_cookie ($cookie = []) {
23 44
		$this->cookie = $cookie;
24 44
		if (!$cookie) {
25 44
			return;
26
		}
27
		/**
28
		 * Fill un-prefixed keys according to system configuration
29
		 */
30 16
		$prefix        = Config::instance()->core['cookie_prefix'];
31 16
		$prefix_length = strlen($prefix);
32 16
		if ($prefix_length) {
33 2
			foreach ($cookie as $key => $value) {
34 2
				if (strpos($key, $prefix) === 0) {
35 2
					$this->cookie[substr($key, $prefix_length)] = $value;
36
				}
37
			}
38
		}
39 16
	}
40
	/**
41
	 * Get cookie by name
42
	 *
43
	 * @param string $name
44
	 *
45
	 * @return null|string Cookie content if exists or `null` otherwise
46
	 */
47 50
	public function cookie ($name) {
48 50
		return @$this->cookie[$name];
49
	}
50
}
51