@@ -27,8 +27,8 @@ public function __construct($access_token, $isAsyncRequest = false, $httpClient
2727 $ this ->access_token = $ access_token ;
2828 $ this ->isAsyncRequest = $ isAsyncRequest ;
2929 $ this ->client = $ httpClient ?: new Client ([
30- 'base_uri ' => self ::WIT_API_BASE_URI ,
31- 'timeout ' => self ::DEFAULT_TIMEOUT ,
30+ 'base_uri ' => self ::WIT_API_BASE_URI ,
31+ 'timeout ' => self ::DEFAULT_TIMEOUT ,
3232 'connect_timeout ' => self ::DEFAULT_TIMEOUT ,
3333 ]);
3434 }
@@ -69,10 +69,72 @@ public function getIntentByText($q, $params = [])
6969 return $ this ->makeRequest ('GET ' , self ::TEXT_INTENT_API , $ query );
7070 }
7171
72- protected function makeRequest ($ method , $ uri , $ query = [])
72+ //Retrieve the list of all available entities
73+ public function getEntities ()
74+ {
75+ return $ this ->makeRequest ('GET ' , 'entities ' );
76+ }
77+
78+ //Create a new entity
79+ public function createEntities ($ id , $ doc )
80+ {
81+ $ data = [
82+ 'id ' => $ id ,
83+ 'doc ' => $ doc ,
84+ ];
85+ return $ this ->makeRequest ('POST ' , 'entities ' , [], $ data );
86+ }
87+
88+ //Retrieve all values of an entity
89+ public function getEntity ($ id )
90+ {
91+ return $ this ->makeRequest ('GET ' , 'entities/ ' . $ id );
92+ }
93+
94+ //Update the information of an entity
95+ public function updateEntity ($ id , $ data )
96+ {
97+ return $ this ->makeRequest ('PUT ' , 'entities/ ' . $ id , [], $ data );
98+ }
99+
100+ //Add new values to a keyword entity
101+ public function addNewValueToEntity ($ id , $ value )
102+ {
103+ return $ this ->makeRequest ('POST ' , 'entities/ ' . $ id . '/values ' , [], $ value );
104+ }
105+
106+ //Remove a given value from an entity
107+ public function removeValueFromEntity ($ id , $ value )
108+ {
109+ return $ this ->makeRequest ('DELETE ' , 'entities/ ' . $ id . '/values/ ' . $ value );
110+ }
111+
112+ //Create a new expression for a keyword entity - POST https://api.wit.ai/entities/$ENTITY_ID/values/$ENTITY_VALUE/expressions
113+ public function createExpressionToEntity ($ id , $ value , $ expression )
114+ {
115+ $ data = ['expression ' => $ expression ];
116+ return $ this ->makeRequest ('POST ' , 'entities/ ' . $ id . '/values/ ' . $ value . '/expressions ' , [], $ data );
117+ }
118+
119+ //Remove an expression from an entity
120+ public function removeExpressionFromEntity ($ id , $ value , $ expression )
121+ {
122+ return $ this ->makeRequest ('DELETE ' , 'entities/ ' . $ id . '/values/ ' . $ value . '/expressions/ ' . $ expression );
123+ }
124+
125+ //Train the app
126+ public function train ($ samples )
127+ {
128+ return $ this ->makeRequest ('POST ' , 'samples ' , [], $ samples );
129+ }
130+
131+ protected function makeRequest ($ method , $ uri , $ query = [], $ data = [])
73132 {
74133 $ options [GuzzleRequestOptions::QUERY ] = $ query ;
75134 $ options [GuzzleRequestOptions::HEADERS ] = $ this ->getDefaultHeaders ();
135+ if (count ($ data ) > 0 ) {
136+ $ options [GuzzleRequestOptions::JSON ] = $ data ;
137+ }
76138 if ($ this ->isAsyncRequest ) {
77139 return $ this ->promises [] = $ this ->client ->requestAsync ($ method , $ uri , $ options );
78140 }
@@ -84,9 +146,9 @@ protected function makeRequest($method, $uri, $query = [])
84146 protected function getDefaultHeaders ()
85147 {
86148 return array_merge ([
87- 'User-Agent ' => 'wit- ' . self ::VERSION ,
88- 'Authorization ' => 'Bearer ' . $ this ->access_token ,
89- 'Accept ' => 'application/vnd.wit. ' . self ::WIT_API_VERSION . '+json ' ,
149+ 'User-Agent ' => 'wit- ' . self ::VERSION ,
150+ 'Authorization ' => 'Bearer ' . $ this ->access_token ,
151+ 'Accept ' => 'application/vnd.wit. ' . self ::WIT_API_VERSION . '+json ' ,
90152 ], $ this ->headers );
91153 }
92154
0 commit comments