Files
citrus-cms/app/Docs/Strategies/FixRequestBodyStrategy.php
T
2026-04-28 21:14:25 +03:00

57 lines
1.7 KiB
PHP

<?php
namespace App\Docs\Strategies;
use Knuckles\Scribe\Extracting\Strategies\Strategy;
use Knuckles\Camel\Extraction\ExtractedEndpointData;
use Illuminate\Support\Str;
/**
* Custom Scribe Strategy to fix request body for GET/HEAD methods.
*
* This strategy ensures that request body is only included for
* POST, PUT, PATCH, DELETE methods, not for GET/HEAD.
*/
class FixRequestBodyStrategy extends Strategy
{
/**
* The stage this strategy belongs to.
*/
public string $stage = 'bodyParameters';
/**
* Trait this strategy applies to.
*/
public static function routableIf(ExtractedEndpointData $endpoint): bool
{
// Only apply to the dynamic endpoint route
return Str::contains($endpoint->route->uri, '{table}/{action}');
}
/**
* @param ExtractedEndpointData $endpointData
* @param array $routeRules
* @return array|null
*/
public function __invoke(ExtractedEndpointData $endpointData, array $routeRules = []): ?array
{
// Get HTTP methods for this endpoint
$httpMethods = $endpointData->httpMethods ?? [];
// Check if the first method (primary method) is GET or HEAD
// Scribe creates separate endpoints for each HTTP method
if (count($httpMethods) > 0) {
$primaryMethod = strtoupper($httpMethods[0]);
// If primary method is GET or HEAD, remove body parameters
if (in_array($primaryMethod, ['GET', 'HEAD']) && count($endpointData->bodyParameters) > 0) {
// Return empty array to remove body parameters
return [];
}
}
// Otherwise, return null to keep existing body parameters
return null;
}
}