Completed
Push — master ( dc230f...bc9de0 )
by Byron
02:42
created

UrlResource::createFromJson()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 12
1
<?php
2
/**
3
 * This file is part of the badams\GoogleUrl library
4
 *
5
 * @license http://opensource.org/licenses/MIT
6
 * @link https://github.com/badams/google-url
7
 * @package badams/google-url
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace badams\GoogleUrl;
14
15
/**
16
 * Class UrlResource
17
 * @package badams\GoogleUrl
18
 * @link https://developers.google.com/url-shortener/v1/url#resource
19
 */
20
class UrlResource implements \JsonSerializable
21
{
22
    /**
23
     * @var string
24
     */
25
    public $kind = 'urlshortener#url';
26
27
    /**
28
     * Short URL, e.g. "http://goo.gl/l6MS".
29
     *
30
     * @var string
31
     */
32
    public $id;
33
34
    /**
35
     * Long URL, e.g. "http://www.google.com/".
36
     * Might not be present if the status is "REMOVED".
37
     *
38
     * @var string|null
39
     */
40
    public $longUrl;
41
42
    /**
43
     * Status of the target URL.
44
     *
45
     * Possible values: "OK", "MALWARE", "PHISHING", or "REMOVED".
46
     * A URL might be marked "REMOVED" if it was flagged as spam, for example.
47
     *
48
     * @var string
49
     */
50
    public $status;
51
52
    /**
53
     * @param \stdClass $json
54
     * @return UrlResource
55
     */
56
    public static function createFromJson(\stdClass $json)
57
    {
58
        $resource = new UrlResource();
59
        $class = new \ReflectionClass($resource);
60
61
        foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
62
            if (isset($json->{$property->getName()})) {
63
                $resource->{$property->getName()} = $json->{$property->getName()};
64
            }
65
        }
66
67
        return $resource;
68
    }
69
70
    /**
71
     * Serialize this resource into an array for json
72
     *
73
     * @return array
74
     */
75
    public function jsonSerialize()
76
    {
77
        $class = new \ReflectionClass($this);
78
        $json = [];
79
80
        foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
81
            if (null !== ($value = $property->getValue($this))) {
82
                $json[$property->getName()] = $value;
83
            }
84
        }
85
86
        return $json;
87
    }
88
}