protected function PUT($action = '', $data = '') { if (!in_array($action, ['entity', 'entities', 'uid'])) { return $this->httpError(400, 'Bad Request'); } ob_start(); if ($action === 'uid') { $args = json_decode($data, true); if (!isset($args['name']) || !isset($args['value']) || !is_string($args['name']) || !is_numeric($args['value'])) { return $this->httpError(400, 'Bad Request'); } try { $result = Nymph::setUID($args['name'], (int) $args['value']); } catch (\Exception $e) { return $this->httpError(500, 'Internal Server Error', $e); } if (!$result) { return $this->httpError(500, 'Internal Server Error'); } header('Content-Type: text/plain'); echo json_encode($result); } else { $ents = json_decode($data, true); if ($action === 'entity') { $ents = [$ents]; } $saved = []; $invalidData = false; $notfound = false; $lastException = null; foreach ($ents as $newEnt) { if (!is_numeric($newEnt['guid']) || (int) $newEnt['guid'] <= 0) { $invalidData = true; continue; } $entity = $this->loadEntity($newEnt); if (!$entity) { $invalidData = true; continue; } try { if ($entity->save()) { $saved[] = $entity; } } catch (Exceptions\EntityInvalidDataException $e) { $invalidData = true; } catch (\Exception $e) { $lastException = $e; } } if (empty($saved)) { if ($invalidData) { return $this->httpError(400, 'Bad Request'); } elseif ($notfound) { return $this->httpError(404, 'Not Found'); } else { return $this->httpError(500, 'Internal Server Error', $lastException); } } header('Content-Type: application/json'); if ($action === 'entity') { echo json_encode($saved[0]); } else { echo json_encode($saved); } } header('HTTP/1.1 200 OK', true, 200); ob_end_flush(); return true; }
/** * @depends testRenameUID */ public function testSetUID() { $this->assertTrue(Nymph::setUID('TestUID', 5)); $this->assertEquals(5, Nymph::getUID('TestUID')); }