| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace LaravelUi5\OData\Service\Contracts; |
| 6: | |
| 7: | use LaravelUi5\OData\Protocol\Planning\ExpandItem; |
| 8: | |
| 9: | /** |
| 10: | * Resolver that can serve as a virtual navigation expand on parent entities. |
| 11: | * |
| 12: | * Implement this alongside CustomEntitySetInterface when the entity set |
| 13: | * should appear as a navigation property on discovered Eloquent models |
| 14: | * without a real Eloquent relation backing it. |
| 15: | * |
| 16: | * Example: KPIs computed from multiple tables, expanded on User and Project. |
| 17: | * |
| 18: | * class Kpis implements CustomEntitySetInterface, VirtualExpandResolverInterface |
| 19: | * { |
| 20: | * public function expandsOn(): array |
| 21: | * { |
| 22: | * return ['User' => 'kpis', 'Project' => 'kpis']; |
| 23: | * } |
| 24: | * |
| 25: | * public function resolveExpand(array $parentRow, string $parentEntityType, ExpandItem $expand): array |
| 26: | * { |
| 27: | * // parentRow has the User/Project data; expand carries $filter, $select, etc. |
| 28: | * return [['kpi_id' => 1, 'name' => 'Hours', 'value' => 42.0]]; |
| 29: | * } |
| 30: | * } |
| 31: | * |
| 32: | * Registration via discoverCustomEntitySet() automatically wires the |
| 33: | * navigation properties and bindings on the parent entity types. |
| 34: | */ |
| 35: | interface VirtualExpandResolverInterface |
| 36: | { |
| 37: | /** |
| 38: | * Declare which entity types this resolver can be expanded on. |
| 39: | * |
| 40: | * Returns an associative array mapping entity type names to navigation |
| 41: | * property names: ['User' => 'kpis', 'Project' => 'kpis']. |
| 42: | * |
| 43: | * @return array<string, string> |
| 44: | */ |
| 45: | public function expandsOn(): array; |
| 46: | |
| 47: | /** |
| 48: | * Resolve the expand for a single parent entity. |
| 49: | * |
| 50: | * @param array<string, mixed> $parentRow The parent entity's data |
| 51: | * @param string $parentEntityType The parent entity type name (e.g. 'User') |
| 52: | * @param ExpandItem $expand The expand item with filter, select, etc. |
| 53: | * @return list<array<string, mixed>> Child rows to attach under the nav property |
| 54: | */ |
| 55: | public function resolveExpand(array $parentRow, string $parentEntityType, ExpandItem $expand): array; |
| 56: | } |
| 57: |