# 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
```

## How do I get the raw array back?

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);
```

## Where are the 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.

## How do I read a field 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;
}
```

## What happens when the API adds a value?

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.

The SDK only throws when a field it needs is missing from the response. See [errors](/docs/sdks/php/errors.md).

## What is in the 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.
