Exemplo n.º 1
0
 protected function POST($action = '', $data = '')
 {
     if (!in_array($action, ['entity', 'entities', 'uid', 'method'])) {
         return $this->httpError(400, 'Bad Request');
     }
     ob_start();
     if (in_array($action, ['entity', 'entities'])) {
         $ents = json_decode($data, true);
         if ($action === 'entity') {
             $ents = [$ents];
         }
         $created = [];
         $invalidData = false;
         foreach ($ents as $newEnt) {
             if ((int) $newEnt['guid'] > 0) {
                 $invalidData = true;
                 continue;
             }
             $entity = $this->loadEntity($newEnt);
             if (!$entity) {
                 $invalidData = true;
                 continue;
             }
             try {
                 if ($entity->save()) {
                     $created[] = $entity;
                 }
             } catch (Exceptions\EntityInvalidDataException $e) {
                 $invalidData = true;
             } catch (\Exception $e) {
                 return $this->httpError(500, 'Internal Server Error', $e);
             }
         }
         if (empty($created)) {
             if ($invalidData) {
                 return $this->httpError(400, 'Bad Request');
             } else {
                 return $this->httpError(500, 'Internal Server Error');
             }
         }
         header('HTTP/1.1 201 Created', true, 201);
         header('Content-Type: application/json');
         if ($action === 'entity') {
             echo json_encode($created[0]);
         } else {
             echo json_encode($created);
         }
     } elseif ($action === 'method') {
         $args = json_decode($data, true);
         array_walk($args['params'], [$this, 'referenceToEntity']);
         if (isset($args['static']) && $args['static']) {
             $class = $args['class'];
             if (!class_exists($class) || !isset($class::$clientEnabledStaticMethods)) {
                 return $this->httpError(400, 'Bad Request');
             }
             if (!in_array($args['method'], $class::$clientEnabledStaticMethods)) {
                 return $this->httpError(403, 'Forbidden');
             }
             try {
                 $return = call_user_func_array([$class, $args['method']], $args['params']);
                 header('Content-Type: application/json');
                 echo json_encode(['return' => $return]);
             } catch (\Exception $e) {
                 return $this->httpError(500, 'Internal Server Error', $e);
             }
         } else {
             $entity = $this->loadEntity($args['entity']);
             if (!in_array($args['method'], $entity->clientEnabledMethods())) {
                 return $this->httpError(403, 'Forbidden');
             }
             if (!$entity || (int) $args['entity']['guid'] > 0 && !$entity->guid || !is_callable([$entity, $args['method']])) {
                 return $this->httpError(400, 'Bad Request');
             }
             try {
                 $return = call_user_func_array([$entity, $args['method']], $args['params']);
                 header('Content-Type: application/json');
                 echo json_encode(['entity' => $entity, 'return' => $return]);
             } catch (\Exception $e) {
                 return $this->httpError(500, 'Internal Server Error', $e);
             }
         }
         header('HTTP/1.1 200 OK', true, 200);
     } else {
         try {
             $result = Nymph::newUID("{$data}");
         } catch (\Exception $e) {
             return $this->httpError(500, 'Internal Server Error', $e);
         }
         if (!is_int($result)) {
             return $this->httpError(500, 'Internal Server Error');
         }
         header('HTTP/1.1 201 Created', true, 201);
         header('Content-Type: text/plain');
         echo $result;
     }
     ob_end_flush();
     return true;
 }
Exemplo n.º 2
0
 /**
  * @depends testNewUID
  */
 public function testIncrementUID()
 {
     $this->assertEquals(2, Nymph::newUID('TestUID'));
 }