Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions phptodo/TodoApp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
require_once __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/src/TodoController.php';
require __DIR__ . '/src/TodoRepository.php';
require __DIR__ . '/Util.php';

use Slim\Factory\AppFactory;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Tuupola\Middleware\CorsMiddleware;


class TodoApp
{
private $app;
private $util;

public function __construct()
{
$this->app = AppFactory::create();
$this->util = new Util();

$repository = new TodoRepository();
$todoController = new TodoController($repository);

$this->app->add(new CorsMiddleware(
[
"origin" => ["*"],
"methods" => ["GET", "POST", "PUT", "PATCH", "DELETE"],
"headers.allow" => ["Authorization", "Content-Type"],
"headers.expose" => [],
"credentials" => true,
"cache" => 0
]
));

$this->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
$this->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
$this->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
$this->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
$this->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;
});
}

public function run()
{
$this->app->run();
$this->util->logEvent();
}
}

$todoApp = new TodoApp();

// Run the application
$todoApp->run();
63 changes: 63 additions & 0 deletions phptodo/Util.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
//TODO: This file changes are in progress

require_once __DIR__ . '/vendor/autoload.php';

use Statsig\StatsigServer;
use Statsig\StatsigOptions;
use Statsig\Adapters\LocalFileDataAdapter;
use Statsig\Adapters\LocalFileLoggingAdapter;
use Statsig\StatsigUser;
use Statsig\StatsigEvent;

class Util
{
private $statsig;
private $user;

public function __construct()
{
$config_adapter = new LocalFileDataAdapter();
$logging_adapter = new LocalFileLoggingAdapter();
$options = new StatsigOptions($config_adapter, $logging_adapter);
$this->statsig = new StatsigServer("secret-9lbe9aax4FWPyJiLrKfo8GAj1cXX2UUqoDBcG4B7rKW", $options);
$this->user = StatsigUser::withUserID("php_user");
$this->user->setEmail("[email protected]");
$this->user->setCountry("IN");
}

public function run()
{
$config = $this->statsig->getConfig($this->user, "warning_banner");
print_r($config);
$jsonString = json_encode($config);
print_r($jsonString);
}

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();
}
}

?>
19 changes: 19 additions & 0 deletions phptodo/composer.json
Original file line number Diff line number Diff line change
@@ -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/"
}
}
}
144 changes: 144 additions & 0 deletions phptodo/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#### TODO project in PHP

##### PHP version

```PHP 8.2.12 (cli) (```

##### Run the project
Navigate till TodoApp.php file in project and execute command

```php -S localhost:8080 .\TodoApp.php ```

### API Structure

The Todo App CRUD API is a set of endpoints that allow users to perform CRUD (Create, Read, Update, Delete) operations on todo items in the Todo App.

The API includes the following endpoints:

- `GET /todos`: Retrieves a list of all todo items.
- `GET /todos/{id}`: Retrieves a specific todo item by its ID.
- `POST /todos`: Creates a new todo item.
- `PUT /todos`: Updates an existing todo item.
- `DELETE /todos/{id}`: Deletes a todo item.

Each endpoint accepts and returns JSON data.

Sample CURL requests with responses for the Todo App CRUD API:

1. Retrieve a list of all todo items:

```bash
curl -s http://localhost:8080/todos
```

Response:

```json
[
{
"id": 1,
"task": "task1",
"description": "",
"completed": false,
"edited": false,
"lastViewed": false,
"serialNumber": 1,
"createdDate": "2023-10-18T07:12:31.830Z",
"modifiedDate": "2023-10-18T07:12:31.830Z"
},
{
"id": 2,
"task": "task2",
"description": "",
"completed": false,
"edited": false,
"lastViewed": false,
"serialNumber": 2,
"createdDate": "2023-10-18T07:12:34.746Z",
"modifiedDate": "2023-10-18T07:12:34.746Z"
},
....
]
```

2. Retrieve a specific todo item by its ID:

```bash
curl -s http://localhost:8080/todos/{id}
```

Response:

```json
{
"id": 1,
"task": "task1",
"description": "",
"completed": false,
"edited": false,
"lastViewed": false,
"serialNumber": 1,
"createdDate": "2023-10-18T07:12:31.830Z",
"modifiedDate": "2023-10-18T07:12:31.830Z"
}
```

3. Create a new todo item:

```bash
curl -s POST -H "Content-Type: application/json" -d '{"serialNumber": 10,"task": "task9","completed": false,"description": "","edited": false,"createdDate": "2023-10-06T10:41:11.806Z","modifiedDate": "2023-10-06T10:41:11.806Z","lastViewed": false}' http://localhost:8080/todos
```

Response:

```json
{
"id": 7,
"task": "task9",
"description": "",
"completed": 0,
"edited": 0,
"serialNumber": 10,
"lastViewed": 0,
"createdDate": "2023-10-06T10:41:11.806Z",
"modifiedDate": "2023-10-06T10:41:11.806Z"
}
```

4. Update an existing todo item:

```bash
curl -s PUT -H "Content-Type: application/json" -d '{"serialNumber": 10,"task": "task9","completed": false,"description": "","edited": true,"createdDate": "2023-10-06T10:41:11.806Z","modifiedDate": "2023-10-06T10:41:11.806Z","lastViewed": true, "id":7}' http://localhost:8080/todos
```

Response:

```json
{
"serialNumber": 10,
"task": "task9",
"completed": false,
"description": "",
"edited": true,
"createdDate": "2023-10-06T10:41:11.806Z",
"modifiedDate": "2023-10-06T10:41:11.806Z",
"lastViewed": true,
"id": 7
}
```

5. Delete a todo item:

```bash
curl -X DELETE http://localhost:8080/todos/{id}
```

Response:

```json
{
"id": "7"
}
```


Loading