Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed image uploading. Both URL and a file #29

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/Client/Image/ImageFileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,22 @@ public function __construct(string $filename)
{
$this->validateFile($filename);
$this->imageFormat = self::ALLOWED_FILE_TYPES[exif_imagetype($filename)];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add ext-exif to composer.json as a required extension? i've missed this.

$this->imageData = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode(file_get_contents($filename)));
$this->imageData = $this->byteArray($filename);
}

/**
* Generate byte array similar to the one in C# and Java
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove unnecessary comment. It does not add any value.

*/
public function byteArray(string $filename): array
{
$data = file_get_contents($filename);
$array = array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure the code style is inline with the other files:
(Short array syntax)

run make cs-fix to fix automatically

foreach(str_split($data) as $char)
{
array_push($array, ord($char));
}

return $array;
}

public function getPostBodyData(): string
Expand Down
17 changes: 15 additions & 2 deletions src/Client/Image/ImageUrlUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,30 @@

final class ImageUrlUpload implements ImageUploadRequestInterface
{
const ALLOWED_FILE_TYPES = [
'gif' => 'GIF',
'jpeg' => 'JPEG',
'jpg' => 'JPEG',
'png' => 'PNG',
'bmp' => 'BMP',
];

private $imageUrl;
private $imageFormat;

public function __construct(string $imageUrl)
{
$this->imageUrl = $imageUrl;
$this->imageUrl = $imageUrl;
$this->imageFormat = self::ALLOWED_FILE_TYPES[
strtolower(array_values(array_slice(explode('.', $imageUrl), -1))[0])
];
}

public function getPostBodyData(): string
{
$data = [
'imageUrl' => $this->imageUrl
'imageFormat' => $this->imageFormat,
'imageUrl' => $this->imageUrl
];

return json_encode($data);
Expand Down
4 changes: 2 additions & 2 deletions src/Client/ImageClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

final class ImageClient
{
const BASE_URL = 'https://image.izettle.com/organizations/%s';
const BASE_URL = 'https://image.izettle.com/v2/images/organizations/%s';
const POST_IMAGE = self::BASE_URL . '/products';

private $client;
Expand All @@ -25,7 +25,7 @@ public function __construct(
ImageBuilderInterface $imageBuilder
) {
$this->client = $client;
$this->organizationUuid = (string) $organizationUuid;
$this->organizationUuid = $organizationUuid ? (string) $organizationUuid : 'self';
$this->imageBuilder = $imageBuilder;
}

Expand Down
15 changes: 14 additions & 1 deletion tests/Unit/Client/Image/ImageFileUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,27 @@
*/
final class ImageFileUploadTest extends TestCase
{

public function byteArray(string $filename): array
{
$data = file_get_contents($filename);
$array = array();
foreach(str_split($data) as $char)
{
array_push($array, ord($char));
}

return $array;
}

/**
* @test
*/
public function productImageUpload_withImage(): void
{
$file = dirname(__FILE__) . '/files/50x50-good.png';
$imageFormat = 'PNG';
$imageData = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode(file_get_contents($file)));
$imageData = $this->byteArray($file);
$productImageUpload = new ImageFileUpload($file);

self::assertInstanceOf(ImageFileUpload::class, $productImageUpload);
Expand Down
17 changes: 15 additions & 2 deletions tests/Unit/Client/Image/ImageUrlUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,25 @@ final class ImageUrlUploadTest extends TestCase
*/
public function productImageUpload_withURL(): void
{
$imageUrl = 'https://example.com/image.png';
$allowedImageType = [
'gif' => 'GIF',
'jpeg' => 'JPEG',
'jpg' => 'JPEG',
'png' => 'PNG',
'bmp' => 'BMP',
];

$imageUrl = 'https://example.com/image.png';
$imageFormat = $allowedImageType[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure the code style is inline with the other files:
(alignment equal signs)

run make cs-fix to fix automatically

strtolower(array_values(array_slice(explode('.', $imageUrl), -1))[0])
];

$productImageUpload = new ImageUrlUpload($imageUrl);

self::assertInstanceOf(ImageUrlUpload::class, $productImageUpload);
self::assertAttributeEquals($imageFormat, 'imageFormat', $productImageUpload);
self::assertAttributeEquals($imageUrl, 'imageUrl', $productImageUpload);
self::assertSame(['imageUrl' => $imageUrl], json_decode($productImageUpload->getPostBodyData(), true));
// self::assertSame(['imageUrl' => $imageUrl], json_decode($productImageUpload->getPostBodyData(), true));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove dead code

Suggested change
// self::assertSame(['imageUrl' => $imageUrl], json_decode($productImageUpload->getPostBodyData(), true));

self::assertSame(['imageFormat' => $imageFormat, 'imageData' => $imageData], json_decode($productImageUpload->getPostBodyData(), true));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should fix this test

Suggested change
self::assertSame(['imageFormat' => $imageFormat, 'imageData' => $imageData], json_decode($productImageUpload->getPostBodyData(), true));
self::assertSame(['imageFormat' => $imageFormat, 'imageUrl' => $imageUrl], json_decode($productImageUpload->getPostBodyData(), true));

}
}