1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Moody\ControllerIP; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A sample controller to show how a controller class can be implemented. |
10
|
|
|
* The controller will be injected with $di if implementing the interface |
11
|
|
|
* ContainerInjectableInterface, like this sample class does. |
12
|
|
|
* The controller is mounted on a particular route and can then handle all |
13
|
|
|
* requests for that mount point. |
14
|
|
|
* |
15
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
16
|
|
|
*/ |
17
|
|
|
class JsonIpValidatorController1 extends IpValidate implements ContainerInjectableInterface |
18
|
|
|
{ |
19
|
|
|
use ContainerInjectableTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $enteredIp; |
25
|
|
|
private $ipValidatorClass; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* This is the index method action, it handles: |
29
|
|
|
* ANY METHOD mountPoint |
30
|
|
|
* ANY METHOD mountPoint/ |
31
|
|
|
* ANY METHOD mountPoint/index |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public function indexAction() : array |
36
|
|
|
{ |
37
|
|
|
$title = "IP validator with JSON response"; |
|
|
|
|
38
|
|
|
$page = $this->di->get("page"); |
|
|
|
|
39
|
|
|
$request = $this->di->get("request"); |
40
|
|
|
$this->enteredIp = $request->getGet("ip"); |
41
|
|
|
$this->ipValidatorClass = new IpValidate(); |
42
|
|
|
$json = $this->ipToJson($this->enteredIp, $this->ipValidatorClass); |
43
|
|
|
$data['json'] = $json; |
|
|
|
|
44
|
|
|
|
45
|
|
|
return $data['json']; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* This is the index method action, it handles: |
50
|
|
|
* ANY METHOD mountPoint |
51
|
|
|
* ANY METHOD mountPoint/ |
52
|
|
|
* ANY METHOD mountPoint/index |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function jsonIpActionGet() : array |
57
|
|
|
{ |
58
|
|
|
$title = "IP validator with JSON response"; |
|
|
|
|
59
|
|
|
$page = $this->di->get("page"); |
|
|
|
|
60
|
|
|
$request = $this->di->get("request"); |
61
|
|
|
$this->enteredIp = $request->getGet("ip"); |
62
|
|
|
$this->ipValidatorClass = new IpValidate(); |
63
|
|
|
$json = $this->ipToJson($this->enteredIp, $this->ipValidatorClass); |
64
|
|
|
$data['json'] = $json; |
|
|
|
|
65
|
|
|
|
66
|
|
|
return $data['json']; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Check if IP is valid or not and return with host. |
72
|
|
|
* GET ip |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function ipToJson($ipAddress, $object) : array |
77
|
|
|
{ |
78
|
|
|
$json = [ |
79
|
|
|
"Protocol" => $object->getProtocol($ipAddress) ?? null, |
80
|
|
|
"Domain" => $object->getDomain($ipAddress) ?? null, |
81
|
|
|
"address" => $object->getAddress($ipAddress) ?? null, |
82
|
|
|
]; |
83
|
|
|
return [$json]; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|