Skip to content

#[TransformKeys]

Transforms all input keys at the class level using a named strategy. Applied before property lookup during hydration, and to property names during serialization.

Syntax

php
use StdOut\SimpleDataObjects\Attributes\TransformKeys;

#[TransformKeys('snake')]    // or 'camel', 'studly', 'kebab'
class MyData extends BaseData { ... }

Strategies

StrategyExample inputAfter transform
snakefirstNamefirst_name
camelfirst_namefirstName
studlyfirst_nameFirstName
kebabfirstNamefirst-name

Example: camelCase API → snake_case input

Your PHP properties are snake_case, but the JSON payload uses camelCase:

php
#[TransformKeys('snake')]
class EventData extends BaseData
{
    public function __construct(
        public readonly string $event_name,
        public readonly string $start_date,
        public readonly int    $ticket_count,
    ) {}
}

$data = EventData::from([
    'eventName'   => 'PHP Conf',
    'startDate'   => '2025-06-15',
    'ticketCount' => 500,
]);

$data->event_name;   // 'PHP Conf'
$data->start_date;   // '2025-06-15'

Serialization mirrors the transform

php
$data->toArray();
// [
//   'eventName'   => 'PHP Conf',
//   'startDate'   => '2025-06-15',
//   'ticketCount' => 500,
// ]

Interaction with #[MapPropertyName]

#[MapPropertyName] on individual parameters takes precedence over #[TransformKeys]. Use #[MapPropertyName] to override specific keys when most keys follow the class-level transform.

Released under the MIT License.