Skip to content

Commit ca9cd62

Browse files
committed
wip
1 parent 269207e commit ca9cd62

13 files changed

+136
-890
lines changed

src/Adapter/EloquentAdapter.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,29 +138,29 @@ public function filterByIds($query, array $ids): void
138138

139139
public function filterByAttribute($query, Attribute $attribute, $value): void
140140
{
141-
$property = $this->getAttributeProperty($attribute);
141+
$column = $this->getAttributeColumn($attribute);
142142

143143
// TODO: extract this into non-adapter territory
144144
if (preg_match('/(.+)\.\.(.+)/', $value, $matches)) {
145145
if ($matches[1] !== '*') {
146-
$query->where($property, '>=', $matches[1]);
146+
$query->where($column, '>=', $matches[1]);
147147
}
148148
if ($matches[2] !== '*') {
149-
$query->where($property, '<=', $matches[2]);
149+
$query->where($column, '<=', $matches[2]);
150150
}
151151

152152
return;
153153
}
154154

155155
foreach (['>=', '>', '<=', '<'] as $operator) {
156156
if (strpos($value, $operator) === 0) {
157-
$query->where($property, $operator, substr($value, strlen($operator)));
157+
$query->where($column, $operator, substr($value, strlen($operator)));
158158

159159
return;
160160
}
161161
}
162162

163-
$query->where($property, $value);
163+
$query->where($column, $value);
164164
}
165165

166166
public function filterByHasOne($query, HasOne $relationship, array $ids): void
@@ -181,9 +181,9 @@ public function filterByHasMany($query, HasMany $relationship, array $ids): void
181181
});
182182
}
183183

184-
public function sortByAttribute($query, Attribute $field, string $direction): void
184+
public function sortByAttribute($query, Attribute $attribute, string $direction): void
185185
{
186-
$query->orderBy($this->getAttributeProperty($field), $direction);
186+
$query->orderBy($this->getAttributeColumn($attribute), $direction);
187187
}
188188

189189
public function paginate($query, int $limit, int $offset): void
@@ -224,6 +224,11 @@ private function getAttributeProperty(Attribute $attribute): string
224224
return $attribute->getProperty() ?: strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $attribute->getName()));
225225
}
226226

227+
private function getAttributeColumn(Attribute $attribute): string
228+
{
229+
return $this->model->getTable().'.'.$this->getAttributeProperty($attribute);
230+
}
231+
227232
private function getRelationshipProperty(Relationship $relationship): string
228233
{
229234
return $relationship->getProperty() ?: $relationship->getName();

src/Handler/Concerns/IncludesData.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ private function loadRelationships(array $models, array $include, Request $reque
103103
$adapter = $this->resource->getAdapter();
104104
$fields = $this->resource->getSchema()->getFields();
105105

106+
// TODO: don't load IDs for relationships which are included below
106107
foreach ($fields as $name => $field) {
107108
if (! $field instanceof Relationship || ! evaluate($field->getLinkage(), [$request]) || ! $field->getLoadable()) {
108109
continue;

src/Handler/Index.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313
use Psr\Http\Server\RequestHandlerInterface;
1414
use Tobyz\JsonApiServer\JsonApi;
1515
use Tobyz\JsonApiServer\Exception\BadRequestException;
16-
use Tobyz\JsonApiServer\Exception\ForbiddenException;
1716
use Tobyz\JsonApiServer\JsonApiResponse;
1817
use Tobyz\JsonApiServer\ResourceType;
1918
use Tobyz\JsonApiServer\Schema\Attribute;
2019
use Tobyz\JsonApiServer\Schema\HasMany;
2120
use Tobyz\JsonApiServer\Schema\HasOne;
22-
use Tobyz\JsonApiServer\Schema\Type;
2321
use Tobyz\JsonApiServer\Serializer;
2422

2523
class Index implements RequestHandlerInterface
@@ -47,7 +45,7 @@ public function handle(Request $request): Response
4745
$query = $adapter->query();
4846

4947
foreach ($schema->getScopes() as $scope) {
50-
$request = $scope($request, $query) ?: $request;
48+
$request = $scope($query, $request, null) ?: $request;
5149
}
5250

5351
if ($filter = $request->getAttribute('jsonApiFilter')) {

src/Schema/Field.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ public function getDefault()
162162
return $this->default;
163163
}
164164

165+
/**
166+
* @return bool|Closure
167+
*/
165168
public function getFilterable()
166169
{
167170
return $this->filterable;

src/Schema/Relationship.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public function loadable(Closure $callback = null)
4848
return $this;
4949
}
5050

51-
// public function notLoadable()
52-
// {
53-
// $this->loadable = false;
54-
//
55-
// return $this;
56-
// }
51+
public function notLoadable()
52+
{
53+
$this->loadable = false;
54+
55+
return $this;
56+
}
5757

5858
public function includable()
5959
{
@@ -98,6 +98,9 @@ public function hasLinks(): bool
9898
return $this->links;
9999
}
100100

101+
/**
102+
* @return bool|Closure
103+
*/
101104
public function getLoadable()
102105
{
103106
return $this->loadable;

src/Schema/Type.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ final class Type
1616
private $limit = 50;
1717
private $countable = true;
1818
private $defaultSort;
19+
private $defaultFilter;
1920
private $scopes = [];
2021
private $saver;
2122
private $creatable = false;
@@ -80,6 +81,15 @@ public function getMeta(): array
8081
return $this->meta;
8182
}
8283

84+
public function filter(string $name, Closure $callback)
85+
{
86+
$this->attribute($name)
87+
->hidden()
88+
->filterable($callback);
89+
90+
return $this;
91+
}
92+
8393
public function paginate(int $perPage)
8494
{
8595
$this->paginate = $perPage;
@@ -90,7 +100,7 @@ public function dontPaginate()
90100
$this->paginate = null;
91101
}
92102

93-
public function getPaginate(): int
103+
public function getPaginate(): ?int
94104
{
95105
return $this->paginate;
96106
}
@@ -279,4 +289,16 @@ public function getDefaultSort()
279289
{
280290
return $this->defaultSort;
281291
}
292+
293+
public function defaultFilter(?array $filter)
294+
{
295+
$this->defaultFilter = $filter;
296+
297+
return $this;
298+
}
299+
300+
public function getDefaultFilter()
301+
{
302+
return $this->defaultFilter;
303+
}
282304
}

src/Serializer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private function addToMap(ResourceType $resource, $model, array $include)
9898
ksort($metas);
9999

100100
foreach ($metas as $name => $meta) {
101-
$data['meta'][$name] = new Structure\Meta($meta->name, ($meta->value)($this->request, $model));
101+
$data['meta'][$name] = new Structure\Meta($meta->getName(), ($meta->getValue())($model, $this->request));
102102
}
103103

104104
$this->merge($data);
@@ -270,11 +270,11 @@ private function resourceIdentifier(array $data): Structure\ResourceIdentifier
270270

271271
private function relatedResourceIdentifier(Schema\Relationship $field, $model)
272272
{
273-
$relatedResource = $this->api->getResource($field->resource);
273+
$relatedResource = $this->api->getResource($type = $field->getType());
274274

275275
return $this->resourceIdentifier([
276-
'type' => $field->resource,
277-
'id' => $relatedResource->getAdapter()->getId($model)
276+
'type' => $type,
277+
'id' => is_string($model) ? $model : $relatedResource->getAdapter()->getId($model)
278278
]);
279279
}
280280
}

0 commit comments

Comments
 (0)