From a2e270de141fbde069dfbc6351124ea29f86a970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20H=C3=BCbner?= Date: Sun, 20 Nov 2016 22:56:26 +0100 Subject: [PATCH] Update Rebrickable client --- .../Converter/PartPropertyNameConverter.php | 24 ++ .../Client/Rebrickable/Entity/Color.php | 233 ++++++++++++ .../Client/Rebrickable/Entity/Part.php | 350 +++++++++++------- .../Client/Rebrickable/Entity/Set.php | 80 ++++ .../Client/Rebrickable/Rebrickable.php | 264 ++++++++----- 5 files changed, 716 insertions(+), 235 deletions(-) create mode 100644 src/AppBundle/Client/Rebrickable/Converter/PartPropertyNameConverter.php create mode 100644 src/AppBundle/Client/Rebrickable/Entity/Color.php create mode 100644 src/AppBundle/Client/Rebrickable/Entity/Set.php diff --git a/src/AppBundle/Client/Rebrickable/Converter/PartPropertyNameConverter.php b/src/AppBundle/Client/Rebrickable/Converter/PartPropertyNameConverter.php new file mode 100644 index 0000000..0e36b32 --- /dev/null +++ b/src/AppBundle/Client/Rebrickable/Converter/PartPropertyNameConverter.php @@ -0,0 +1,24 @@ +rb_color_id; + } + + /** + * @param int $rb_color_id + */ + public function setRbColorId($rb_color_id) + { + $this->rb_color_id = $rb_color_id; + } + + /** + * @return array + */ + public function getLdrawColorId() + { + return $this->ldraw_color_id; + } + + /** + * @param array $ldraw_color_id + */ + public function setLdrawColorId($ldraw_color_id) + { + $this->ldraw_color_id = $ldraw_color_id; + } + + /** + * @return array + */ + public function getBricklinkColorId() + { + return $this->bricklink_color_id; + } + + /** + * @param array $bricklink_color_id + */ + public function setBricklinkColorId($bricklink_color_id) + { + $this->bricklink_color_id = $bricklink_color_id; + } + + /** + * @return array + */ + public function getBrickowlColorId() + { + return $this->brickowl_color_id; + } + + /** + * @param array $brickowl_color_id + */ + public function setBrickowlColorId($brickowl_color_id) + { + $this->brickowl_color_id = $brickowl_color_id; + } + + /** + * @return string + */ + public function getColorName() + { + return $this->color_name; + } + + /** + * @param string $color_name + */ + public function setColorName($color_name) + { + $this->color_name = $color_name; + } + + /** + * @return int + */ + public function getNumParts() + { + return $this->num_parts; + } + + /** + * @param int $num_parts + */ + public function setNumParts($num_parts) + { + $this->num_parts = $num_parts; + } + + /** + * @return int + */ + public function getNumSets() + { + return $this->num_sets; + } + + /** + * @param int $num_sets + */ + public function setNumSets($num_sets) + { + $this->num_sets = $num_sets; + } + + /** + * @return int + */ + public function getYear1() + { + return $this->year1; + } + + /** + * @param int $year1 + */ + public function setYear1($year1) + { + $this->year1 = $year1; + } + + /** + * @return int + */ + public function getYear2() + { + return $this->year2; + } + + /** + * @param int $year2 + */ + public function setYear2($year2) + { + $this->year2 = $year2; + } + + /** + * @return string + */ + public function getRgb() + { + return $this->rgb; + } + + /** + * @param string $rgbl + */ + public function setRgb($rgb) + { + $this->rgb = $rgb; + } + + +} \ No newline at end of file diff --git a/src/AppBundle/Client/Rebrickable/Entity/Part.php b/src/AppBundle/Client/Rebrickable/Entity/Part.php index 7d172a7..d70651c 100644 --- a/src/AppBundle/Client/Rebrickable/Entity/Part.php +++ b/src/AppBundle/Client/Rebrickable/Entity/Part.php @@ -3,160 +3,242 @@ * Created by PhpStorm. * User: hubnedav * Date: 11/11/16 - * Time: 12:45 AM + * Time: 12:45 AM. */ namespace AppBundle\Client\Rebrickable\Entity; - class Part { - protected $id; - protected $name; - protected $category; - protected $typeId; - protected $colors; - protected $external_part_ids; - protected $part_url; - protected $part_img_url; + /** + * Part ID. + * + * @var int + */ + private $id; + /** + * Quantity of part in set returned from getSetParts. + * + * @var int + */ + private $qty; + /** + * Part Name. + * + * @var string + */ + private $name; + /** + * Year the part first appeared in sets. + * + * @var int + */ + private $year1; + /** + * Year the part was last seen in sets. + * + * @var int + */ + private $year2; + /** + * Part category/type description. + * + * @var string + */ + private $category; - public function __construct($part_id, $part_name, $category, $part_type_id, $colors, $external_part_ids, $part_url, $part_img_url) - { - $this->id = $part_id; - $this->name = $part_name; - $this->category = $category; - $this->typeId = $part_type_id; - $this->colors = $colors; - $this->external_part_ids = $external_part_ids; - $this->part_url = $part_url; - $this->part_img_url = $part_img_url; - } + private $typeId; - /** - * @return mixed - */ - public function getId() - { - return $this->id; - } + /** + * Array of colors the part appears in. + * + * @var array + */ + private $colors; - /** - * @param mixed $id - */ - public function setId($id) - { - $this->id = $id; - } + private $colorId; - /** - * @return mixed - */ - public function getName() - { - return $this->name; - } + /** + * Array of related Part IDs used by external systems. + * + * @var array + */ + private $external_part_ids; + /** + * Rebrickable URL to the Part Details page. + * + * @var string + */ + private $part_url; + /** + * Rebrickable URL of the main part image (tries to use most common color). + * + * @var string + */ + private $part_img_url; - /** - * @param mixed $name - */ - public function setName($name) - { - $this->name = $name; - } - /** - * @return mixed - */ - public function getCategory() - { - return $this->category; - } + public function __construct() + { + } - /** - * @param mixed $category - */ - public function setCategory($category) - { - $this->category = $category; - } + /** + * @return int + */ + public function getId() + { + return $this->id; + } - /** - * @return mixed - */ - public function getTypeId() - { - return $this->typeId; - } + /** + * @param $part_id + */ + public function setId($id) + { + $this->id = $id; + } - /** - * @param mixed $typeId - */ - public function setTypeId($typeId) - { - $this->typeId = $typeId; - } + /** + * @return int + */ + public function getQty() + { + return $this->qty; + } - /** - * @return mixed - */ - public function getColors() - { - return $this->colors; - } + /** + * @param $qty + */ + public function setQty($qty) + { + $this->qty = $qty; + } - /** - * @param mixed $colors - */ - public function setColors($colors) - { - $this->colors = $colors; - } + /** + * @return string + */ + public function getName() + { + return $this->name; + } - /** - * @return mixed - */ - public function getExternalPartIds() - { - return $this->external_part_ids; - } + /** + * @param mixed $name + */ + public function setName($name) + { + $this->name = $name; + } - /** - * @param mixed $external_part_ids - */ - public function setExternalPartIds($external_part_ids) - { - $this->external_part_ids = $external_part_ids; - } + /** + * @return mixed + */ + public function getYear1() + { + return $this->year1; + } - /** - * @return mixed - */ - public function getPartUrl() - { - return $this->part_url; - } + /** + * @param mixed $year1 + */ + public function setYear1($year1) + { + $this->year1 = $year1; + } - /** - * @param mixed $part_url - */ - public function setPartUrl($part_url) - { - $this->part_url = $part_url; - } + /** + * @return mixed + */ + public function getYear2() + { + return $this->year2; + } - /** - * @return mixed - */ - public function getPartImgUrl() - { - return $this->part_img_url; - } + /** + * @param mixed $year2 + */ + public function setYear2($year2) + { + $this->year2 = $year2; + } - /** - * @param mixed $part_img_url - */ - public function setPartImgUrl($part_img_url) - { - $this->part_img_url = $part_img_url; - } -} \ No newline at end of file + /** + * @return mixed + */ + public function getCategory() + { + return $this->category; + } + + /** + * @param mixed $category + */ + public function setCategory($category) + { + $this->category = $category; + } + + /** + * @return mixed + */ + public function getColors() + { + return $this->colors; + } + + /** + * @param mixed $colors + */ + public function setColors($colors) + { + $this->colors = $colors; + } + + /** + * @return mixed + */ + public function getExternalPartIds() + { + return $this->external_part_ids; + } + + /** + * @param mixed $external_part_ids + */ + public function setExternalPartIds($external_part_ids) + { + $this->external_part_ids = $external_part_ids; + } + + /** + * @return mixed + */ + public function getPartUrl() + { + return $this->part_url; + } + + /** + * @param mixed $part_url + */ + public function setPartUrl($part_url) + { + $this->part_url = $part_url; + } + + /** + * @return mixed + */ + public function getPartImgUrl() + { + return $this->part_img_url; + } + + /** + * @param $part_img_url + */ + public function setPartImgUrl($part_img_url) + { + $this->part_img_url = $part_img_url; + } +} diff --git a/src/AppBundle/Client/Rebrickable/Entity/Set.php b/src/AppBundle/Client/Rebrickable/Entity/Set.php new file mode 100644 index 0000000..9c836e8 --- /dev/null +++ b/src/AppBundle/Client/Rebrickable/Entity/Set.php @@ -0,0 +1,80 @@ +set_id; + } + + /** + * @param int $set_id + */ + public function setSetId($set_id) + { + $this->set_id = $set_id; + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @param string $name + */ + public function setName($name) + { + $this->name = $name; + } + + /** + * @return int + */ + public function getNumParts() + { + return $this->num_parts; + } + + /** + * @param int $num_parts + */ + public function setNumParts($num_parts) + { + $this->num_parts = $num_parts; + } +} diff --git a/src/AppBundle/Client/Rebrickable/Rebrickable.php b/src/AppBundle/Client/Rebrickable/Rebrickable.php index a9e312d..7d4cdc7 100644 --- a/src/AppBundle/Client/Rebrickable/Rebrickable.php +++ b/src/AppBundle/Client/Rebrickable/Rebrickable.php @@ -2,126 +2,188 @@ namespace AppBundle\Client\Rebrickable; +use AppBundle\Client\Rebrickable\Entity\Color; +use AppBundle\Client\Rebrickable\Entity\Part; +use AppBundle\Client\Rebrickable\Entity\Set; +use AppBundle\Client\Rebrickable\Converter\PartPropertyNameConverter; use GuzzleHttp\Client; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ConnectException; use Symfony\Component\Asset\Exception\LogicException; +use Symfony\Component\Serializer\Encoder\JsonEncoder; +use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; +use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; +use Symfony\Component\Serializer\Serializer; class Rebrickable { - const BASE_URI = 'https://rebrickable.com/api/'; - const FORMAT = 'json'; + const BASE_URI = 'https://rebrickable.com/api/'; + const FORMAT = 'json'; - private $client; + private $client; - private $apiKey; + private $apiKey; - /** - * RebrickableAPI constructor. - */ - public function __construct($apiKey) - { - $this->client = new Client(['base_uri' => self::BASE_URI]); - $this->apiKey = $apiKey; - } + /** + * RebrickableAPI constructor. + */ + public function __construct($apiKey) + { + $this->client = new Client(['base_uri' => self::BASE_URI]); + $this->apiKey = $apiKey; + } + /** + * @param $method + * @param $parameters + * + * @return null|string + */ + private function call($method, $parameters = []) + { + $parameters['query']['key'] = $this->apiKey; + $parameters['query']['format'] = self::FORMAT; - private function call($method, $parameters) { - $parameters['query']['key'] = $this->apiKey; - $parameters['query']['format'] = self::FORMAT; + try { + $response = $this->client->request('GET', $method, $parameters); - try - { - $response = $this->client->request('GET', $method, $parameters); + if ($response->getStatusCode() === 200) { + $content = $response->getBody()->getContents(); + if ($content === 'INVALIDKEY') { + //TODO + throw new LogicException('Invalid API Key'); + } elseif ($content === 'NOSET' || $content === 'NOPART') { + return null; + } else { + return $content; + } + } else { + //TODO + throw new LogicException($response->getStatusCode()); + } + } catch (ConnectException $e) { + //TODO + throw new LogicException($e->getMessage()); + } catch (ClientException $e) { + //TODO + throw new LogicException($e->getMessage()); + } + } - if ($response->getStatusCode() === 200) - { - $content = $response->getBody()->getContents(); - if($content === 'INVALIDKEY') { - //TODO - throw new LogicException("Invalid API Key"); - } else { - return $content; - } - } - else - { - //TODO - throw new LogicException($response->getStatusCode()); - } - } catch (ConnectException $e) { - //TODO - throw new LogicException($e->getMessage()); - } catch (ClientException $e) { - //TODO - throw new LogicException($e->getMessage()); - } - } + private function getSerializer() + { + $encoders = array(new JsonEncoder()); + $nameConverter = new PartPropertyNameConverter(); + $normalizers = array(new ObjectNormalizer(null,$nameConverter), new ArrayDenormalizer()); + $serializer = new Serializer($normalizers, $encoders); - /** - * Get a list of all parts (normal + spare) used in a set. - * - * @param string $setName unique rebrickable set name - * - * @return array - */ - public function getSetParts($setName) - { - $parameters = [ - 'query' => [ - 'set' => $setName, - ] - ]; + return $serializer; + } - return $this->call('get_set_parts', $parameters); - } + /** + * Get a list of all parts (normal + spare) used in a set. + * + * @param string $setName unique rebrickable set name + * + * @return Part[]|null + */ + public function getSetParts($setName) + { + $parameters = [ + 'query' => [ + 'set' => $setName, + ], + ]; - /** - * Get details about a specific part. - * - * @param $partID - * - * @return - */ - public function getPart($partID) - { - $parameters = [ - 'query' => [ - 'part_id' => $partID, - 'inc_ext' => 1 - ] - ]; + $data = $this->call('get_set_parts', $parameters); - return $this->call('get_part',$parameters); - } + $serializer = $this->getSerializer(); + $partsSTD = json_decode($data, true)[0]['parts']; - /** - * Get the list of colors used by all parts. - * - * @return - */ - public function getColors() - { - return $this->call('get_colors', []); - } + return $data ? $serializer->denormalize($partsSTD, Part::class.'[]', self::FORMAT) : null; + } - /** - * Get the list of sets that a specific part/color appears in. - * - * @param $partID - * @param $colorID - * - * @return - */ - public function getPartSets($partID, $colorID) - { - $parameters = [ - 'query' => [ - 'part_id' => $partID, - 'color_id' => $colorID - ] - ]; + /** + * Get details about a specific part. + * + * @param $partID + * + * @return Part|null + */ + public function getPart($partID) + { + $parameters = [ + 'query' => [ + 'part_id' => $partID, + 'inc_ext' => 1, + ], + ]; - return $this->call('get_part_sets', $parameters); - } + $data = $this->call('get_part', $parameters); + $serializer = $this->getSerializer(); + + return $data ? $serializer->deserialize($data, Part::class, self::FORMAT) : null; + } + + /** + * Get associative array of colors used by all parts where key == rb_color_id + * + * @return Color[]|null + */ + public function getColors() + { + $data = json_decode($this->call('get_colors'), true); + + $serializer = $this->getSerializer(); + + $colors = []; + + foreach ($data as $item) { + $color = $serializer->denormalize($item, Color::class, self::FORMAT); + $colors[$color->getRbColorId()] = $color; + } + + return $data ? $colors : null; + } + + /** + * Get associative array of themes used by all parts where key == part_type_id + * + * @return string[] + */ + public function getPartTypes() + { + $data = json_decode($this->call('get_part_types'), true)['part_types']; + + $types = []; + foreach ($data as $item) { + $types[$item['part_type_id']] = $item['desc']; + } + + return $data ? $types : null; + } + + /** + * Get the list of sets that a specific part/color appears in. + * + * @param $partID + * @param $colorID + * + * @return Set[] + */ + public function getPartSets($partID, $colorID) + { + $parameters = [ + 'query' => [ + 'part_id' => $partID, + 'color_id' => $colorID, + ], + ]; + + $serializer = $this->getSerializer(); + + $data = json_decode($this->call('get_part_sets', $parameters), true)[0]['sets']; + + return $data ? $serializer->denormalize($data, Set::class.'[]', self::FORMAT) : null; + } }