示例#1
0
 public function post($resource, $request, $response)
 {
     $DOName = $this->getDOName($resource);
     if (empty($DOName)) {
         $response->status(404);
         $response->body("Resource '{$resource}' is not found.");
         return;
     }
     $dataObj = Openbiz::getObject($DOName);
     $dataRec = new DataRecord(null, $dataObj);
     $inputRecord = json_decode($request->getBody());
     foreach ($inputRecord as $k => $v) {
         $dataRec[$k] = $v;
         // or $dataRec->$k = $v;
     }
     try {
         $dataRec->save();
     } catch (Openbiz\Validation\Exception $e) {
         $response->status(400);
         $errmsg = implode("\n", $e->errors);
         $response->body($errmsg);
         return;
     } catch (Openbiz\data\Exception $e) {
         $response->status(400);
         $response->body($e->getMessage());
         return;
     }
     $format = strtolower($request->params('format'));
     $response->status(200);
     if ($format == 'json') {
         $response['Content-Type'] = 'application/json';
         $response->body(json_encode($dataRec->toArray()));
     } else {
         $response['Content-Type'] = "text/xml; charset=utf-8";
         $xml = new array2xml('Data');
         $xml->createNode($dataRec->toArray());
         $response->body($xml);
     }
     return;
 }