39 lines
No EOL
1,004 B
PHP
39 lines
No EOL
1,004 B
PHP
<?php
|
|
|
|
namespace App\Controllers\Api;
|
|
|
|
use App\Services\Subnet;
|
|
use Core\Http\Controllers\Controller;
|
|
use Core\Http\View\Engine;
|
|
use Exception;
|
|
|
|
class SubnetController extends Controller
|
|
{
|
|
/**
|
|
* Get all subnet data
|
|
*
|
|
* @return \Core\Http\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());
|
|
}
|
|
}
|
|
} |