Completed
Push — master ( 537401...833a82 )
by Angus
02:48
created

generic_helper.php ➔ get_time_class()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 5
nop 1
dl 0
loc 22
ccs 10
cts 10
cp 1
crap 5
rs 8.6737
c 0
b 0
f 0
1
<?php declare(strict_types=1); defined('BASEPATH') or exit('No direct script access allowed');
2
3
/**
4
 * Checks if view file exists.
5
 * @param string $path Location of view file. Use without /views/ & .php
6
 * @return bool
7
 */
8
function view_exists(string $path) : bool {
9 23
	return file_exists(APPPATH . "/views/{$path}.php");
10
}
11
12
function get_time_class(string $time_string) : string {
13 4
	$time = strtotime($time_string);
14
15 4
	if(is_int($time)) {
16 3
		if($time < TIMEAGO_MONTH) {
17
			//More than a month old.
18 1
			$time_string = "sprite-month";
19 2
		} elseif($time < TIMEAGO_WEEK) {
20
			//More than a week old, but less than a month old.
21 1
			$time_string = "sprite-week";
22 1
		} elseif($time < TIMEAGO_3DAY) {
23
			//More than 3 days old but less than a week old.
24
			$time_string = "sprite-3day";
25
		} else {
26
			//Less than a week old.
27 3
			$time_string = "sprite-day";
28
		}
29
	} else {
30 1
		$time_string = "sprite-error"; //TODO: Create the sprite for this
31
	}
32 4
	return $time_string;
33
}
34
35
if (!function_exists('http_parse_headers')) { #http://www.php.net/manual/en/function.http-parse-headers.php#112917
36
	function http_parse_headers ($raw_headers){
37 13
		$headers = array(); // $headers = [];
38 13
		foreach (explode("\n", $raw_headers) as $i => $h) {
39 13
			$h = explode(':', $h, 2);
40 13
			if (isset($h[1])){
41 13
				if(!isset($headers[$h[0]])){
42 13
					$headers[$h[0]] = trim($h[1]);
43 8
				}else if(is_array($headers[$h[0]])){
44 7
					$tmp = array_merge($headers[$h[0]],array(trim($h[1])));
45 7
					$headers[$h[0]] = $tmp;
46
				}else{
47 8
					$tmp = array_merge(array($headers[$h[0]]),array(trim($h[1])));
48 13
					$headers[$h[0]] = $tmp;
49
				}
50
			}
51
		}
52 13
		return $headers;
53
	}
54
}
55