IPcalc-u-later/app/Controllers/Api/SubnetController.php

39 lines
No EOL
989 B
PHP

<?php
namespace App\Controllers\Api;
use App\Services\Subnet;
use Core\Controllers\Controller;
use Core\View\Render;
use Exception;
class SubnetController extends Controller
{
/**
* Get all subnet data
*
* @return \Core\View\Render
*/
public function data(): Render
{
// Get the user input
$subnet = $this->request->get('subnet', '');
try {
// Validate the input
if (!str_contains($subnet, '/')) {
throw new Exception('Invalid subnet format.');
}
// Delegate calculation to subnet service
$subnetInfo = Subnet::calculate($subnet);
return $this->response->json()
->with('result', $subnetInfo);
} catch (Exception $e) {
return $this->response->status(403)
->json()
->with('message', $e->getMessage());
}
}
}