1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace vinicinbgs\Autentique\Utils; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
class Query |
8
|
|
|
{ |
9
|
|
|
const DIR = "queries/"; |
10
|
|
|
|
11
|
|
|
/** @var string */ |
12
|
|
|
protected $resource; |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
protected $file; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Query constructor. |
19
|
|
|
*/ |
20
|
|
|
public function __construct(string $resource) |
21
|
|
|
{ |
22
|
|
|
$this->resource = __DIR__ . "/../" . self::DIR . strtolower($resource); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Get query |
27
|
|
|
* |
28
|
|
|
* @return string|string[]|null |
29
|
|
|
*/ |
30
|
|
|
public function query(string $file) |
31
|
|
|
{ |
32
|
|
|
if (!file_exists("$this->resource/$file") || empty($file)) { |
33
|
|
|
throw new Exception("File '$file' is not found", 404); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$query = file_get_contents("$this->resource/$file"); |
37
|
|
|
|
38
|
|
|
return $this->formatQueryRemoveBrokenLine($query); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Format query to remove LF (line feed \n) and CR (carriege return \r) |
43
|
|
|
* |
44
|
|
|
* @param string $query |
45
|
|
|
* @return string|string[]|null |
46
|
|
|
*/ |
47
|
|
|
private function formatQueryRemoveBrokenLine(string $query) |
48
|
|
|
{ |
49
|
|
|
return preg_replace("/[\n\r]/", "", $query); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* set Variables in query adding value |
54
|
|
|
* |
55
|
|
|
* @param string|array $variables |
56
|
|
|
* @param string|array $value |
57
|
|
|
* @param string $graphQuery |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function setVariables($variables, $value, string $graphQuery): string |
61
|
|
|
{ |
62
|
|
|
if (is_array($variables) && is_array($value)) { |
63
|
|
|
$variablesLength = count($variables); |
64
|
|
|
|
65
|
|
|
for ($i = 0; $i < $variablesLength; $i++) { |
66
|
|
|
$variable = "\$" . $variables[$i]; |
67
|
|
|
$graphQuery = str_replace($variable, $value[$i], $graphQuery); |
68
|
|
|
} |
69
|
|
|
} elseif (is_string($variables)) { |
70
|
|
|
$graphQuery = str_replace("\$" . $variables, $value, $graphQuery); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $graphQuery; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|