# Responses

Every method returns a read-only object built from the API's JSON. Fields are properties in camelCase, and the same object answers to the API's own field names through array access.

fields.phpphp

```php
$response = $client->resolveAddress('paf_2670849');
$address = $response->result;

echo $address->line1, PHP_EOL;      // property
echo $address['line_1'], PHP_EOL;   // the API's field name, same value
```

## Raw array

Call `toArray()` on any response or nested object. It returns the decoded body with the API's field names, so it round-trips through `json_encode()`. Every object is also `JsonSerializable`.

array.phpphp

```php
$data = $response->toArray();
echo json_encode($response, JSON_PRETTY_PRINT);
```

## Types

| Namespace                  | Holds                                                                                                                            |
| -------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| `IdealPostcodes\Responses` | One class per operation response, such as `PostcodeResponse` and `AutocompleteResponse`. Each has `code`, `message` and `result` |
| `IdealPostcodes\Types`     | Everything inside a result: `Address`, `AddressListItem`, `UkAddressSuggestion`, `Licensee`, `Config` and the native records     |

Every class carries a PHPStan array shape for its `toArray()` form, and `IdealPostcodes\Params` holds a shape for each method's `$params` and `$body`. PHPStan and IDEs that read array shapes check the keys you pass.

## Fields with more than one shape

Some results come in one of several forms, and the SDK picks the class that matches. Check with `instanceof`.

| Field                            | Classes                                                                                   |
| -------------------------------- | ----------------------------------------------------------------------------------------- |
| `findAddress()` hits             | `UkAddressSuggestion` with a `udprn`, or `AddressSuggestion` for addresses outside the UK |
| `addressCleanse()` result        | `GbrCleanseMatch` with a `match` address, or `GbrCleanseNoMatch`                          |
| `emailValidation()` result       | `Email`, or `UnknownEmail` when deliverability cannot be checked                          |
| `phoneNumberValidation()` result | `PhoneNumber`, or `InvalidPhoneNumber`                                                    |

union.phpphp

```php
use IdealPostcodes\Types\GbrCleanseMatch;

$result = $client->addressCleanse(['query' => '9 Malyons Rd, Swanley, BR8'])->result;

if ($result instanceof GbrCleanseMatch) {
    echo $result->match->postcode, ' with confidence ', $result->confidence, PHP_EOL;
}
```

## New API values

Your integration keeps working. Fields with a fixed set of values, such as `premise_match` on a cleanse result, are typed as that set for PHPStan and your IDE, but the SDK does not check them at runtime. A value added to the API after your SDK version arrives as a plain string, so compare against the values you handle and fall through for the rest. A field the API adds is left out of the object, `toArray()` and array access until an SDK version that knows it.

## Missing fields

Reading a response never throws. A field the API leaves out or sends as `null` is `null`, or an empty array for a list, whatever its type says. A scalar of another type is converted when nothing is lost, as PHP itself does: `2.0` or `"2"` for an integer, `5` for a string. `2.5` for an integer stays as sent. Any other value of the wrong type is passed through as sent, so the declared types describe the API as documented, not a runtime guarantee.

## Native record

`native` on an `Address` is the raw record from the address's source dataset, as a class named for the dataset: `PafAddress` for Royal Mail PAF, `AbpAddress` for AddressBase Premium, `UspsAddress` for the United States and so on. The `dataset` field on the address says which. A dataset the SDK does not know yet comes back as a plain array, so a new dataset never breaks your integration. `native` is `null` when the API sends no record.

native.phpphp

```php
use IdealPostcodes\Types\PafAddress;

$address = $client->resolveAddress('paf_2670849')->result;

if ($address->native instanceof PafAddress) {
    echo $address->native->deliveryPointSuffix, PHP_EOL;
}
```

The [data sources](/docs/data.md) pages document every dataset's fields.
