Completed
Push — v2 ( e6c7b3...40717e )
by Beñat
06:35
created

NetworkPost::fromJson()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 11
rs 8.8571
cc 5
eloc 8
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Stack Exchange Api Client library.
5
 *
6
 * Copyright (c) 2014-2016 Beñat Espiña <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace BenatEspina\StackExchangeApiClient\Model;
13
14
/**
15
 * The network post model class.
16
 *
17
 * @author Beñat Espiña <[email protected]>
18
 */
19
class NetworkPost implements Model
20
{
21
    const POST_TYPES = ['question', 'answer'];
22
23
    protected $id;
24
    protected $postType;
25
    protected $score;
26
    protected $title;
27
28
    public static function fromProperties($id, $postType, $score, $title)
29
    {
30
        $instance = new self();
31
        $instance
32
            ->setId($id)
33
            ->setPostType($postType)
34
            ->setScore($score)
35
            ->setTitle($title);
36
37
        return $instance;
38
    }
39
40
    public static function fromJson(array $data)
41
    {
42
        $instance = new self();
43
        $instance
44
            ->setId(array_key_exists('post_id', $data) ? $data['post_id'] : null)
45
            ->setPostType(array_key_exists('post_type', $data) ? $data['post_type'] : null)
46
            ->setScore(array_key_exists('score', $data) ? $data['score'] : null)
47
            ->setTitle(array_key_exists('title', $data) ? $data['title'] : null);
48
49
        return $instance;
50
    }
51
52
    public function getId()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
53
    {
54
        return $this->id;
55
    }
56
57
    public function setId($id)
58
    {
59
        $this->id = $id;
60
61
        return $this;
62
    }
63
64
    public function getPostType()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
65
    {
66
        return $this->postType;
67
    }
68
69
    public function setPostType($postType)
70
    {
71
        if (in_array($postType, self::POST_TYPES, true)) {
72
            $this->postType = $postType;
73
        }
74
75
        return $this;
76
    }
77
78
    public function getScore()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
79
    {
80
        return $this->score;
81
    }
82
83
    public function setScore($score)
84
    {
85
        $this->score = $score;
86
87
        return $this;
88
    }
89
90
    public function getTitle()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
91
    {
92
        return $this->title;
93
    }
94
95
    public function setTitle($title)
96
    {
97
        $this->title = $title;
98
99
        return $this;
100
    }
101
}
102