Completed
Push — master ( 53eae5...5a39da )
by Angus
03:43
created

generic_helper.php ➔ array_keys_exist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
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 19
	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';
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 (string $raw_headers) : array {
37
		$headers = array(); // $headers = [];
38
		foreach (explode("\n", $raw_headers) as $i => $h) {
39
			$h = explode(':', $h, 2);
40
			if (isset($h[1])){
41
				if(!isset($headers[$h[0]])){
42
					$headers[$h[0]] = trim($h[1]);
43
				}else if(is_array($headers[$h[0]])){
44
					$tmp = array_merge($headers[$h[0]],array(trim($h[1])));
45
					$headers[$h[0]] = $tmp;
46
				}else{
47
					$tmp = array_merge(array($headers[$h[0]]),array(trim($h[1])));
48
					$headers[$h[0]] = $tmp;
49
				}
50
			}
51
		}
52
		return $headers;
53
	}
54
}
55
56
function get_notices() : string {
57 17
	$CI = get_instance();
58
59 17
	$notice_str = '';
60 17
	if(function_exists('validation_errors') && validation_errors()) {
61
		$notice_str = validation_errors();
62 17
	} elseif($CI->ion_auth->errors()) {
63 1
		$notice_str = $CI->ion_auth->errors();
64 16
	} elseif($CI->ion_auth->messages()) {
65
		$notice_str = $CI->ion_auth->messages();
66 16
	} elseif($notices = $CI->session->flashdata('errors')) {
67
		$CI->session->unset_userdata('errors'); //Sometimes we call this flashdata without redirecting, so make sure we remove it
68 View Code Duplication
		if(is_string($notices)) {
69
			$notice_str = $CI->config->item('error_start_delimiter', 'ion_auth') . $notices . $CI->config->item('error_end_delimiter', 'ion_auth');
70
		} elseif(is_array($notices)) {
71
			foreach($notices as $notice) {
72
				$notice_str .= $CI->config->item('error_start_delimiter', 'ion_auth') . $notice . $CI->config->item('error_end_delimiter', 'ion_auth');
73
			}
74
		}
75 16
	} elseif($notices = $CI->session->flashdata('notices')) {
76 1
		$CI->session->unset_userdata('notices'); //Sometimes we call this flashdata without redirecting, so make sure we remove it
77 1 View Code Duplication
		if(is_string($notices)) {
78 1
			$notice_str = $CI->config->item('message_start_delimiter', 'ion_auth') . $notices . $CI->config->item('message_end_delimiter', 'ion_auth');
79
		} elseif(is_array($notices)) {
80
			foreach($notices as $notice) {
81
				$notice_str .= $CI->config->item('message_start_delimiter', 'ion_auth') . $notice . $CI->config->item('message_end_delimiter', 'ion_auth');
82
			}
83
		}
84
	}
85 17
	return $notice_str;
86
}
87
function exit_ci($status = NULL) : void {
88
	if(ENVIRONMENT !== 'testing') {
89
		exit($status);
90
	} else {
91
		throw new CIPHPUnitTestExitException('exit() called');
92
	}
93
}
94
95
function array_keys_exist(array $keys, array $array) : bool {
96
	return !array_diff($keys, array_keys($array));
97
}
98