Test Failed
Push — master ( 313166...27e48c )
by Jean-Christophe
20:50 queued 11s
created

URequestTesterTrait::isPost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Ubiquity\utils\http\traits;
4
5
use Ubiquity\controllers\Startup;
6
7
/**
8
 * Ubiquity\utils\http\traits$URequestTesterTrait
9
 * This class is part of Ubiquity
10
 * @author jc
11
 * @version 1.0.0
12
 *
13
 */
14
trait URequestTesterTrait {
15
	/**
16
	 * Tests if a value is present on the request and is not empty
17
	 *
18
	 * @param string $key
19
	 *
20
	 * @return bool
21
	 */
22
	public static function filled($key): bool {
23
		return isset ( $_REQUEST [$key] ) && $_REQUEST [$key] != null;
24
	}
25
26
	/**
27
	 * Tests if a value is present on the request
28
	 *
29
	 * @param string $key
30
	 *
31
	 * @return bool
32
	 */
33
	public static function has($key): bool {
34
		return isset ( $_REQUEST [$key] );
35
	}
36
37
	/**
38
	 * Returns true if the request is an Ajax request
39
	 *
40
	 * @return boolean
41
	 */
42
	public static function isAjax(): bool {
43
		return (isset ( $_SERVER ['HTTP_X_REQUESTED_WITH'] ) && ! empty ( $_SERVER ['HTTP_X_REQUESTED_WITH'] ) && strtolower ( $_SERVER ['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest');
44
	}
45
46
	/**
47
	 * Returns true if the request is sent by the POST method
48
	 *
49
	 * @return boolean
50
	 */
51
	public static function isPost(): bool {
52
		return $_SERVER ['REQUEST_METHOD'] === 'POST';
53
	}
54
55
	/**
56
	 * Returns true if the request is cross site
57
	 *
58
	 * @return boolean
59
	 */
60
	public static function isCrossSite(): bool {
61
		return \stripos ( $_SERVER ['HTTP_REFERER'], $_SERVER ['SERVER_NAME'] ) === FALSE;
62
	}
63
64
	/**
65
	 * Returns true if request contentType is set to json
66
	 *
67
	 * @return boolean
68
	 */
69
	public static function isJSON(): bool {
70
		$contentType = self::getContentType ();
71
		return \stripos ( $contentType, 'json' ) !== false;
72
	}
73
74
	/**
75
	 * Returns the request content-type header
76
	 *
77
	 * @return string
78
	 */
79
	public static function getContentType(): ?string {
80
		$headers = Startup::getHttpInstance ()->getAllHeaders ();
81
		if (isset ( $headers ['Content-Type'] )) {
82
			return $headers ['Content-Type'];
83
		}
84
		return null;
85
	}
86
}
87
88