-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCrossPosting.php
152 lines (132 loc) · 3.96 KB
/
CrossPosting.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/*
* Класс компонет для кросспостинга
*/
namespace bemulima\crossposting;
use Yii;
use yii\base\Component;
use yii\base\InvalidParamException;
use yii\web\UrlManager;
use frontend\config\urlrule\ViewUrlRule;
/**
* Description of CrossPosting
*
* @author Programmer
*/
class CrossPosting extends Component {
/**
* Массив стен, соцсетей
* @var array
*/
private $_services = [];
/**
* пебликуемый текст
* @var string
*/
private $_text;
/**
* урл поста
* @var string
*/
private $_url;
/**
* путь до картинки
* @var string
*/
private $_images;
/**
* @param array $images puth to img
* @return $this
*/
public function images(array $images) {
$this->_images = $images;
return $this;
}
/**
* @param string $text text of post
* @return $this
*/
public function text($text) {
$this->_text = $text;
return $this;
}
/**
* @param string|array $url url to post
* @return $this
*/
public function url($url) {
if(is_array($url)){
$urlrule = new ViewUrlRule;
$this->_url = $urlrule->createUrl((new UrlManager), 'ad/view', $url);
}else
$this->_url = $url;
return $this;
}
/**
* @param array $services list
*/
public function setServices(array $services)
{
$this->_services = $services;
}
/**
* Возвращает объект для кросспостинга
* @return ServiceInterface[]
*/
public function getServices()
{
$services = [];
foreach ($this->_services as $name) {
$services[$name] = $this->getService($name);
}
return $services;
}
/**
* @param string $name crossposting name.
* @return ServiceInterface.
* @throws InvalidParamException on non existing client request.
*/
public function service(string $name)
{
if (!array_key_exists($name, $this->_services)) {
throw new InvalidParamException("Unknown crossposting service '{$name}'.");
}
if(empty($this->_services[$name]['groups']))
throw new InvalidParamException("Необходимо ввести хотя б ID одной группы.");
$groups = [];
foreach($this->_services[$name]['groups'] as $key => $val){
$groups[] = $val;
}
$params = [
'class' => $this->_services[$name]['class'],
'accessToken' => $this->_services[$name]['accessToken'],
'privateKey' => $this->_services[$name]['privateKey'],
'publicKey' => $this->_services[$name]['publicKey'],
'appID' => $this->_services[$name]['appID'],
'wallIDs' => $groups,
];
if(!empty($this->_text)){
$params['text'] = $this->_text;
}else{
throw new InvalidParamException("Необходимо ввести текст.");
}
if(!empty($this->_url)){
$params['url'] = $this->_url;
}else{
throw new InvalidParamException("Необходимо ввести url.");
}
if(!empty($this->_images)){
$params['images'] = $this->_images;
}
return $this->createService($params);
}
/**
* Создает объект класс для кросспостинга
* @param array $config конфигурация класса кросспостинга
* @return ServiceInterface.
*/
protected function createService($config)
{
return Yii::createObject($config);
}
}