### Fetch GET Parameters with Validation (PHP) Source: https://context7.com/1312inc/webasyst-apipack/llms.txt Demonstrates fetching GET parameters using `ApiParamsFetcher::get()`. It shows how to retrieve optional or required parameters, cast them to specific types (string, integer), and handle missing parameters with `ApiMissingParamException`. It also illustrates trimming whitespace from string inputs. ```php get('search', false, ApiParamsCaster::CAST_STRING); // Returns: "hello" or null if not provided // Fetch required integer parameter try { $userId = $fetcher->get('user_id', true, ApiParamsCaster::CAST_INT); // Returns: 123 (as integer) } catch (ApiMissingParamException $e) { // HTTP 400: "Missing required param: 'user_id'" echo $e->getMessage(); } // Fetch with trimmed string $name = $fetcher->get('name', false, ApiParamsCaster::CAST_STRING_TRIM); // Input: " John Doe " => Returns: "John Doe" ``` -------------------------------- ### Type Casting with DateTime and Custom Formats (PHP) Source: https://context7.com/1312inc/webasyst-apipack/llms.txt Details the `ApiParamsCaster` class for type conversion, specifically focusing on datetime parsing using `DateTimeImmutable`. It shows how to fetch datetime parameters from GET or POST requests with specified formats and how `ApiCastParamException` is thrown for format mismatches. Direct usage of the caster for manual conversion is also demonstrated. ```php get('start_date', true, ApiParamsCaster::CAST_DATETIME, 'Y-m-d'); // Input: "2024-01-15" => Returns: DateTimeImmutable object $timestamp = $fetcher->post('created_at', false, ApiParamsCaster::CAST_DATETIME, 'Y-m-d H:i:s'); // Input: "2024-01-15 14:30:00" => Returns: DateTimeImmutable object } catch (ApiCastParamException $e) { // Thrown when date format doesn't match echo $e->getMessage(); // "Wrong format Y-m-d for value invalid_date" } // Direct caster usage $caster = new ApiParamsCaster(); $date = $caster->cast('2024-12-25', ApiParamsCaster::CAST_DATETIME, 'd/m/Y'); // Throws ApiCastParamException - format mismatch ``` -------------------------------- ### Handle API Exceptions with ApiPack1312 Source: https://context7.com/1312inc/webasyst-apipack/llms.txt Illustrates comprehensive exception handling for API parameter retrieval using ApiPack1312. It covers specific exceptions like ApiMissingParamException, ApiWrongParamException, and ApiCastParamException, as well as the base ApiException for general errors. This allows for granular error management and appropriate HTTP response codes. ```php get('user_id', true, ApiParamsCaster::CAST_INT); $status = $fetcher->post('status', true, ApiParamsCaster::CAST_ENUM, ['active', 'inactive']); $createdAt = $fetcher->post('created_at', false, ApiParamsCaster::CAST_DATETIME, 'Y-m-d'); } catch (ApiMissingParamException $e) { http_response_code($e->getCode()); // 400 echo json_encode([ 'error' => $e->getMessage(), 'description' => $e->getErrorDescription() ]); // {"error": "Missing required param: 'user_id'", "description": "Missing required param: 'user_id'"} } catch (ApiWrongParamException $e) { http_response_code($e->getCode()); // 400 echo json_encode([ 'error' => $e->getMessage(), 'description' => $e->getErrorDescription() ]); // {"error": "Wrong param: 'status'", "description": "Wrong param: 'status'. Invalid value"} } catch (ApiCastParamException $e) { http_response_code(400); echo json_encode(['error' => $e->getMessage()]); } catch (ApiException $e) { http_response_code($e->getCode()); // 500 for general API errors echo json_encode(['error' => $e->getMessage()]); } ?> ``` -------------------------------- ### Fetch POST/JSON Parameters with Type Casting (PHP) Source: https://context7.com/1312inc/webasyst-apipack/llms.txt Illustrates using `ApiParamsFetcher::post()` to retrieve parameters from POST requests or JSON bodies. It covers fetching required boolean, float, and array types, demonstrating automatic JSON parsing when the Content-Type is `application/json`. Error handling for incorrect parameter types is managed by `ApiWrongParamException`. ```php post('is_active', true, ApiParamsCaster::CAST_BOOLEAN); // "true", "1", "yes" => true; "false", "0", "no" => false // Fetch float value $price = $fetcher->post('price', true, ApiParamsCaster::CAST_FLOAT); // Input: "99.99" => Returns: 99.99 (as float) // Fetch array parameter $tags = $fetcher->post('tags', false, ApiParamsCaster::CAST_ARRAY); // Returns: ['tag1', 'tag2'] or casts single value to array // JSON request body example (Content-Type: application/json) // POST body: {"email": "user@example.com", "count": "5"} $email = $fetcher->post('email', true, ApiParamsCaster::CAST_STRING); $count = $fetcher->post('count', true, ApiParamsCaster::CAST_INT); // Returns: "user@example.com" and 5 ``` -------------------------------- ### Validate Enum Parameters with ApiParamsCaster Source: https://context7.com/1312inc/webasyst-apipack/llms.txt Demonstrates how to validate if a parameter value matches one of the allowed options using ApiParamsCaster::CAST_ENUM. The format parameter accepts an array of valid options. It shows both direct usage of the caster and integration with ApiParamsFetcher. ```php post('status', true, ApiParamsCaster::CAST_ENUM, $allowedStatuses); // Input: "approved" => Returns: "approved" // Input: "invalid" => Throws ApiCastParamException } catch (ApiCastParamException $e) { echo $e->getMessage(); // "Wrong value invalid. Expected one of pending, approved, rejected" } // Direct caster usage for enum validation $caster = new ApiParamsCaster(); $sortOrder = $caster->cast('asc', ApiParamsCaster::CAST_ENUM, ['asc', 'desc']); // Returns: "asc" $invalid = $caster->cast('random', ApiParamsCaster::CAST_ENUM, ['asc', 'desc']); // Throws: ApiCastParamException ?> ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.