Skip to content

Commit

Permalink
improved some code
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Zonneveld committed Feb 10, 2023
1 parent d7569be commit cfdf312
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 95 deletions.
4 changes: 2 additions & 2 deletions src/Mappers/Contacts/AttributeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class AttributeMapper extends BaseMapper
*/
public function map(stdClass $data): Attribute
{
$fieldType = $data->type;
$isSoftReadOnly = property_exists($data, 'is_soft_read_only') && $data->is_soft_read_only;
$isHardReadOnly = property_exists($data, 'is_hard_read_only') && $data->is_hard_read_only;
$isPiggyDefined = property_exists($data, 'is_piggy_defined') && $data->is_piggy_defined;
Expand All @@ -25,13 +26,12 @@ public function map(stdClass $data): Attribute
$options[] = get_object_vars($item);
}
}
// var_dump('options', $options);

return new Attribute(
$data->name,
$data->label,
$data->type,
$data->field_type ?? null,
$fieldType,
$data->description ?? null,
$isSoftReadOnly,
$isHardReadOnly,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class RewardAttributeMapper
*/
public function map(stdClass $rewardAttribute): RewardAttribute
{
$fieldType = $rewardAttribute->type;
$isSoftReadOnly = property_exists($rewardAttribute, 'is_soft_read_only') && $rewardAttribute->is_soft_read_only;
$isHardReadOnly = property_exists($rewardAttribute, 'is_hard_read_only') && $rewardAttribute->is_hard_read_only;
$isPiggyDefined = property_exists($rewardAttribute, 'is_piggy_defined') && $rewardAttribute->is_piggy_defined;
Expand All @@ -31,7 +32,7 @@ public function map(stdClass $rewardAttribute): RewardAttribute
$rewardAttribute->label,
$rewardAttribute->description,
$rewardAttribute->type,
$rewardAttribute->field_type ?? null,
$fieldType,
$isSoftReadOnly,
$isHardReadOnly,
$isPiggyDefined,
Expand Down
12 changes: 6 additions & 6 deletions src/Models/Contacts/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ class Attribute
*/
public $options;

public function __construct(string $name, string $label, string $type, ?string $fieldType = null, ?string $description = null, ?bool $isSoftReadOnly = null, ?bool $isHardReadOnly = null, ?bool $isPiggyDefined = null, ?array $options = null)
public function __construct(string $name, string $label, string $type, ?string $fieldType, ?string $description = null, ?bool $isSoftReadOnly = null, ?bool $isHardReadOnly = null, ?bool $isPiggyDefined = null, ?array $options = null)
{
$this->name = $name;
$this->label = $label;
$this->type = $type;
$this->fieldType = $fieldType ?? "";
$this->fieldType = $fieldType;
$this->description = $description;
$this->isSoftReadOnly = $isSoftReadOnly;
$this->isHardReadOnly = $isHardReadOnly;
Expand Down Expand Up @@ -121,16 +121,16 @@ public function setType(string $type): void
*/
public function getFieldType(): ?string
{
return $this->fieldType;
return $this->type;
}

/**
* @param string $fieldType
* @param string $type
* @return void
*/
public function setFieldType(string $fieldType): void
public function setFieldType(string $type): void
{
$this->fieldType = $fieldType;
$this->type = $type;
}

/**
Expand Down
122 changes: 61 additions & 61 deletions src/Models/Contacts/Option.php
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
<?php

namespace Piggy\Api\Models\Contacts;

/**
* Class Option
* @package Piggy\Api\Models
*/
class Option
{
/**
* @var string|null
*/
public $label;

/**
* @var string|null
*/
public $value;

public function __construct(?string $label, ?string $value)
{
$this->label = $label;
$this->value = $value;
}

/**
* @return string|null
*/
public function getLabel(): string
{
return $this->label;
}

/**
* @param string|null $label
* @return void
*/
public function setLabel(string $label): void
{
$this->label = $label;
}

/**
* @return string|null
*/
public function getValue(): string
{
return $this->value;
}

/**
* @param string|null $value
* @return void
*/
public function setValue(string $value): void
{
$this->value = $value;
}


}
//
//namespace Piggy\Api\Models\Contacts;
//
///**
// * Class Option
// * @package Piggy\Api\Models
// */
//class Option
//{
// /**
// * @var string|null
// */
// public $label;
//
// /**
// * @var string|null
// */
// public $value;
//
// public function __construct(?string $label, ?string $value)
// {
// $this->label = $label;
// $this->value = $value;
// }
//
// /**
// * @return string|null
// */
// public function getLabel(): string
// {
// return $this->label;
// }
//
// /**
// * @param string|null $label
// * @return void
// */
// public function setLabel(string $label): void
// {
// $this->label = $label;
// }
//
// /**
// * @return string|null
// */
// public function getValue(): string
// {
// return $this->value;
// }
//
// /**
// * @param string|null $value
// * @return void
// */
// public function setValue(string $value): void
// {
// $this->value = $value;
// }
//
//
//}
2 changes: 1 addition & 1 deletion src/Models/Loyalty/RewardAttributes/RewardAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class RewardAttribute
*/
public $placeholder;

public function __construct(string $name, string $label, string $description, string $dataType, ?string $fieldType = null, ?bool $isSoftReadOnly = null, ?bool $isHardReadOnly = null, ?bool $isPiggyDefined = null, ?array $options = null, ?string $placeholder = null)
public function __construct(string $name, string $label, string $description, string $dataType, ?string $fieldType, ?bool $isSoftReadOnly = null, ?bool $isHardReadOnly = null, ?bool $isPiggyDefined = null, ?array $options = null, ?string $placeholder = null)
{
$this->name = $name;
$this->label = $label;
Expand Down
6 changes: 2 additions & 4 deletions src/Resources/OAuth/Contacts/ContactAttributesResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ContactAttributesResource extends BaseResource
*/
public function list(): array
{
$response = $this->client->get($this->resourceUri, []); // todo make it work with list here and then try to remove it
$response = $this->client->get($this->resourceUri, []);

$mapper = new AttributesMapper();

Expand All @@ -37,20 +37,18 @@ public function list(): array
* @param string $name
* @param string $label
* @param string $type
* @param null|string $fieldType
* @param null|string $description
* @param array|null $options
* @return Attribute
* @throws PiggyRequestException
* @throws \Exception
*/
public function create(string $name, string $label, string $type, ?string $fieldType = null, ?string $description = null, ?array $options = null): Attribute
public function create(string $name, string $label, string $type, ?string $description = null, ?array $options = null): Attribute
{
$contactAttributes = [
"name" => $name,
"label" => $label,
"data_type" => $type,
"field_type" => $fieldType,
"description" => $description,
"options" => $options
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ public function list(int $page = 1, int $limit = 30): array
* @param string $label
* @param null|string $description
* @param string $dataType
* @param null|string $fieldType,
* @param null|array $options
* @param null|string $placeholder
* @return RewardAttribute
* @throws PiggyRequestException
*/
public function create(string $name, string $label, string $description, string $dataType, ?string $fieldType = null, ?array $options = null, ?string $placeholder = null ): RewardAttribute
public function create(string $name, string $label, string $description, string $dataType, ?array $options = null, ?string $placeholder = null ): RewardAttribute
{
$rewardAttributes = [
"name" => $name,
Expand Down
2 changes: 0 additions & 2 deletions src/Resources/OAuth/Loyalty/Tokens/LoyaltyTokenResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public function create(string $version, string $shopId, string $uniqueId, ?int $

$response = $this->client->post($this->resourceUri, $inputValues);

// var_dump($response->getData());

return $response->getData();
}

Expand Down
13 changes: 3 additions & 10 deletions tests/OAuth/ContactAttributes/ContactAttributesResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public function it_returns_a_list_of_contact_attributes()
"name" => "asdf",
"label" => "some_label",
"type" => "text",
"field_type" => "text",
"is_soft_read_only" => false,
"is_hard_read_only" => false,
"is_piggy_defined" => true,
Expand All @@ -29,7 +28,6 @@ public function it_returns_a_list_of_contact_attributes()
$this->assertEquals("asdf", $contactAttributes[0]->getName());
$this->assertEquals("some_label", $contactAttributes[0]->getLabel());
$this->assertEquals("text", $contactAttributes[0]->getType());
$this->assertEquals("text", $contactAttributes[0]->getFieldType());
$this->assertEquals(false, $contactAttributes[0]->getIsSoftReadOnly());
$this->assertEquals(false, $contactAttributes[0]->getIsHardReadOnly());
$this->assertEquals(true, $contactAttributes[0]->getIsPiggyDefined());
Expand All @@ -46,7 +44,6 @@ public function it_returns_a_list_of_contact_attributes_with_options()
"name" => "another_first_name",
"label" => "another_label",
"type" => "select",
"field_type" => "select",
"description" => "another_description",
"is_soft_read_only" => true,
"is_hard_read_only" => true,
Expand All @@ -61,12 +58,9 @@ public function it_returns_a_list_of_contact_attributes_with_options()

$contactAttributes = $this->mockedClient->contactAttributes->list();

var_dump($contactAttributes);

$this->assertEquals("another_first_name", $contactAttributes[0]->getName());
$this->assertEquals("another_label", $contactAttributes[0]->getLabel());
$this->assertEquals("select", $contactAttributes[0]->getType());
$this->assertEquals("select", $contactAttributes[0]->getFieldType());
$this->assertEquals("another_description", $contactAttributes[0]->getDescription());
$this->assertEquals(true, $contactAttributes[0]->getIsSoftReadOnly());
$this->assertEquals(true, $contactAttributes[0]->getIsHardReadOnly());
Expand Down Expand Up @@ -111,7 +105,7 @@ public function it_creates_a_phone_number_contact_attribute()
]
);

$contactAttribute = $this->mockedClient->contactAttributes->create('some_phone_number', 'some_label_for_phone_number', 'phone', null, 'some_description');
$contactAttribute = $this->mockedClient->contactAttributes->create('some_phone_number', 'some_label_for_phone_number', 'phone', 'some_description');

$this->assertEquals("some_phone_number", $contactAttribute->getName());
$this->assertEquals("some_label_for_phone_number", $contactAttribute->getLabel());
Expand All @@ -130,12 +124,11 @@ public function it_creates_a_license_plate_contact_attribute()
"name" => "henkie_name",
"label" => "henkie_label",
"type" => "license_plate",
"field_type" => null,
"description" => 'henkie description',
]
);

$contactAttribute = $this->mockedClient->contactAttributes->create('henkie_name', 'henkie_label', 'license_plate', null, 'henkie description');
$contactAttribute = $this->mockedClient->contactAttributes->create('henkie_name', 'henkie_label', 'license_plate', 'henkie description');

$this->assertEquals("henkie_name", $contactAttribute->getName());
$this->assertEquals("henkie_label", $contactAttribute->getLabel());
Expand Down Expand Up @@ -163,7 +156,7 @@ public function it_creates_a_multi_select_contact_attribute()
]
);

$contactAttribute = $this->mockedClient->contactAttributes->create('pietje_name', 'pietje_label', 'multi_select', null, 'pietje_description', [["name" => "some_option_label", "value" => "3"], ["name" => 'some_second_option_label', "value" => "4"]]);
$contactAttribute = $this->mockedClient->contactAttributes->create('pietje_name', 'pietje_label', 'multi_select', 'pietje_description', [["name" => "some_option_label", "value" => "3"], ["name" => 'some_second_option_label', "value" => "4"]]);

$this->assertEquals("pietje_name", $contactAttribute->getName());
$this->assertEquals("pietje_label", $contactAttribute->getLabel());
Expand Down
1 change: 0 additions & 1 deletion tests/OAuth/Contacts/ContactsResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public function it_gets_a_contact()
"label" => "Nombre",
"description" => "Voornaam",
"type" => "text",
"field_type" => "text",
"is_soft_read_only" => false,
"is_hard_read_only" => false,
"is_piggy_defined" => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public function it_returns_a_list_of_reward_attributes_with_options()
"label" => "another_label",
"description" => "another_description",
"type" => "select",
"field_type" => "select",
"is_soft_read_only" => true,
"is_hard_read_only" => true,
"is_piggy_defined" => false,
Expand All @@ -70,7 +69,6 @@ public function it_returns_a_list_of_reward_attributes_with_options()
$this->assertEquals("another_label", $rewardAttributes[0]->getLabel());
$this->assertEquals("another_description", $rewardAttributes[0]->getDescription());
$this->assertEquals("select", $rewardAttributes[0]->getType());
$this->assertEquals("select", $rewardAttributes[0]->getFieldType());
$this->assertEquals(true, $rewardAttributes[0]->getIsSoftReadOnly());
$this->assertEquals(true, $rewardAttributes[0]->getIsHardReadOnly());
$this->assertEquals(false, $rewardAttributes[0]->getIsPiggyDefined());
Expand All @@ -92,7 +90,6 @@ public function it_returns_a_list_of_reward_attributes_with_options_multi_select
"label" => "another_label",
"description" => "another_description",
"type" => "multi_select",
"field_type" => "multi_select",
"is_soft_read_only" => true,
"is_hard_read_only" => true,
"is_piggy_defined" => false,
Expand All @@ -113,7 +110,6 @@ public function it_returns_a_list_of_reward_attributes_with_options_multi_select
$this->assertEquals("another_label", $rewardAttributes[0]->getLabel());
$this->assertEquals("another_description", $rewardAttributes[0]->getDescription());
$this->assertEquals("multi_select", $rewardAttributes[0]->getType());
$this->assertEquals("multi_select", $rewardAttributes[0]->getFieldType());
$this->assertEquals(true, $rewardAttributes[0]->getIsSoftReadOnly());
$this->assertEquals(true, $rewardAttributes[0]->getIsHardReadOnly());
$this->assertEquals(false, $rewardAttributes[0]->getIsPiggyDefined());
Expand Down Expand Up @@ -211,7 +207,6 @@ public function it_creates_a_reward_attribute_with_select_type_and_description()
"Province",
"Please select the province you're living in",
"select",
"select",
[["label" => "Noord-Holland", "value" => 'noord_holland'],
["label" => "Zuid-Holland", "value" => "zuid_holland"],
["label" => "Zeeland", "value" => "zeeland"],
Expand Down

0 comments on commit cfdf312

Please sign in to comment.