1
0
mirror of https://github.com/ToxicCrack/PrintABrick.git synced 2025-05-17 04:40:08 -07:00

Update Rebrickable client

This commit is contained in:
David Hübner 2016-11-20 22:56:26 +01:00
parent 2d57bc8ed3
commit a2e270de14
5 changed files with 716 additions and 235 deletions

View File

@ -0,0 +1,24 @@
<?php
namespace AppBundle\Client\Rebrickable\Converter;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
class PartPropertyNameConverter implements NameConverterInterface
{
public function normalize($propertyName)
{
return $propertyName;
}
public function denormalize($propertyName)
{
switch ($propertyName) {
case 'part_name': return 'name';
case 'part_id': return 'id';
case 'part_type_id': return 'typeId';
case 'rb_color_id': return 'colorId';
default: return $propertyName;
}
}
}

View File

@ -0,0 +1,233 @@
<?php
/**
* Created by PhpStorm.
* User: hubnedav
* Date: 11/18/16
* Time: 1:45 AM
*/
namespace AppBundle\Client\Rebrickable\Entity;
class Color
{
/**
* Internal Rebrickable color ID used
* @var int
*/
protected $rb_color_id;
/**
* Array of mapped LDraw colors
* @var array
*/
protected $ldraw_color_id;
/**
* Array of mapped BrickLink colors
* @var array
*/
protected $bricklink_color_id;
/**
* Array of mapped BrickOwl colors
* @var array
*/
protected $brickowl_color_id;
/**
* Color name
* @var string
*/
protected $color_name;
/**
* Number of parts the color appears in.
* @var int
*/
protected $num_parts;
/**
* Number of sets the color appears in.
* @var int
*/
protected $num_sets;
/**
* First year it was used.
* @var int
*/
protected $year1;
/**
* Last year it was used.
* @var int
*/
protected $year2;
/**
* Hex codes for the RGB value of this color: RRGGBB
* @var string
*/
protected $rgb;
/**
* Color constructor.
*/
public function __construct()
{
}
/**
* @return int
*/
public function getRbColorId()
{
return $this->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;
}
}

View File

@ -3,37 +3,87 @@
* 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)
private $typeId;
/**
* Array of colors the part appears in.
*
* @var array
*/
private $colors;
private $colorId;
/**
* 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;
public function __construct()
{
$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;
}
/**
* @return mixed
* @return int
*/
public function getId()
{
@ -41,7 +91,7 @@ class Part
}
/**
* @param mixed $id
* @param $part_id
*/
public function setId($id)
{
@ -49,7 +99,23 @@ class Part
}
/**
* @return mixed
* @return int
*/
public function getQty()
{
return $this->qty;
}
/**
* @param $qty
*/
public function setQty($qty)
{
$this->qty = $qty;
}
/**
* @return string
*/
public function getName()
{
@ -64,6 +130,38 @@ class Part
$this->name = $name;
}
/**
* @return mixed
*/
public function getYear1()
{
return $this->year1;
}
/**
* @param mixed $year1
*/
public function setYear1($year1)
{
$this->year1 = $year1;
}
/**
* @return mixed
*/
public function getYear2()
{
return $this->year2;
}
/**
* @param mixed $year2
*/
public function setYear2($year2)
{
$this->year2 = $year2;
}
/**
* @return mixed
*/
@ -80,22 +178,6 @@ class Part
$this->category = $category;
}
/**
* @return mixed
*/
public function getTypeId()
{
return $this->typeId;
}
/**
* @param mixed $typeId
*/
public function setTypeId($typeId)
{
$this->typeId = $typeId;
}
/**
* @return mixed
*/
@ -153,7 +235,7 @@ class Part
}
/**
* @param mixed $part_img_url
* @param $part_img_url
*/
public function setPartImgUrl($part_img_url)
{

View File

@ -0,0 +1,80 @@
<?php
namespace AppBundle\Client\Rebrickable\Entity;
class Set
{
/**
* Set ID if relevan to type.
*
* @var int
*/
protected $set_id;
/**
* Set description.
*
* @var string
*/
protected $name;
/**
* Number of sets the part appears in.
*
* @var int
*/
protected $num_parts;
/**
* Set constructor.
*/
public function __construct()
{
}
/**
* @return int
*/
public function getSetId()
{
return $this->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;
}
}

View File

@ -2,10 +2,18 @@
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
{
@ -25,27 +33,31 @@ class Rebrickable
$this->apiKey = $apiKey;
}
private function call($method, $parameters) {
/**
* @param $method
* @param $parameters
*
* @return null|string
*/
private function call($method, $parameters = [])
{
$parameters['query']['key'] = $this->apiKey;
$parameters['query']['format'] = self::FORMAT;
try
{
try {
$response = $this->client->request('GET', $method, $parameters);
if ($response->getStatusCode() === 200)
{
if ($response->getStatusCode() === 200) {
$content = $response->getBody()->getContents();
if ($content === 'INVALIDKEY') {
//TODO
throw new LogicException("Invalid API Key");
throw new LogicException('Invalid API Key');
} elseif ($content === 'NOSET' || $content === 'NOPART') {
return null;
} else {
return $content;
}
}
else
{
} else {
//TODO
throw new LogicException($response->getStatusCode());
}
@ -58,22 +70,37 @@ class Rebrickable
}
}
private function getSerializer()
{
$encoders = array(new JsonEncoder());
$nameConverter = new PartPropertyNameConverter();
$normalizers = array(new ObjectNormalizer(null,$nameConverter), new ArrayDenormalizer());
$serializer = new Serializer($normalizers, $encoders);
return $serializer;
}
/**
* Get a list of all parts (normal + spare) used in a set.
*
* @param string $setName unique rebrickable set name
*
* @return array
* @return Part[]|null
*/
public function getSetParts($setName)
{
$parameters = [
'query' => [
'set' => $setName,
]
],
];
return $this->call('get_set_parts', $parameters);
$data = $this->call('get_set_parts', $parameters);
$serializer = $this->getSerializer();
$partsSTD = json_decode($data, true)[0]['parts'];
return $data ? $serializer->denormalize($partsSTD, Part::class.'[]', self::FORMAT) : null;
}
/**
@ -81,28 +108,59 @@ class Rebrickable
*
* @param $partID
*
* @return
* @return Part|null
*/
public function getPart($partID)
{
$parameters = [
'query' => [
'part_id' => $partID,
'inc_ext' => 1
]
'inc_ext' => 1,
],
];
return $this->call('get_part',$parameters);
$data = $this->call('get_part', $parameters);
$serializer = $this->getSerializer();
return $data ? $serializer->deserialize($data, Part::class, self::FORMAT) : null;
}
/**
* Get the list of colors used by all parts.
* Get associative array of colors used by all parts where key == rb_color_id
*
* @return
* @return Color[]|null
*/
public function getColors()
{
return $this->call('get_colors', []);
$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;
}
/**
@ -111,17 +169,21 @@ class Rebrickable
* @param $partID
* @param $colorID
*
* @return
* @return Set[]
*/
public function getPartSets($partID, $colorID)
{
$parameters = [
'query' => [
'part_id' => $partID,
'color_id' => $colorID
]
'color_id' => $colorID,
],
];
return $this->call('get_part_sets', $parameters);
$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;
}
}