1 | <?php |
||
2 | |||
3 | namespace Anax\Controller; |
||
4 | |||
5 | use Anax\Commons\ContainerInjectableInterface; |
||
6 | use Anax\Commons\ContainerInjectableTrait; |
||
7 | |||
8 | // use Anax\Route\Exception\ForbiddenException; |
||
9 | // use Anax\Route\Exception\NotFoundException; |
||
10 | // use Anax\Route\Exception\InternalErrorException; |
||
11 | |||
12 | /** |
||
13 | * A sample controller to show how a controller class can be implemented. |
||
14 | * The controller will be injected with $di if implementing the interface |
||
15 | * ContainerInjectableInterface, like this sample class does. |
||
16 | * The controller is mounted on a particular route and can then handle all |
||
17 | * requests for that mount point. |
||
18 | * |
||
19 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) |
||
20 | */ |
||
21 | class IpGeoJsonController implements ContainerInjectableInterface |
||
22 | { |
||
23 | use ContainerInjectableTrait; |
||
24 | |||
25 | |||
26 | |||
27 | /** |
||
28 | * @var string $db a sample member variable that gets initialised |
||
29 | */ |
||
30 | private $db = "not active"; |
||
31 | |||
32 | |||
33 | |||
34 | /** |
||
35 | * The initialize method is optional and will always be called before the |
||
36 | * target method/action. This is a convienient method where you could |
||
37 | * setup internal properties that are commonly used by several methods. |
||
38 | * |
||
39 | * @return void |
||
40 | */ |
||
41 | public function initialize() : void |
||
42 | { |
||
43 | // Use to initialise member variables. |
||
44 | $this->db = "active"; |
||
45 | $this->IpCheck = new IpCheck(); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
46 | $this->IpCurl = new IpCurl(); |
||
0 ignored issues
–
show
|
|||
47 | } |
||
48 | |||
49 | public function indexActionGet() : array |
||
50 | { |
||
51 | $request = $this->di->get("request"); |
||
52 | |||
53 | $ip = $request->getGet("ip"); |
||
54 | |||
55 | if ($this->IpCheck->validateIp($ip)) { |
||
56 | $api_result = $this->IpCurl->curl($ip); |
||
57 | $type = $api_result['type']; |
||
58 | $ort = $api_result['region_name']; |
||
59 | $country = $api_result['country_name']; |
||
60 | $domain = $this->IpCheck->validateDomain($ip); |
||
61 | $json = [ |
||
62 | "valid" => "True", |
||
63 | "ip" => $ip, |
||
64 | "domain" => $domain, |
||
65 | "type" => $type, |
||
66 | "ort" => $ort, |
||
67 | "country" => $country, |
||
68 | ]; |
||
69 | } else { |
||
70 | $json = [ |
||
71 | "valid" => "False", |
||
72 | "ip" => $ip, |
||
73 | "domain" => "Not found", |
||
74 | ]; |
||
75 | } |
||
76 | |||
77 | |||
78 | $title = "Ip Validator with Geo"; |
||
0 ignored issues
–
show
|
|||
79 | |||
80 | |||
81 | |||
82 | return [$json]; |
||
83 | } |
||
84 | } |
||
85 |