1+ <?php
2+ class Zftest_Service_Joindin
3+ {
4+ const JOINDIN_API_BASE = 'http://joind.in/api ' ;
5+ const JOINDIN_DEF_TYPE = 'xml ' ;
6+ const JOINDIN_OUT_XML = 'xml ' ;
7+ const JOINDIN_OUT_JSON = 'json ' ;
8+
9+ /**
10+ * @var Zend_Http_Client A client to connect with Joind.in
11+ */
12+ protected $ _client ;
13+ /**
14+ * @var string The username to connect to Joind.in
15+ */
16+ protected $ _username ;
17+ /**
18+ * @var string The password to connect to Joind.in
19+ */
20+ protected $ _password ;
21+ /**
22+ * @var string The output type of request
23+ */
24+ protected $ _output ;
25+ /**
26+ * @var SimpleXMLElement
27+ */
28+ protected $ _message ;
29+ /**
30+ * @var Zftest_Service_Joindin_Site
31+ */
32+ protected $ _site ;
33+ /**
34+ * @var Zftest_Service_Joindin_Event
35+ */
36+ protected $ _event ;
37+ /**
38+ * @var Zftest_Service_Joindin_Talk
39+ */
40+ protected $ _talk ;
41+ /**
42+ * @var Zftest_Service_Joindin_User
43+ */
44+ protected $ _user ;
45+ /**
46+ * @var Zftest_Service_Joindin_Comment
47+ */
48+ protected $ _comment ;
49+ /**
50+ * Constructor for this joindin class
51+ *
52+ * @param null|string $username
53+ * @param null|string $password
54+ * @param string $output
55+ */
56+ public function __construct ($ username = null , $ password = null , $ output = self ::JOINDIN_DEF_TYPE )
57+ {
58+ if (null !== $ username ) {
59+ $ this ->setUsername ($ username );
60+ }
61+ if (null !== $ password ) {
62+ $ this ->setPassword ($ password );
63+ }
64+ $ this ->setOutput ($ output );
65+ $ this ->setMessage (new SimpleXMLElement ('<request></request> ' ));
66+ $ this ->_site = new Zftest_Service_Joindin_Site ();
67+ $ this ->_event = new Zftest_Service_Joindin_Event ();
68+ $ this ->_talk = new Zftest_Service_Joindin_Talk ();
69+ $ this ->_user = new Zftest_Service_Joindin_User ();
70+ $ this ->_comment = new Zftest_Service_Joindin_Comment ();
71+ }
72+ /**
73+ * Sets the HTTP client
74+ *
75+ * @param Zend_Http_Client $client
76+ * @return Zftest_Service_Joindin
77+ */
78+ public function setClient (Zend_Http_Client $ client )
79+ {
80+ $ this ->_client = $ client ;
81+ return $ this ;
82+ }
83+ /**
84+ * Retrieves the HTTP client
85+ *
86+ * @return Zend_Http_Client
87+ */
88+ public function getClient ()
89+ {
90+ if (null === $ this ->_client ) {
91+ $ this ->_client = new Zend_Http_Client ();
92+ }
93+ return $ this ->_client ;
94+ }
95+ /**
96+ * Sets the username for this joindin
97+ *
98+ * @param string $username
99+ * @return Zftest_Service_Joindin
100+ */
101+ public function setUsername ($ username )
102+ {
103+ $ this ->_username = (string ) $ username ;
104+ return $ this ;
105+ }
106+ /**
107+ * Retrieves the username from this joindin class
108+ *
109+ * @return string
110+ */
111+ public function getUsername ()
112+ {
113+ if (null === $ this ->_username ) {
114+ throw new Zftest_Service_Joindin_Exception ('Username is not set ' );
115+ }
116+ return $ this ->_username ;
117+ }
118+ /**
119+ * Sets the password for this joindin account
120+ *
121+ * @param string $password
122+ * @return Zftest_Service_Joindin
123+ */
124+ public function setPassword ($ password )
125+ {
126+ $ this ->_password = (string ) $ password ;
127+ return $ this ;
128+ }
129+ /**
130+ * Retrieves the password from this joindin account
131+ *
132+ * @return string
133+ */
134+ public function getPassword ()
135+ {
136+ return $ this ->_password ;
137+ }
138+ public function setOutput ($ output )
139+ {
140+ $ types = array ('json ' , 'xml ' );
141+ if (!in_array ($ output , $ types )) {
142+ $ output = self ::JOINDIN_DEF_TYPE ;
143+ }
144+ $ this ->_output = (string ) $ output ;
145+ return $ this ;
146+ }
147+ public function getOutput ()
148+ {
149+ return $ this ->_output ;
150+ }
151+ public function setMessage (SimpleXmlElement $ message )
152+ {
153+ $ this ->_message = $ message ;
154+ }
155+ public function getMessage ()
156+ {
157+ if (null === $ this ->_message ) {
158+ throw new Zftest_Service_Joindin_Exception ('Request message is not set ' );
159+ }
160+ return $ this ->_message ;
161+ }
162+ public function user ()
163+ {
164+ $ this ->_user ->setJoindin ($ this );
165+ return $ this ->_user ;
166+ }
167+ public function site ()
168+ {
169+ $ this ->_site ->setJoindin ($ this );
170+ return $ this ->_site ;
171+ }
172+ public function connect (SimpleXMLElement $ message , $ apiEnd = null )
173+ {
174+ $ this ->getClient ()
175+ ->setUri (self ::JOINDIN_API_BASE . $ apiEnd )
176+ ->setMethod (Zend_Http_Client::POST );
177+ if (self ::JOINDIN_OUT_XML === $ this ->getOutput ()) {
178+ $ this ->getClient ()
179+ ->setHeaders ('Content-Type ' , 'text/xml ' )
180+ ->setRawData ($ message ->asXML ());
181+ }
182+ $ request = $ this ->getClient ()->request ();
183+ return $ this ->getClient ()->getLastResponse ();
184+ }
185+ }
0 commit comments