Completed
Push — master ( 8e24d3...3f57d6 )
by Eric
02:26
created

IpModelTrait::setIpstackUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 1
dl 0
loc 3
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
/**
3
 * Showing off a standard class with methods and properties.
4
 */
5
namespace Erjh17\CallUrlModel;
6
7
use Anax\Commons\ContainerInjectableInterface;
8
use Anax\Commons\ContainerInjectableTrait;
9
10
trait IpModelTrait
11
{
12
    // use GeoModelTrait;
13
    // use WeatherModelTrait;
14
    // use ContainerInjectableTrait;
15
    /**
16
     * @var int        $ipAddress  The ip address.
17
     * @var string     $message  The message.
18
     * @var boolean    $valid  If the ip address is valid.
19
     * @var string     $host  The ip adress' host.
20
     */
21
22
    private $ipAddress;
23
    private $message;
24
    private $valid;
25
    private $host;
26
    private $errorMsg;
27
    private $ipstackkey;
28
29
    /**
30
     * [isValidIp description]
31
     *
32
     * @return boolean [description]
33
     */
34
    public function isValidIp()
35
    {
36
        return $this->valid;
37
    }
38
39
    /**
40
     * Returns the data of the IpModel.
41
     *
42
     * @return [array] array of info.
0 ignored issues
show
Documentation Bug introduced by
The doc comment [array] at position 0 could not be parsed: Unknown type name '[' at position 0 in [array].
Loading history...
43
     */
44
    public function getInfo()
45
    {
46
        return array(
47
            'ip' => $this->ipAddress,
48
            'host' => $this->host,
49
            'message' => $this->message,
50
            'valid' => $this->valid,
51
            'errorMsg' => $this->errorMsg
52
        );
53
    }
54
55
    public function fillInfo($result, $keys)
56
    {
57
        foreach ($keys as $value) {
58
            if (array_key_exists($value, $result)) {
59
                $this->$value = $result[$value];
60
            }
61
        }
62
    }
63
64
    /**
65
     * [pushInfo description]
66
     *
67
     * @param  [type] $result [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
68
     * @param  [type] $keys   [description]
69
     *
70
     * @return [type]         [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
71
     */
72
    public function pushInfo($result, $keys)
73
    {
74
        $retArray = [];
75
        foreach ($keys as $value) {
76
            if (array_key_exists($value, $result)) {
77
                $retArray[$value] = $result[$value];
78
            }
79
        }
80
        return $retArray;
81
    }
82
83
    /**
84
     * Validates the ip address stored in the Class instance.
85
     *
86
     * @return [array] Array with data
0 ignored issues
show
Documentation Bug introduced by
The doc comment [array] at position 0 could not be parsed: Unknown type name '[' at position 0 in [array].
Loading history...
87
     */
88
    public function validateIp()
89
    {
90
        if ($this->ipAddress) {
91
            if (filter_var($this->ipAddress, FILTER_VALIDATE_IP)) {
92
                $this->valid = true;
93
                if (filter_var($this->ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
94
                    $this->message = "$this->ipAddress är en giltig IPv4 adress";
95
                    $this->host = gethostbyaddr($this->ipAddress);
96
                } elseif (filter_var($this->ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
97
                    $this->message = "$this->ipAddress är en giltig IPv6 adress";
98
                }
99
            } else {
100
                $this->message = "$this->ipAddress är inte en giltig IP adress";
101
            }
102
        }
103
        return $this->getInfo();
104
    }
105
}
106