Skip to content

Commit 7cb03a4

Browse files
committed
Initial version of client
1 parent 1f02cbb commit 7cb03a4

File tree

6 files changed

+783
-3
lines changed

6 files changed

+783
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
co-reg-api
2-
==========
1+
DatingVIP API Client
2+
====================
33

4-
Co-Registration API (PHP)
4+
PHP implementation of API client to DatingVIP platform

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "datingvip/api",
3+
"type": "library",
4+
"description": "DatingVIP API Client",
5+
"keywords": ["datingvip", "api client"],
6+
"homepage": "https://github.com/DatingVIP/api-client",
7+
"license": "LGPLv3",
8+
"authors": [
9+
{
10+
"name": "Boris Momčilović",
11+
"email": "[email protected]",
12+
"homepage": "http://firstbeatmedia.com",
13+
"role": "Developer"
14+
}
15+
],
16+
"require": {
17+
"php": ">=5.4.0",
18+
"datingvip/curl": "dev-master"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"DatingVIP\\API\\": "src"
23+
}
24+
}
25+
}

composer.lock

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DatingVIP/API/Client.php

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
/**
3+
* API Client
4+
*
5+
* @package DatingVIP
6+
* @subpackage api
7+
* @category lib
8+
* @author Boris Momčilović <[email protected]>
9+
* @copyright All rights reserved
10+
* @version 1.0
11+
*/
12+
13+
namespace DatingVIP\API;
14+
15+
use DatingVIP\cURL\Request as cURL;
16+
use Exception;
17+
18+
class Client
19+
{
20+
const COMMAND = 'cmd';
21+
22+
/**
23+
* API URL
24+
*
25+
* @var string
26+
* @access protected
27+
*/
28+
protected $url;
29+
30+
/**
31+
* API authorization user
32+
*
33+
* @var string
34+
* @access protected
35+
*/
36+
protected $user;
37+
38+
/**
39+
* API authorization pass
40+
*
41+
* @var string $pass
42+
* @access protected
43+
*/
44+
protected $pass;
45+
46+
/**
47+
* Instance of DatingVIP\cURL\Request lib
48+
*
49+
* @var Request
50+
* @access private
51+
*/
52+
private $curl;
53+
54+
/**
55+
* Set API url
56+
*
57+
* @param string $url
58+
* @access public
59+
* @return bool
60+
*/
61+
public function setUrl($url)
62+
{
63+
$this->url = (string) $url;
64+
65+
return !empty ($this->url);
66+
}
67+
68+
/**
69+
* Set auth data for API
70+
*
71+
* @param string $user
72+
* @param string $pass
73+
* @access public
74+
* @return bool
75+
*/
76+
public function setAuth($user, $pass)
77+
{
78+
$this->user = (string) $user;
79+
$this->pass = (string) $pass;
80+
81+
return $this->hasAuth ();
82+
}
83+
84+
/**
85+
* Execute API command
86+
*
87+
* @param Command $command
88+
* @access public
89+
* @return Response
90+
* @throws \Exception
91+
*/
92+
public function execute(Command $command)
93+
{
94+
if (!$command->isValid ()) {
95+
throw new Exception ('Invalid API command supplied');
96+
}
97+
98+
$result = $this->curl()->post ($this->getUrl ($command), $command->getData ());
99+
100+
return new Response ($result->getData ());
101+
}
102+
103+
/**
104+
* Get browser for making API requests
105+
* - create an instance
106+
* - assign auth data if we have it
107+
*
108+
* @param void
109+
* @access private
110+
* @return cURL
111+
*/
112+
private function curl()
113+
{
114+
if (! ($this->curl instanceof cURL)) {
115+
$this->curl = new cURL ();
116+
}
117+
118+
if ($this->hasAuth ()) {
119+
$this->curl->setCredentials ($this->user, $this->pass);
120+
}
121+
122+
return $this->curl;
123+
}
124+
125+
/**
126+
* Get API URL for given command
127+
*
128+
* @param Command $command
129+
* @access private
130+
* @return string
131+
*/
132+
private function getUrl(Command $command)
133+
{
134+
return $this->url
135+
. (stripos ($this->url, '?') !== false ? '&' : '?')
136+
. http_build_query ([self::COMMAND => $command->getName ()]);
137+
}
138+
139+
/**
140+
* Check if API has auth data set
141+
* - checks if user and pass aren't empty
142+
*
143+
* @param void
144+
* @access private
145+
* @return bool
146+
*/
147+
private function hasAuth()
148+
{
149+
return !empty ($this->user) && !empty ($this->pass);
150+
}
151+
152+
}

src/DatingVIP/API/Command.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
/**
3+
* API Command
4+
*
5+
* @package DatingVIP
6+
* @subpackage api
7+
* @category lib
8+
* @author Boris Momčilović <[email protected]>
9+
* @copyright All rights reserved
10+
* @version 1.0
11+
*/
12+
13+
namespace DatingVIP\API;
14+
15+
class Command
16+
{
17+
const VAR_CONTROLLER = 'c';
18+
19+
/**
20+
* Command name
21+
*
22+
* @var string
23+
* @access private
24+
*/
25+
private $name;
26+
27+
/**
28+
* Command data
29+
*
30+
* @var array
31+
* @access private
32+
*/
33+
private $data;
34+
35+
/**
36+
* Default command constructor
37+
* - shorthand to set method
38+
*
39+
* @param string $name [= '']
40+
* @param array $data [= []]
41+
* @access public
42+
*/
43+
public function __construct($name = '', array $data = [])
44+
{
45+
$this->set ($name, $data);
46+
}
47+
48+
/**
49+
* Set command and optionally set data
50+
* - return if set command is valid
51+
*
52+
* @param string $name
53+
* @param array $data [= []]
54+
* @access public
55+
* @return bool
56+
*/
57+
public function set($name, array $data = [])
58+
{
59+
$this->setName ($name);
60+
$this->setData ($data);
61+
return $this->isValid ();
62+
}
63+
64+
/**
65+
* Get command name
66+
*
67+
* @param void
68+
* @access public
69+
* @return string
70+
*/
71+
public function getName()
72+
{
73+
return (string) $this->name;
74+
}
75+
76+
/**
77+
* Get command data
78+
*
79+
* @param void
80+
* @access public
81+
* @return array
82+
*/
83+
public function getData()
84+
{
85+
return (array) $this->data;
86+
}
87+
88+
/**
89+
* Check if set name is considered valid
90+
* - must not be empty
91+
* - must have a dot
92+
*
93+
* @param void
94+
* @access public
95+
* @return bool
96+
*/
97+
public function isValid()
98+
{
99+
return !empty ($this->name) && strpos ($this->name, '.') !== false;
100+
}
101+
102+
/**
103+
* Set command name
104+
* - return if set name is considered valid
105+
*
106+
* @param string $name
107+
* @access private
108+
* @return bool
109+
*/
110+
private function setName($name)
111+
{
112+
$this->name = (string) $name;
113+
return $this->isValid ();
114+
}
115+
116+
/**
117+
* Set command data
118+
*
119+
* @param array $data
120+
* @access private
121+
* @return bool
122+
*/
123+
private function setData(array $data)
124+
{
125+
$this->data = (array) $data;
126+
127+
// remove reserved stuff
128+
if (isset ($this->data[self::VAR_CONTROLLER])) { unset ($this->data[self::VAR_CONTROLLER]); }
129+
130+
return !empty ($this->data);
131+
}
132+
133+
}

0 commit comments

Comments
 (0)