Skip to content

Commit 5d20c38

Browse files
authored
Merge pull request #103 from rapkis/feature/request-headers-in-repositories
Add ability to send headers through repositories
2 parents 190554f + 7f10e84 commit 5d20c38

File tree

8 files changed

+36
-27
lines changed

8 files changed

+36
-27
lines changed

src/Actions/Create.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ trait Create
1111
/**
1212
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
1313
* @param array $parameters
14+
* @param array $headers
1415
*
1516
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
1617
*/
17-
public function create(ItemInterface $item, array $parameters = [])
18+
public function create(ItemInterface $item, array $parameters = [], array $headers = [])
1819
{
1920
return $this->getClient()->post(
2021
$this->getEndpoint().'?'.http_build_query($parameters),
21-
$this->documentFactory->make($item)
22+
$this->documentFactory->make($item),
23+
$headers,
2224
);
2325
}
2426
}

src/Actions/Delete.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ trait Delete
99
/**
1010
* @param string $id
1111
* @param array $parameters
12+
* @param array $headers
1213
*
1314
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
1415
*/
15-
public function delete(string $id, array $parameters = [])
16+
public function delete(string $id, array $parameters = [], array $headers = [])
1617
{
17-
return $this->getClient()->delete($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters));
18+
return $this->getClient()->delete($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters), $headers);
1819
}
1920
}

src/Actions/FetchMany.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ trait FetchMany
88
{
99
/**
1010
* @param array $parameters
11+
* @param array $headers
1112
*
1213
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
1314
*/
14-
public function all(array $parameters = [])
15+
public function all(array $parameters = [], array $headers = [])
1516
{
16-
return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters));
17+
return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters), $headers);
1718
}
1819
}

src/Actions/FetchOne.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ trait FetchOne
99
/**
1010
* @param string $id
1111
* @param array $parameters
12+
* @param array $headers
1213
*
1314
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
1415
*/
15-
public function find(string $id, array $parameters = [])
16+
public function find(string $id, array $parameters = [], array $headers = [])
1617
{
17-
return $this->getClient()->get($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters));
18+
return $this->getClient()->get($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters), $headers);
1819
}
1920
}

src/Actions/Save.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ trait Save
1414
/**
1515
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
1616
* @param array $parameters
17+
* @param array $headers
1718
*
1819
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
1920
*/
20-
public function save(ItemInterface $item, array $parameters = [])
21+
public function save(ItemInterface $item, array $parameters = [], array $headers = [])
2122
{
2223
if ($item->isNew()) {
23-
return $this->saveNew($item, $parameters);
24+
return $this->saveNew($item, $parameters, $headers);
2425
}
2526

26-
return $this->saveExisting($item, $parameters);
27+
return $this->saveExisting($item, $parameters, $headers);
2728
}
2829
}

src/Actions/TakeOne.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ trait TakeOne
88
{
99
/**
1010
* @param array $parameters
11+
* @param array $headers
1112
*
1213
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
1314
*/
14-
public function take(array $parameters = [])
15+
public function take(array $parameters = [], array $headers = [])
1516
{
16-
return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters));
17+
return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters), $headers);
1718
}
1819
}

src/Actions/Update.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ trait Update
1111
/**
1212
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
1313
* @param array $parameters
14+
* @param array $headers
1415
*
1516
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
1617
*/
17-
public function update(ItemInterface $item, array $parameters = [])
18+
public function update(ItemInterface $item, array $parameters = [], array $headers = [])
1819
{
1920
return $this->getClient()->patch(
2021
$this->getEndpoint().'/'.urlencode($item->getId()).'?'.http_build_query($parameters),
21-
$this->documentFactory->make($item)
22+
$this->documentFactory->make($item),
23+
$headers,
2224
);
2325
}
2426
}

tests/RepositoryTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ public function itCanGetAll()
5050

5151
$client->expects($this->once())
5252
->method('get')
53-
->with('mocks?foo=bar')
53+
->with('mocks?foo=bar', ['Test-Header' => 'Foo-Bar'])
5454
->willReturn($document);
5555

5656
$repository = new MockRepository($client, new DocumentFactory());
5757

58-
$this->assertSame($document, $repository->all(['foo' => 'bar']));
58+
$this->assertSame($document, $repository->all(['foo' => 'bar'], ['Test-Header' => 'Foo-Bar']));
5959
}
6060

6161
/**
@@ -70,12 +70,12 @@ public function itCanTakeOne()
7070

7171
$client->expects($this->once())
7272
->method('get')
73-
->with('mocks?foo=bar')
73+
->with('mocks?foo=bar', ['Test-Header' => 'Foo-Bar'])
7474
->willReturn($document);
7575

7676
$repository = new MockRepository($client, new DocumentFactory());
7777

78-
$this->assertSame($document, $repository->take(['foo' => 'bar']));
78+
$this->assertSame($document, $repository->take(['foo' => 'bar'], ['Test-Header' => 'Foo-Bar']));
7979
}
8080

8181
/**
@@ -90,12 +90,12 @@ public function itCanFindOne()
9090

9191
$client->expects($this->once())
9292
->method('get')
93-
->with('mocks/1?foo=bar')
93+
->with('mocks/1?foo=bar', ['Test-Header' => 'Foo-Bar'])
9494
->willReturn($document);
9595

9696
$repository = new MockRepository($client, new DocumentFactory());
9797

98-
$this->assertSame($document, $repository->find('1', ['foo' => 'bar']));
98+
$this->assertSame($document, $repository->find('1', ['foo' => 'bar'], ['Test-Header' => 'Foo-Bar']));
9999
}
100100

101101
/**
@@ -111,12 +111,12 @@ public function itCanSaveNew()
111111

112112
$client->expects($this->once())
113113
->method('post')
114-
->with('mocks?foo=bar')
114+
->with('mocks?foo=bar', $document, ['Test-Header' => 'Foo-Bar'])
115115
->willReturn($document);
116116

117117
$repository = new MockRepository($client, new DocumentFactory());
118118

119-
$this->assertSame($document, $repository->save(new Item(), ['foo' => 'bar']));
119+
$this->assertSame($document, $repository->save(new Item(), ['foo' => 'bar'], ['Test-Header' => 'Foo-Bar']));
120120
}
121121

122122
/**
@@ -132,12 +132,12 @@ public function itCanSaveExisting()
132132

133133
$client->expects($this->once())
134134
->method('patch')
135-
->with('mocks/1?foo=bar')
135+
->with('mocks/1?foo=bar', $document, ['Test-Header' => 'Foo-Bar'])
136136
->willReturn($document);
137137

138138
$repository = new MockRepository($client, new DocumentFactory());
139139

140-
$this->assertSame($document, $repository->save((new Item())->setId('1'), ['foo' => 'bar']));
140+
$this->assertSame($document, $repository->save((new Item())->setId('1'), ['foo' => 'bar'], ['Test-Header' => 'Foo-Bar']));
141141
}
142142

143143
/**
@@ -152,11 +152,11 @@ public function itCanDelete()
152152

153153
$client->expects($this->once())
154154
->method('delete')
155-
->with('mocks/1?foo=bar')
155+
->with('mocks/1?foo=bar', ['Test-Header' => 'Foo-Bar'])
156156
->willReturn($document);
157157

158158
$repository = new MockRepository($client, new DocumentFactory());
159159

160-
$this->assertSame($document, $repository->delete('1', ['foo' => 'bar']));
160+
$this->assertSame($document, $repository->delete('1', ['foo' => 'bar'], ['Test-Header' => 'Foo-Bar']));
161161
}
162162
}

0 commit comments

Comments
 (0)