diff --git a/phptodo/app.php b/phptodo/app.php new file mode 100644 index 0000000..1c177e4 --- /dev/null +++ b/phptodo/app.php @@ -0,0 +1,71 @@ +add(new CorsMiddleware( + [ + "origin" => ["*"], + "methods" => ["GET", "POST", "PUT", "PATCH", "DELETE"], + "headers.allow" => ["Authorization", "Content-Type"], + "headers.expose" => [], + "credentials" => true, + "cache" => 0 + ] +)); + +$app->get('/todos', function (Request $request, Response $response, $args) use ($todoController) { + + $todos = $todoController->getAllTodos(); + $response->getBody()->write(json_encode($todos)); + $response = $response->withStatus(200, 'OK'); + return $response; +}); + +// Map the GET /todos/{id} endpoint to the getTodoById method +$app->get('/todos/{id}', function (Request $request, Response $response, $args) use ($todoController) { + $id = $args['id']; + $todo = $todoController->getTodoById($id); + $response->getBody()->write(json_encode($todo)); + $response = $response->withStatus(200, 'OK'); + return $response; +}); + +// Map the POST /todos endpoint to the createTodo method +$app->post('/todos', function (Request $request, Response $response, $args) use ($todoController) { + + $data = $request->getBody(); + $todo = $todoController->createTodo($data); + $response->getBody()->write(json_encode($todo)); + $response = $response->withStatus(200, 'OK'); + return $response->withHeader('Content-Type', 'application/json'); +}); + +// Map the PUT /todos/{id} endpoint to the updateTodoById method +$app->put('/todos', function (Request $request, Response $response, $args) use ($todoController) { + $data = $request->getBody(); + $todo = $todoController->updateTodoById($data); + $response->getBody()->write(json_encode($todo)); + $response = $response->withStatus(201, 'OK'); + return $response; +}); + +// Map the DELETE /todos/{id} endpoint to the deleteTodoById method +$app->delete('/todos/{id}', function (Request $request, Response $response, $args) use ($todoController) { + $id = $args['id']; + $todoController->deleteTodoById($id); + $response->getBody()->write(json_encode(['id' => $id])); + $response = $response->withStatus(200, 'OK'); + return $response; +}); + +$app->run(); diff --git a/phptodo/composer.json b/phptodo/composer.json new file mode 100644 index 0000000..cea1757 --- /dev/null +++ b/phptodo/composer.json @@ -0,0 +1,19 @@ +{ + "name": "statsig/phptodo", + "description": "A PHP Todo project", + "type": "project", + "require": { + "statsig/statsigsdk": "^2.2", + "doctrine/dbal": "^3.7", + "slim/slim": "4.*", + "tuupola/cors-middleware": "*", + "psr/http-message": "^1.0", + "selective/basepath": "*" + + }, + "autoload": { + "psr-4": { + "Statsig\\Phptodo\\": "src/" + } + } +} diff --git a/phptodo/main.php b/phptodo/main.php new file mode 100644 index 0000000..e69de29 diff --git a/phptodo/src/TodoController.php b/phptodo/src/TodoController.php new file mode 100644 index 0000000..913a2c9 --- /dev/null +++ b/phptodo/src/TodoController.php @@ -0,0 +1,82 @@ +repository = $repository; + } + + public function getAllTodos() + { + $todos = $this->repository->getAllTodos(); + return $todos; + } + + public function getTodoById($id) + { + $todo = $this->repository->getTodoById($id); + if ($todo) { + return $todo; + } + return $this->notFound(); + } + + public function createTodo() + { + $input = $this->getJsonInput(); + $todo = $this->repository->createTodo( + $input['task'] ?? '', + $input['description'] ?? '', + $input['completed'] ?? false, + $input['edited'] ?? false, + $input['serialNumber'] ?? 0, + $input['lastViewed'] ?? '', + $input['createdDate'] ?? '', + $input['modifiedDate'] ?? '' + ); + return $todo; + } + + public function updateTodoById() + { + $input = $this->getJsonInput(); + $todo = $this->repository->updateTodo( + $input['id'] ?? 0, + $input['task'] ?? '', + $input['description'] ?? '', + $input['completed'] ?? false, + $input['edited'] ?? false, + $input['serialNumber'] ?? 0, + $input['lastViewed'] ?? '', + $input['createdDate'] ?? '', + $input['modifiedDate'] ?? '' + ); + return $todo; + } + + public function deleteTodoById($id) + { + $this->repository->deleteTodo($id); + return ['id' => $id]; + } + + public function sendJsonResponse($data) + { + header('Content-Type: application/json'); + return json_encode($data); + } + + public function getJsonInput() + { + $input = file_get_contents('php://input'); + return json_decode($input, true) ?? []; + } + + public function notFound() + { + header('HTTP/1.1 404 Not Found'); + echo '404 Not Found'; + } +} diff --git a/phptodo/src/todo.php b/phptodo/src/todo.php new file mode 100644 index 0000000..9ce390a --- /dev/null +++ b/phptodo/src/todo.php @@ -0,0 +1,26 @@ +id = $id; + $this->task = $task; + $this->description = $description; + $this->completed = $completed; + $this->edited = $edited; + $this->serialNumber = $serialNumber; + $this->lastViewed = $lastViewed; + $this->createdDate = $createdDate; + $this->modifiedDate = $modifiedDate; + } +} diff --git a/phptodo/src/todorepository.php b/phptodo/src/todorepository.php new file mode 100644 index 0000000..ac9ce86 --- /dev/null +++ b/phptodo/src/todorepository.php @@ -0,0 +1,87 @@ +filePath)) { + touch($this->filePath); + } + } + + public function getAllTodos() + { + $todos = json_decode(file_get_contents($this->filePath), true); + return $todos; + } + + public function getTodoById($id) + { + $todos = json_decode(file_get_contents($this->filePath), true); + foreach ($todos as $todo) { + if ($todo['id'] == $id) { + return $todo; + } + } + return null; + } + + public function createTodo($task, $description, $completed, $edited, $serialNumber, $lastViewed, $createdDate, $modifiedDate) + { + $todos = json_decode(file_get_contents($this->filePath), true); + $count = count($todos ?? []); + + $newTodo = [ + 'id' => $count + 1, + 'task' => $task, + 'description' => $description, + 'completed' => $completed, + 'edited' => $edited, + 'serialNumber' => $serialNumber, + 'lastViewed' => $lastViewed, + 'createdDate' => $createdDate, + 'modifiedDate' => $modifiedDate + ]; + $todos[] = $newTodo; + file_put_contents($this->filePath, json_encode($todos)); + return $newTodo; + } + + public function updateTodo($id, $task, $description, $completed, $edited, $serialNumber, $lastViewed, $createdDate, $modifiedDate) + { + $todos = json_decode(file_get_contents($this->filePath), true); + $tempTodo = null; + error_log($id); + foreach ($todos as &$todo) { + if ($todo['id'] == $id) { + $todo['task'] = $task; + $todo['description'] = $description; + $todo['completed'] = $completed; + $todo['edited'] = $edited; + $todo['serialNumber'] = $serialNumber; + $todo['lastViewed'] = $lastViewed; + $todo['createdDate'] = $createdDate; + $todo['modifiedDate'] = $modifiedDate; + $tempTodo = $todo; + break; + } + } + file_put_contents($this->filePath, json_encode($todos)); + return $tempTodo; + } + + public function deleteTodo($id) + { + $todos = json_decode(file_get_contents($this->filePath), true); + foreach ($todos as $key => $todo) { + if ($todo['id'] == $id) { + unset($todos[$key]); + break; + } + } + file_put_contents($this->filePath, json_encode(array_values($todos))); + } +} diff --git a/phptodo/src/todos.json b/phptodo/src/todos.json new file mode 100644 index 0000000..e69de29 diff --git a/phptodo/src/util.php b/phptodo/src/util.php new file mode 100644 index 0000000..c762271 --- /dev/null +++ b/phptodo/src/util.php @@ -0,0 +1,83 @@ +statsig = new StatsigServer("secret-9lbe9aax4FWPyJiLrKfo8GAj1cXX2UUqoDBcG4B7rKW", $options); + $this->user = StatsigUser::withUserID("php_user"); + $this->user->setEmail("user_php@statsig.com"); + $this->user->setCountry("IN"); + } + + public function run() + { + // Main functionality of the application + $config = $this->statsig->getConfig($this->user, "warning_banner"); + // error_log("The value of variable is: " . $config); + print_r($config); + $jsonString = json_encode($config); + print_r($jsonString); + + $repository = new TodoRepository(); + $this->todo_controller = new TodoController($repository); + // $this->todo_controller->handleRequest(); + + } + + public function getExperiment() + { + $todo_experiment = $this->statsig->getExperiment($this->user, "item_sorting"); + print_r($todo_experiment); + print_r($todo_experiment->getName()); + print_r($todo_experiment->getValue()); + + $jsonString = json_encode($todo_experiment); + print_r($jsonString); + } + + public function logEvent() + { + $event = new StatsigEvent("TODO_CREATE"); + $event->setUser($this->user); + $event->setValue("TODO"); + $event->setMetadata(array("title" => "TODO_1")); + $this->statsig->logEvent($event); + } + + public function flush() + { + $this->statsig->flush(); + } +} + +$main = new Main(); +$main->run(); +$main->getExperiment(); +$main->logEvent(); +$main->flush(); + +phpinfo() +?> \ No newline at end of file diff --git a/phptodo/todos.json b/phptodo/todos.json new file mode 100644 index 0000000..8dc3c09 --- /dev/null +++ b/phptodo/todos.json @@ -0,0 +1 @@ +[{"id":1,"task":"task2_Edited_2","description":"","completed":true,"edited":true,"serialNumber":2,"lastViewed":true,"createdDate":"2023-10-10T15:05:15.486Z","modifiedDate":"2023-10-10T20:36:13.596Z"},{"id":2,"task":"task9","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":4,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":5,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":6,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":7,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":8,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":9,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":10,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":11,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":12,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":13,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":14,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":15,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":16,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":17,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":18,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":19,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":20,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":21,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":22,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":23,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":24,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":25,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":26,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"},{"id":27,"task":"task3","description":"","completed":true,"edited":false,"serialNumber":10,"lastViewed":false,"createdDate":"2023-10-06T10:41:11.806Z","modifiedDate":"2023-10-06T10:41:11.806Z"}] \ No newline at end of file