PagSeguroHelper   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 148
rs 10
wmc 23
lcom 0
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A formatDate() 0 12 3
A decimalFormat() 0 9 2
A subDays() 0 7 1
A printError() 0 12 4
A removeStringExtraSpaces() 0 4 1
A truncateValue() 0 15 4
A formatString() 0 5 1
A isEmpty() 0 4 2
A isNotificationEmpty() 0 12 4
A getOnlyNumbers() 0 4 1
1
<?php
2
/**
3
 * 2007-2014 [PagSeguro Internet Ltda.]
4
 *
5
 * NOTICE OF LICENSE
6
 *
7
 *Licensed under the Apache License, Version 2.0 (the "License");
8
 *you may not use this file except in compliance with the License.
9
 *You may obtain a copy of the License at
10
 *
11
 *http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 *Unless required by applicable law or agreed to in writing, software
14
 *distributed under the License is distributed on an "AS IS" BASIS,
15
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 *See the License for the specific language governing permissions and
17
 *limitations under the License.
18
 *
19
 *  @author    PagSeguro Internet Ltda.
20
 *  @copyright 2007-2014 PagSeguro Internet Ltda.
21
 *  @license   http://www.apache.org/licenses/LICENSE-2.0
22
 */
23
24
/***
25
 * Helper functions
26
 */
27
class PagSeguroHelper
28
{
29
30
    /***
31
     * @param $date
32
     * @return bool|string
33
     */
34
    public static function formatDate($date)
35
    {
36
        $format = DateTime::ATOM;
37
        if ($date instanceof DateTime) {
38
            $d = $date->format($format);
39
        } elseif (is_numeric($date)) {
40
            $d = date($format, $date);
41
        } else {
42
            $d = (string) $date;
43
        }
44
        return $d;
45
    }
46
47
    /***
48
     * @param $value
49
     * @return string
50
     */
51
    public static function decimalFormat($value)
52
    {
53
        if (is_float($value)) {
54
            $value = (float) $value;
55
            $value = floor($value * 100) / 100;
56
            $value = (string) number_format($value, 2, '.', '');
57
        }
58
        return $value;
59
    }
60
61
    /***
62
     * @param $date
63
     * @param $days
64
     * @return bool|string
65
     */
66
    public static function subDays($date, $days)
67
    {
68
        $d = self::formatDate($date);
69
        $d = date_parse($d);
70
        $d = mktime($d['hour'], $d['minute'], $d['second'], $d['month'], $d['day'] - $days, $d['year']);
71
        return self::formatDate($d);
72
    }
73
74
    /***
75
     * @param $var
76
     * @param null $dump
77
     */
78
    public static function printError($var, $dump = null)
79
    {
80
        if (is_array($var) || is_object($var)) {
81
            echo "<pre>";
82
            if ($dump) {
83
                var_dump($var);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($var); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
84
            } else {
85
                print_r($var);
86
            }
87
            echo "</pre>";
88
        }
89
    }
90
91
    /***
92
     * Remove left, right and inside extra spaces in string
93
     * @param string $string
94
     * @return string
95
     */
96
    public static function removeStringExtraSpaces($string)
97
    {
98
        return trim(preg_replace("/( +)/", " ", $string));
99
    }
100
101
    /***
102
     * Perform truncate of string value
103
     * @param string $string
104
     * @param int $limit
105
     * @param mixed $endchars
106
     * @return string
107
     */
108
    public static function truncateValue($string, $limit, $endchars = '...')
109
    {
110
111
        if (!is_array($string) && !is_object($string)) {
112
113
            $stringLength = strlen($string);
114
            $endcharsLength = strlen($endchars);
115
116
            if ($stringLength > (int) $limit) {
117
                $cut = (int) ($limit - $endcharsLength);
118
                $string = substr($string, 0, $cut) . $endchars;
119
            }
120
        }
121
        return $string;
122
    }
123
124
    /***
125
     * Return formatted string to send in PagSeguro request
126
     * @param string $string
127
     * @param int $limit
128
     * @param mixed $endchars
129
     * @return string
130
     */
131
    public static function formatString($string, $limit, $endchars = '...')
132
    {
133
        $string = PagSeguroHelper::removeStringExtraSpaces($string);
134
        return PagSeguroHelper::truncateValue($string, $limit, $endchars);
135
    }
136
137
    /***
138
     * Check if var is empty
139
     * @param string $value
140
     * @return boolean
141
     */
142
    public static function isEmpty($value)
143
    {
144
        return (!isset($value) || trim($value) == "");
145
    }
146
147
    /***
148
     * Check if notification post is empty
149
     * @param array $notification_data
150
     * @return boolean
151
     */
152
    public static function isNotificationEmpty(array $notification_data)
153
    {
154
        $isEmpty = true;
155
156
        if (isset($notification_data['notificationCode']) && isset($notification_data['notificationType'])) {
157
            $isEmpty = (PagSeguroHelper::isEmpty($notification_data['notificationCode']) ||
158
                            PagSeguroHelper::isEmpty($notification_data['notificationType'])
159
                       );
160
        }
161
162
        return $isEmpty;
163
    }
164
165
    /***
166
     * Remove all non digit character from string
167
     * @param string $value
168
     * @return string
169
     */
170
    public static function getOnlyNumbers($value)
171
    {
172
        return preg_replace('/\D/', '', $value);
173
    }
174
}
175