The Concept
Reusable response objects are implemented as subclasses ofAbstractObjectProperty. They define a fixed set of properties that can be included in multiple ApivalkResponseDocumentation definitions.
Built-in Examples
ValidationErrorObject
This object is used to represent a single validation error or a generic error message.
Structure:
parameter(string): The name of the field that caused the error.message(string): The human-readable error message.errorKey(string): The machine-readable error code.
Creating Your Own
You can create custom reusable objects by following these steps:- Create a Property Collection: Extend
AbstractPropertyCollectionand add your properties. - Create the Object: Extend
AbstractObjectPropertyand return your collection ingetPropertyCollection().
Example: UserObject
This example shows an object schema used for documentation and, optionally, as a runtime data container.
- The constructor must call
parent::__construct($name, $description). - Do not hydrate runtime values in the constructor. The documentation needs to be built independently of real values.
- Populate the object only when you actually need an instantiated object at runtime, for example in
populate(), a builder, or from a DTO.
- from a database record
- via setters/getters
- with a fluent builder
- by mapping a DTO
Benefits
- Consistency: All endpoints returning a “User” will have the exact same structure.
- Maintenance: If you add a field to the
UserObject, it is automatically updated in the OpenAPI documentation for all endpoints that use it. - DRY: You don’t have to redefine the same properties repeatedly.