Subscribe.class.php
3.4 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* 应用订阅的操作类
*
* @name Facade_Subscribe
* @package facade
* @copyright yoho.inc
* @version 5.0 (2014-02-08 10:59:47)
* @author fei.hong <fei.hong@yoho.cn>
*/
class Facade_Subscribe
{
private static $_service = null;
const APP_KEY_YOHOBUY = '1392109259';
const APP_KEY_SHOW = '1392109210';
/**
* 单例模式实例化服务层对象
*
* @return object
*/
private static function service()
{
if (null === self::$_service)
{
self::$_service = new Service_Subscribe();
}
return self::$_service;
}
/**
* 添加订阅
*
* @param string $appId 应用的ID
* @param string $appSecret 应用的密钥
* @param string $name 应用名称
* @param string $scope 订阅信息(多个之间以逗号“,”分隔开, 格式如nick,gender)
* @param integer $state 应用状态(0:禁用,1:有效)
* @return mixed <false:失败, array:成功>
*/
public static function add($appId, $appSecret, $name, $scope, $state)
{
$result = false;
if (is_string($name) && is_string($scope) && is_numeric($state))
{
$name = trim($name);
$scope = trim(trim($scope, ','));
if ($name !== '' && $scope !== '')
{
$appId = $appId ? strval($appId) : time();
$appSecret = $appSecret ? strval($appSecret) : md5($name.$appId.mt_rand());
$status = self::service()->add($appId, $appSecret, $name, $scope, $state);
if ($status)
{
$result = self::service()->get($appId);
}
}
}
return $result;
}
/**
* 获取订阅
*
* @param string $appId 应用的ID
* @return array
*/
public static function get($appId)
{
$result = array();
if (isset($appId))
{
$result = self::service()->get($appId);
}
return $result;
}
/**
* 获取所有APP
*
* @return array
*/
public static function getAllApps()
{
$result = self::service()->getAllApps();
return $result ? $result : array();
}
/**
* 编辑订阅
*
* @param string $appId 应用的ID
* @param string $newSecret 颁发密钥<optional> (不修改时为null)
* @param string $name 应用名称<optional> (不修改时为null)
* @param string $scope 订阅信息<optional> (不修改时为null)
* @param integer $state 应用状态<optional> (不修改时为null;0:表示禁用,1:表示有效)
* @return mixed <false:失败, array:成功>
*/
public static function upd($appId, $newSecret, $name, $scope, $state)
{
$result = false;
if (isset($appId))
{
$newSecret = is_string($newSecret) ? trim($newSecret) : null;
$name = is_string($name) ? trim($name) : null;
$scope = is_string($scope) ? trim(trim($scope, ',')) : null;
$state = is_numeric($state) ? intval($state) : null;
$status = self::service()->upd($appId, $newSecret, $name, $scope, $state);
if ($status)
{
$result = self::service()->get($appId);
}
}
return $result;
}
}