CrudResourceConfig - viames/pair GitHub Wiki

Pair framework: CrudResourceConfig

Pair\Api\CrudResourceConfig is the typed internal value object used by Pair v4 auto-CRUD resources.

It centralizes CRUD defaults and keeps legacy array configs compatible at public boundaries.

Why it exists

Before this object, ApiExposable, CrudController, QueryFilter, and SpecGenerator read the same config keys directly from arrays. That made defaults easy to duplicate and array-key mistakes easy to miss.

CrudResourceConfig gives framework internals typed accessors while preserving existing apiConfig(): array and crud(..., array $config) usage.

Public compatibility

Applications can keep writing array config:

$this->crud('faqs', \App\Models\Faq::class, [
    'readModel' => \App\Api\ReadModels\FaqReadModel::class,
    'sortable' => ['createdAt'],
    'perPage' => 20,
]);

The framework normalizes that array through CrudResourceConfig.

Advanced code may pass an explicit config object:

use Pair\Api\CrudResourceConfig;

$config = CrudResourceConfig::fromArray([
    'readModel' => \App\Api\ReadModels\FaqReadModel::class,
    'perPage' => 20,
]);

$this->crud('faqs', \App\Models\Faq::class, $config);

Defaults

Default values are centralized in CrudResourceConfig:

  • readModel: null
  • resource: null
  • searchable: []
  • sortable: []
  • filterable: []
  • includes: []
  • includeReadModels: []
  • includeResources: []
  • includePreloader: null
  • perPage: 20
  • maxPerPage: 100
  • rules: ['create' => [], 'update' => []]
  • defaultSort: null

Main methods

  • from(array|CrudResourceConfig|null $config): CrudResourceConfig
  • fromArray(array $config): CrudResourceConfig
  • toArray(): array
  • readModel(): ?string
  • resource(): ?string
  • searchable(): array
  • sortable(): array
  • filterable(): array
  • includes(): array
  • includeReadModels(): array
  • includeResources(): array
  • includePreloader(): ?string
  • perPage(): int
  • maxPerPage(): int
  • rules(): array
  • createRules(): array
  • updateRules(): array
  • defaultSort(): ?string

Notes

  • Comments and docblocks are explanatory only; config behavior is encoded in CrudResourceConfig, not in comments.
  • getApiConfig() still returns an array for compatibility.
  • ApiExposable::getCrudResourceConfig() exposes the typed config for internal framework consumers and uses CrudResourceMetadata to avoid repeated normalization.
  • CrudController, SpecGenerator, and QueryFilter consume the typed config internally, even when the original input was an array.
  • includePreloader points to an optional CrudIncludePreloader implementation for bulk include loading on collection responses.

See also: ApiExposable, CrudController, CrudIncludePreloader, CrudResourceMetadata, QueryFilter, SpecGenerator.