Skip to content

Commit

Permalink
Merge pull request #103 from rapkis/feature/request-headers-in-reposi…
Browse files Browse the repository at this point in the history
…tories

Add ability to send headers through repositories
  • Loading branch information
JaZo committed May 15, 2024
2 parents 190554f + 7f10e84 commit 5d20c38
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 27 deletions.
6 changes: 4 additions & 2 deletions src/Actions/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ trait Create
/**
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
* @param array $parameters
* @param array $headers
*
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
*/
public function create(ItemInterface $item, array $parameters = [])
public function create(ItemInterface $item, array $parameters = [], array $headers = [])
{
return $this->getClient()->post(
$this->getEndpoint().'?'.http_build_query($parameters),
$this->documentFactory->make($item)
$this->documentFactory->make($item),
$headers,
);
}
}
5 changes: 3 additions & 2 deletions src/Actions/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ trait Delete
/**
* @param string $id
* @param array $parameters
* @param array $headers
*
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
*/
public function delete(string $id, array $parameters = [])
public function delete(string $id, array $parameters = [], array $headers = [])
{
return $this->getClient()->delete($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters));
return $this->getClient()->delete($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters), $headers);
}
}
5 changes: 3 additions & 2 deletions src/Actions/FetchMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ trait FetchMany
{
/**
* @param array $parameters
* @param array $headers
*
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
*/
public function all(array $parameters = [])
public function all(array $parameters = [], array $headers = [])
{
return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters));
return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters), $headers);
}
}
5 changes: 3 additions & 2 deletions src/Actions/FetchOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ trait FetchOne
/**
* @param string $id
* @param array $parameters
* @param array $headers
*
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
*/
public function find(string $id, array $parameters = [])
public function find(string $id, array $parameters = [], array $headers = [])
{
return $this->getClient()->get($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters));
return $this->getClient()->get($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters), $headers);
}
}
7 changes: 4 additions & 3 deletions src/Actions/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ trait Save
/**
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
* @param array $parameters
* @param array $headers
*
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
*/
public function save(ItemInterface $item, array $parameters = [])
public function save(ItemInterface $item, array $parameters = [], array $headers = [])
{
if ($item->isNew()) {
return $this->saveNew($item, $parameters);
return $this->saveNew($item, $parameters, $headers);
}

return $this->saveExisting($item, $parameters);
return $this->saveExisting($item, $parameters, $headers);
}
}
5 changes: 3 additions & 2 deletions src/Actions/TakeOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ trait TakeOne
{
/**
* @param array $parameters
* @param array $headers
*
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
*/
public function take(array $parameters = [])
public function take(array $parameters = [], array $headers = [])
{
return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters));
return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters), $headers);
}
}
6 changes: 4 additions & 2 deletions src/Actions/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ trait Update
/**
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
* @param array $parameters
* @param array $headers
*
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
*/
public function update(ItemInterface $item, array $parameters = [])
public function update(ItemInterface $item, array $parameters = [], array $headers = [])
{
return $this->getClient()->patch(
$this->getEndpoint().'/'.urlencode($item->getId()).'?'.http_build_query($parameters),
$this->documentFactory->make($item)
$this->documentFactory->make($item),
$headers,
);
}
}
24 changes: 12 additions & 12 deletions tests/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public function itCanGetAll()

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

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

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

/**
Expand All @@ -70,12 +70,12 @@ public function itCanTakeOne()

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

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

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

/**
Expand All @@ -90,12 +90,12 @@ public function itCanFindOne()

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

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

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

/**
Expand All @@ -111,12 +111,12 @@ public function itCanSaveNew()

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

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

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

/**
Expand All @@ -132,12 +132,12 @@ public function itCanSaveExisting()

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

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

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

/**
Expand All @@ -152,11 +152,11 @@ public function itCanDelete()

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

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

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

0 comments on commit 5d20c38

Please sign in to comment.