Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.

Commit 43329a0

Browse files
committed
Merge pull request #10 from davalb/master
Inventory API Call
2 parents 0bd4241 + 61c6a25 commit 43329a0

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

lib/Discogs/Model/Inventory.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/*
3+
* This file is part of the DiscogsAPI PHP SDK.
4+
*
5+
* (c) Richard van den Brand <richard@vandenbrand.org>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Discogs\Model;
12+
13+
class Inventory implements \Countable, \IteratorAggregate
14+
{
15+
protected $pagination;
16+
protected $listings;
17+
18+
public function setPagination($pagination)
19+
{
20+
$this->pagination = $pagination;
21+
}
22+
23+
public function getPagination()
24+
{
25+
return $this->pagination;
26+
}
27+
28+
public function setListings($listings)
29+
{
30+
$this->listings = $listings;
31+
}
32+
33+
public function getListings()
34+
{
35+
return $this->listings;
36+
}
37+
38+
public function count()
39+
{
40+
return $this->getPagination()->getItems();
41+
}
42+
43+
public function getIterator()
44+
{
45+
return new \ArrayIterator($this->listings);
46+
}
47+
}

lib/Discogs/ResponseTransformer/Model.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ protected function getClass($path)
140140
$key = 'resultset';
141141
} else if (preg_match('#^/(artists|releases|masters|labels)/#i', $path, $matches)) {
142142
$key = $this->getSingular($matches[1]);
143+
} else if (preg_match('#^/users/[\d\w_-]+/inventory#i', $path, $matches)) {
144+
$key = 'inventory';
143145
} else {
144146
$key = '';
145147
}

lib/Discogs/Service.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ public function getLabel($labelId)
169169
{
170170
return $this->call(sprintf('/labels/%d', $labelId));
171171
}
172+
/**
173+
* Implements API users inventory method
174+
*
175+
* @param string $discogsUsername
176+
* @return mixed
177+
* @throws NoResultException
178+
*/
179+
public function getInventory($discogsUsername)
180+
{
181+
return $this->call(sprintf('/users/%s/inventory', $discogsUsername));
182+
}
172183

173184
/**
174185
* @param CacherInterface $cacher

tests/Discogs/Test/Integration/ServiceTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ public function testLabel()
8787
$this->assertEquals($response->getId(), 1);
8888
}
8989

90+
public function testInventory(){
91+
$response = $this->service->getInventory('buyreggae');
92+
$this->assertInstanceOf('Discogs\Model\Pagination', $response->getPagination());
93+
$this->assertTrue(is_array($response->getListings()));
94+
$this->assertGreaterThan(0, $response->count());
95+
$this->assertInstanceOf('ArrayIterator', $response->getIterator());
96+
}
97+
9098
public function testNext()
9199
{
92100
$response = $this->service->search(array('q' => 'vibrasphere'));

0 commit comments

Comments
 (0)