Twitter::locationNormalizer()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Oryzone PHPoAuthUserData package <https://github.com/Oryzone/PHPoAuthUserData>.
5
 *
6
 * (c) Oryzone, developed by Luciano Mammino <[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 OAuth\UserData\Extractor;
13
14
use OAuth\UserData\Utils\ArrayUtils;
15
16
/**
17
 * Class Twitter
18
 * @package OAuth\UserData\Extractor
19
 */
20
class Twitter extends LazyExtractor
21
{
22
    /**
23
     * Request constants
24
     */
25
    const REQUEST_PROFILE = '/account/verify_credentials.json';
26
27
    /**
28
     * Constructor
29
     */
30
    public function __construct()
31
    {
32
        parent::__construct(
33
            self::getDefaultLoadersMap(),
34
            self::getDefaultNormalizersMap(),
35
            self::getSupportedFields()
36
        );
37
    }
38
39
    protected static function getSupportedFields()
40
    {
41
        return array(
42
            self::FIELD_UNIQUE_ID,
43
            self::FIELD_USERNAME,
44
            self::FIELD_FULL_NAME,
45
            self::FIELD_FIRST_NAME,
46
            self::FIELD_LAST_NAME,
47
            self::FIELD_DESCRIPTION,
48
            self::FIELD_LOCATION,
49
            self::FIELD_PROFILE_URL,
50
            self::FIELD_IMAGE_URL,
51
            self::FIELD_WEBSITES,
52
            self::FIELD_EXTRA
53
        );
54
    }
55
56
    protected function profileLoader()
57
    {
58
        return json_decode($this->service->request(self::REQUEST_PROFILE), true);
59
    }
60
61
    protected function uniqueIdNormalizer($data)
62
    {
63
        return $data['id'];
64
    }
65
66
    protected function usernameNormalizer($data)
67
    {
68
        return isset($data['screen_name']) ? $data['screen_name'] : null;
69
    }
70
71
    protected function fullNameNormalizer($data)
72
    {
73
        return isset($data['name']) ? $data['name'] : null;
74
    }
75
76
    protected function firstNameNormalizer()
77
    {
78
        $fullName = $this->getField(self::FIELD_FULL_NAME);
79
        if ($fullName) {
80
            $names = explode(' ', $fullName);
81
82
            return $names[0];
83
        }
84
85
        return null;
86
    }
87
88
    protected function lastNameNormalizer()
89
    {
90
        $fullName = $this->getField(self::FIELD_FULL_NAME);
91
        if ($fullName) {
92
            $names = explode(' ', $fullName);
93
94
            return $names[sizeof($names) - 1];
95
        }
96
97
        return null;
98
    }
99
100
    protected function descriptionNormalizer($data)
101
    {
102
        return isset($data['description']) ? $data['description'] : null;
103
    }
104
105
    protected function locationNormalizer($data)
106
    {
107
        return isset($data['location']) ? $data['location'] : null;
108
    }
109
110
    protected function imageUrlNormalizer($data)
111
    {
112
        return isset($data['profile_image_url']) ? $data['profile_image_url'] : null;
113
    }
114
115
    protected function profileUrlNormalizer($data)
116
    {
117
        return isset($data['screen_name']) ? sprintf('https://twitter.com/%s', $data['screen_name']) : null;
118
    }
119
120
    protected function websitesNormalizer($data)
121
    {
122
        $websites = array();
123
        if (isset($data['url'])) {
124
            $websites[] = $data['url'];
125
        }
126
        if (isset($data['entities']['url']['urls'])) {
127
            foreach ($data['entities']['url']['urls'] as $urlData) {
128
                $websites[] = $urlData['expanded_url'];
129
            }
130
        }
131
132
        return array_unique($websites);
133
    }
134
135
    protected function extraNormalizer($data)
136
    {
137
        return ArrayUtils::removeKeys($data, array(
138
            'id',
139
            'screen_name',
140
            'name',
141
            'description',
142
            'location',
143
            'url',
144
            'profile_image_url',
145
        ));
146
    }
147
}
148