1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace LaravelUi5\OData\Service\Resolver;
6:
7: use Illuminate\Database\Query\Builder;
8: use Illuminate\Support\Facades\DB;
9: use LaravelUi5\OData\Driver\Sql\SqlEntitySetResolver;
10: use LaravelUi5\OData\Http\CustomQueryOptions;
11: use LaravelUi5\OData\Service\Contracts\EntitySetResolverInterface;
12: use LaravelUi5\OData\Service\Contracts\EntitySetSourceInterface;
13: use LaravelUi5\OData\Service\Contracts\ResolverBindingInterface;
14:
15: /**
16: * Serializable binding for a table-or-view-backed entity set.
17: *
18: * Stores the table/view name and optional connection. For sources that
19: * need implicit filters or dependency injection, use SqlSourceBinding
20: * with an EntitySetSourceInterface class instead.
21: */
22: final readonly class SqlBinding implements ResolverBindingInterface, EntitySetSourceInterface
23: {
24: public function __construct(
25: public string $table,
26: public ?string $connection = null,
27: ) {}
28:
29: public function query(CustomQueryOptions $options): Builder
30: {
31: return DB::connection($this->connection)->table($this->table);
32: }
33:
34: public function createResolver(): EntitySetResolverInterface
35: {
36: return new SqlEntitySetResolver($this);
37: }
38:
39: /**
40: * A raw table/view has no authored class to reflect on — it is gated by
41: * converting it to a SqlSourceBinding with an EntitySetSourceInterface class.
42: */
43: public function getSourceClass(): ?string
44: {
45: return null;
46: }
47: }
48: