|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Habrahabr\Api\Resources; |
|
4
|
|
|
|
|
5
|
|
|
use Habrahabr\Api\Exception\IncorrectUsageException; |
|
6
|
|
|
use Habrahabr\Api\HttpAdapter\HttpAdapterInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class AbstractResource |
|
10
|
|
|
* |
|
11
|
|
|
* Базовый класс для всех Habrahabr Api ресурсов |
|
12
|
|
|
* |
|
13
|
|
|
* @package Habrahabr\Api\Resources |
|
14
|
|
|
* @version 0.1.5 |
|
15
|
|
|
* @author thematicmedia <[email protected]> |
|
16
|
|
|
* @link https://tmtm.ru/ |
|
17
|
|
|
* @link https://habrahabr.ru/ |
|
18
|
|
|
* @link https://github.com/thematicmedia/habrahabr_api |
|
19
|
|
|
* |
|
20
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
21
|
|
|
* file that was distributed with this source code. |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class AbstractResource |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var HttpAdapterInterface|null Экземпляр Habrahabr Api HTTP адаптера |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $adapter; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Установить экземпляр Habrahabr Api HTTP адаптера для Habrahabr Api ресурса |
|
32
|
|
|
* |
|
33
|
|
|
* @param HttpAdapterInterface $adapter Экземпляр Habrahabr Api HTTP адаптера |
|
34
|
|
|
* @return $this |
|
35
|
|
|
*/ |
|
36
|
96 |
|
public function setAdapter(HttpAdapterInterface $adapter) |
|
37
|
|
|
{ |
|
38
|
96 |
|
$this->adapter = $adapter; |
|
39
|
|
|
|
|
40
|
96 |
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Проверяет ID на валидность |
|
45
|
|
|
* |
|
46
|
|
|
* @param mixed $id ID |
|
47
|
|
|
* @return bool |
|
48
|
|
|
* @throws IncorrectUsageException |
|
49
|
|
|
*/ |
|
50
|
28 |
|
protected function checkId($id) |
|
51
|
|
|
{ |
|
52
|
28 |
|
if (intval($id) != $id || $id < 1) { |
|
53
|
11 |
|
throw new IncorrectUsageException('Id must be integer and positive'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
18 |
|
return true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Проверяет Алиас на валидность |
|
61
|
|
|
* |
|
62
|
|
|
* @param mixed $alias Алиас |
|
63
|
|
|
* @return bool |
|
64
|
|
|
* @throws IncorrectUsageException |
|
65
|
|
|
*/ |
|
66
|
30 |
|
protected function checkAliasName($alias) |
|
67
|
|
|
{ |
|
68
|
30 |
|
if (!preg_match('/^[a-z0-9\-_]+$/i', $alias)) { |
|
69
|
9 |
|
throw new IncorrectUsageException('Alias must be string without special chars'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
21 |
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Проверяет номер страницы на валидность |
|
77
|
|
|
* |
|
78
|
|
|
* @param mixed $page Номер страницы |
|
79
|
|
|
* @return bool |
|
80
|
|
|
* @throws IncorrectUsageException |
|
81
|
|
|
*/ |
|
82
|
30 |
|
protected function checkPageNumber($page = 1) |
|
83
|
|
|
{ |
|
84
|
30 |
|
if (intval($page) != $page || $page < 1) { |
|
85
|
5 |
|
throw new IncorrectUsageException('Page number must be integer and positive'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
25 |
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|