Class Salient\Sync\SyncSerializeRules

<TEntity is SyncEntityInterface>

final
implements SyncSerializeRulesInterface<TEntity>, Buildable<SyncSerializeRulesBuilder<TEntity>>
uses HasBuilder, HasMutator

Rules applied when serializing a sync entity

Rules can be applied to any combination of paths and class properties reachable from the entity, e.g. for a User entity that normalises property names:

<?php
[
    // Applied to '.Manager.OrgUnit'
    '.manager.org_unit',
    User::class => [
        // Ignored for '.Manager.OrgUnit' because a path-based rule applies,
        // but applied to '.OrgUnit', '.Staff[].OrgUnit', ...
        'org_unit',
    ],
];

Path-based rules apply to specific nodes in the object graph below the entity (only the OrgUnit of the user's manager in the example above), whereas class-based rules apply to every appearance of a class in the entity's object graph (the OrgUnit of every User object, including the one being serialized, in this example).

Each rule must be either a string containing the path or key to act upon, or an array with up to 3 values:

  1. the path or key to act upon (string; required; must be the first value)
  2. a new key for the value (int|string; optional)
  3. a closure to return a new value for the key (Closure($value, $store, $rules): mixed; optional)

Optional values may be omitted.

If multiple rules apply to the same key, path-based rules take precedence over class-based ones, then later rules take precedence over earlier ones:

<?php
[
    // Ignored because it applies to the same path as the next rule
    '.manager.org_unit',
    // Applied to '.Manager.OrgUnit'
    [
        '.manager.org_unit',
        'org_unit_id',
        fn($ou) => $ou->Id,
    ],
    User::class => [
        // Ignored because it applies to the same property as the next rule
        'org_unit',
        // Ignored for '.Manager.OrgUnit' because a path-based rule applies,
        // but applied to '.OrgUnit', '.Staff[].OrgUnit', ...
        ['org_unit', 'org_unit_id'],
    ],
];
Methods
Methods used from Salient\Core\Concern\HasBuilder
getBuilder(), build(), resolve()