-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathUtil.php
113 lines (104 loc) · 3.27 KB
/
Util.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace Samwilson\PhpFlickr;
class Util
{
public const BASE58_ALPHABET = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
public const PRIVACY_PUBLIC = 1;
public const PRIVACY_FRIENDS = 2;
public const PRIVACY_FAMILY = 3;
public const PRIVACY_FRIENDS_FAMILY = 4;
public const PRIVACY_PRIVATE = 5;
public const DATE_GRANULARITY_EXACT = 0;
public const DATE_GRANULARITY_MONTH = 4;
public const DATE_GRANULARITY_YEAR = 6;
public const DATE_GRANULARITY_CIRCA = 8;
/**
* Encode a photo ID to Flickr's short-URL base-58 system.
* @link https://www.flickr.com/groups/api/discuss/72157616713786392/
* @param int $num
* @return string
*/
public static function base58encode($num)
{
$base_count = strlen(static::BASE58_ALPHABET);
$encoded = '';
while ($num >= $base_count) {
$div = $num / $base_count;
$mod = ($num - ($base_count * intval($div)));
$encoded = static::BASE58_ALPHABET[$mod] . $encoded;
$num = intval($div);
}
if ($num) {
$encoded = static::BASE58_ALPHABET[$num] . $encoded;
}
return $encoded;
}
/**
* Decode a photo ID from Flickr's short-URL base-58 system.
* @link https://www.flickr.com/groups/api/discuss/72157616713786392/
* @param string $num
* @return bool|int
*/
public static function base58decode($num)
{
$decoded = 0;
$multi = 1;
while (strlen($num) > 0) {
$digit = $num[strlen($num) - 1];
$decoded += $multi * strpos(static::BASE58_ALPHABET, $digit);
$multi = $multi * strlen(static::BASE58_ALPHABET);
$num = substr($num, 0, -1);
}
return $decoded;
}
/**
* Get the privacy integer given the three privacy categories.
* @param bool $isPublic
* @param bool $isFriend
* @param bool $isFamily
* @return int
*/
public static function privacyLevel($isPublic, $isFriend, $isFamily)
{
if ($isPublic) {
return static::PRIVACY_PUBLIC;
}
if ($isFriend && $isFamily) {
return static::PRIVACY_FRIENDS_FAMILY;
}
if ($isFriend) {
return static::PRIVACY_FRIENDS;
}
if ($isFamily) {
return static::PRIVACY_FAMILY;
}
return static::PRIVACY_PRIVATE;
}
/**
* Get all privacy levels.
* @return array
*/
public static function getPrivacyLevels()
{
return [
'public' => static::PRIVACY_PUBLIC,
'friends_family' => static::PRIVACY_FRIENDS_FAMILY,
'friends' => static::PRIVACY_FRIENDS,
'family' => static::PRIVACY_FAMILY,
'private' => static::PRIVACY_PRIVATE,
];
}
/**
* Get the name of a given privacy level.
* @param int $id The value of one of the PRIVACY_* constants.
* @return string|bool The name of the privacy leve, or false if it doesn't exist.
*/
public static function getPrivacyLevelById($id)
{
$privacyLevels = array_flip(static::getPrivacyLevels());
if (!isset($privacyLevels[$id])) {
return false;
}
return $privacyLevels[$id];
}
}