Country-Click uses three PHP classes inDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/AndresLopezCorrales/Countries-WebPage/llms.txt
Use this file to discover all available pages before exploring further.
src/controller/ to separate concerns: ApiConsumer orchestrates the full request/response cycle, GetAttributesController extracts individual fields from the API JSON, and GetNameController handles input from the SVG map click. Each class is included with include at the point of use rather than via an autoloader, keeping the project dependency-free.
ApiConsumer
ApiConsumer is the main controller class. It is included and instantiated directly in index.php as $consumer = new ApiConsumer(). It is responsible for reading user input, building and executing the API request, assembling extracted data into an ordered array, and rendering the country info HTML panel to the page.
Methods
searchBar(): string
Checks whether the search form was submitted by testing for isset($_POST['submit']) and isset($_POST['search']). If both are present, returns the value of $_POST['search']. Otherwise returns an empty string. This method does not validate or sanitise the input beyond checking its existence.
getSearchBarInfo(): array|string
The core data-fetching method. It determines which country name to look up, performs the REST Countries API call, and assembles a flat array of all eight display fields. Returns an 8-element indexed array on success, or a generic error string if an exception is thrown.
The method follows this sequence:
- Calls
searchBar()to check for a typed search query. - Falls back to
GetNameController::clickCountries()if the search bar returned an empty string. - URL-encodes the country name and constructs the API URL.
- Calls
file_get_contents()withignore_errors: true. - Decodes the JSON response and checks for error fields.
- Calls each method on
GetAttributesControllerto extract individual fields. - Joins the languages array into a comma-separated string with
implode(). - Returns the assembled
$allAttributesarray.
$allAttributes array is ordered by index as follows:
ApiConsumer.php
menu(): void
Calls getSearchBarInfo() and checks whether the result is a non-empty array using is_array($searchInfo) && count($searchInfo) > 0. If the check passes, it unpacks the eight values by index, selects a regional continent image with a switch statement on the $region value, and outputs the complete country info HTML block directly with echo. If the check fails (no country was searched, or an error occurred), the method exits silently and nothing is rendered in the info panel.
GetAttributesController
GetAttributesController is a utility class with one method per data field. Each method accepts the full decoded API response array $data and uses isset() to guard against missing keys before returning the value. If the expected key is absent, the method returns an empty string instead of throwing an error. This makes the class safe to use even against partial or malformed API responses.
Methods
The full JSON-decoded API response array. All methods on this class expect the same argument — the complete
$data array returned by json_decode($response, true). Each method reads from $data[0], the first match in the API results.getCommonName(array $data): string
Returns $data[0]['name']['common'] — the country’s common English name. Returns an empty string if the key is not set.
getOfficialName(array $data): string
Returns $data[0]['name']['official'] — the country’s full official name. Returns an empty string if the key is not set.
getRegion(array $data): string
Returns $data[0]['region'] — the world region or continent. Returns an empty string if the key is not set.
getPopulation(array $data): int|string
Returns $data[0]['population'] — the total population as an integer. Returns an empty string if the key is not set.
getCapital(array $data): string
Returns $data[0]['capital'][0] — the first (primary) capital city. Returns an empty string if the key is not set.
getLanguage(array $data): array|string
Iterates over the $data[0]['languages'] object and collects each language name string into a $idiomas array, which it returns. Returns an empty string if the languages key is not set. The caller (ApiConsumer::getSearchBarInfo()) is responsible for joining the array into a display string with implode().
GetAttributesController.php
getTimezone(array $data): string
Returns $data[0]['timezones'][0] — the first (primary) timezone string, for example UTC+01:00. Returns an empty string if the key is not set.
getFlag(array $data): string
Returns $data[0]['flags']['png'] — the URL of the country’s PNG flag image. Returns an empty string if the key is not set.
GetNameController
GetNameController is a single-method class whose only responsibility is reading the country name that the JavaScript SVG map click handler posted to the server. It is included by ApiConsumer::getSearchBarInfo() via include before being instantiated.
Methods
clickCountries(): string
Checks isset($_POST['titulo']). If the field is present, returns its value — the xlink:title attribute from the clicked SVG <a class="country"> element, which is the country’s English name as it appears in the map markup. Returns an empty string if the field is not set (i.e., the page was loaded without a map click).
GetNameController.php
Why POST instead of GET
The JavaScript click handler ingetname.js dynamically creates a hidden <form> element, sets its method to POST, appends a hidden <input name="titulo"> field containing the country title, appends the form to the document body, and calls form.submit(). This is a reliable, cross-browser technique for triggering a full PHP page re-render with the selected country value. Using POST (rather than a URL query string) avoids exposing the country name as a GET parameter in the browser’s address bar and is consistent with how the search bar form also submits its data.