Client
5 months ago
Exception
5 months ago
Message
5 months ago
Protocol
5 months ago
Socket
5 months ago
.htaccess
5 months ago
SocketAPI.php
5 months ago
autoload.php
5 months ago
index.html
5 months ago
web.config
5 months ago
SocketAPI.php
99 lines
| 1 | <?php |
| 2 | /* |
| 3 | * |
| 4 | * JetBackup @ package |
| 5 | * Created By Idan Ben-Ezra |
| 6 | * |
| 7 | * Copyrights @ JetApps |
| 8 | * https://www.jetapps.com |
| 9 | * |
| 10 | **/ |
| 11 | namespace JetBackup\SocketAPI; |
| 12 | |
| 13 | use JetBackup\SocketAPI\Client\Client; |
| 14 | use JetBackup\SocketAPI\Exception\ClientException; |
| 15 | use JetBackup\SocketAPI\Exception\SocketAPIException; |
| 16 | |
| 17 | class SocketAPI { |
| 18 | |
| 19 | private $_data; |
| 20 | private string $_function; |
| 21 | |
| 22 | public function __construct($function) { |
| 23 | $this->_function = $function; |
| 24 | } |
| 25 | /** |
| 26 | * @return array |
| 27 | * @throws SocketAPIException |
| 28 | */ |
| 29 | public function execute() { |
| 30 | try { |
| 31 | $message = []; |
| 32 | $message['function'] = $this->_function; |
| 33 | if($this->_data) $message['data'] = $this->_data; |
| 34 | |
| 35 | $client = new Client(); |
| 36 | $response = json_decode($client->send(json_encode($message)), true); |
| 37 | $client->close(); |
| 38 | if($response === false) throw new ClientException("Invalid response"); |
| 39 | return $response; |
| 40 | } catch(ClientException $e) { |
| 41 | throw new SocketAPIException($e->getMessage()); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @param string $key |
| 47 | * @param mixed $value |
| 48 | * |
| 49 | * @return $this |
| 50 | */ |
| 51 | public function arg($key, $value) { |
| 52 | $this->_data[$key] = $value; |
| 53 | return $this; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param int $limit |
| 58 | * @param int $skip |
| 59 | * |
| 60 | * @return $this |
| 61 | */ |
| 62 | public function limit($limit, $skip=0) { |
| 63 | $this->_data['limit'] = $limit; |
| 64 | $this->_data['skip'] = $skip; |
| 65 | return $this; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @param string $key |
| 70 | * |
| 71 | * @return $this |
| 72 | */ |
| 73 | public function sortAsc($key) { |
| 74 | $this->_data['sort'][$key] = 1; |
| 75 | return $this; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @param string $key |
| 80 | * |
| 81 | * @return $this |
| 82 | */ |
| 83 | public function sortDesc($key) { |
| 84 | $this->_data['sort'][$key] = -1; |
| 85 | return $this; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @return SocketAPI |
| 90 | * @throws SocketAPIException |
| 91 | */ |
| 92 | public static function api($function):SocketAPI { |
| 93 | if(!function_exists('socket_connect')) |
| 94 | throw new SocketAPIException("The function socket_connect not installed or disabled within your PHP."); |
| 95 | |
| 96 | return new SocketAPI($function); |
| 97 | } |
| 98 | } |
| 99 |