Skip to content

Commit

Permalink
Merge branch 'release/0.9.4'
Browse files Browse the repository at this point in the history
* release/0.9.4:
  Add support to map param types
  • Loading branch information
inakiabt committed Mar 26, 2017
2 parents a1cc247 + 37826d8 commit 4d6f97e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "inakiabt/etsy-php",
"version": "0.9.3",
"version": "0.9.4",
"description": "Simple PHP wrapper for Etsy API",
"license": "MIT",
"authors": [
Expand Down
41 changes: 24 additions & 17 deletions src/Etsy/RequestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ public static function validateParams($args, $methodInfo)
return $result;
}

protected static function transformValueType($type) {
switch($type)
{
case 'integer':
return 'int';
case 'double':
return 'float';
}
return $type;
}

public static function validateData($args, $methodInfo)
{
$result = array('_valid' => array());
Expand All @@ -58,39 +69,35 @@ public static function validateData($args, $methodInfo)
if (isset($methodsParams[$name]))
{
$validType = $methodsParams[$name];
$type = gettype($arg);
$type = self::transformValueType(gettype($arg));
switch($type)
{
case 'integer':
$type = 'int';
break;
case 'double':
$type = 'float';
break;
case 'array':
if (@array_key_exists('json', $arg)) {
$type = 'json';
$arg = $arg['json'];
break;
}

if (count($arg) > 0)
{
if (preg_match('@^map\(@', $validType)) {
$valueTypes = array();
foreach ($arg as $value)
{
$valueTypes[] = self::transformValueType(gettype($value));
}
$type = 'map(' . implode($valueTypes, ', ') . ')';
break;
}

if (preg_match('/@.*?;type=.*?\/.+$/', @$arg[0]))
{
$type = 'imagefile';
$name = '@' . $name;
$arg = @$arg[0];
} else {
$item_type = @gettype($arg[0]);
switch($item_type)
{
case 'integer':
$item_type = 'int';
break;
case 'double':
$item_type = 'float';
break;
}
$item_type = self::transformValueType(@gettype($arg[0]));
$type = 'array('.$item_type.')';
}
}
Expand Down
33 changes: 33 additions & 0 deletions tests/Etsy/EtsyApiBuildRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,39 @@ public function testValidJsonDataParam()
'method' => 'POST'));
}

public function testValidMapParam()
{
$args = array(
'params' => array(
'listing_id' => 654321
),
'data' => array(
"custom_property_names" => array(2, "Steel Black"),
"variations" => array(
'json' => json_encode(
array(
array(
'property_id' => 200,
'value' => "Black"
),
array(
'property_id' => 200,
'value' => "White"
)
)
)
)
)
);

$result = $this->api->createListingVariations($args);
$args['data']['variations'] = $args['data']['variations']['json'];
$this->assertEquals($result, array(
'path' => '/listings/654321/variations',
'data' => $args['data'],
'method' => 'POST'));
}

/**
* @expectedException Exception
*/
Expand Down

0 comments on commit 4d6f97e

Please sign in to comment.