From b7c21ba06764d26e5093a56ba5f3b59362849cfd Mon Sep 17 00:00:00 2001 From: CodeRaiser Date: Fri, 22 Mar 2019 10:48:57 +0200 Subject: [PATCH 1/2] Fixed image uploading. Both URL and a file --- src/Client/Image/ImageFileUpload.php | 17 ++++++++++++++++- src/Client/Image/ImageUrlUpload.php | 17 +++++++++++++++-- src/Client/ImageClient.php | 4 ++-- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/Client/Image/ImageFileUpload.php b/src/Client/Image/ImageFileUpload.php index 7169f91..0154d50 100644 --- a/src/Client/Image/ImageFileUpload.php +++ b/src/Client/Image/ImageFileUpload.php @@ -31,7 +31,22 @@ public function __construct(string $filename) { $this->validateFile($filename); $this->imageFormat = self::ALLOWED_FILE_TYPES[exif_imagetype($filename)]; - $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 + */ + 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; } public function getPostBodyData(): string diff --git a/src/Client/Image/ImageUrlUpload.php b/src/Client/Image/ImageUrlUpload.php index 0b34fdb..fdd9c47 100644 --- a/src/Client/Image/ImageUrlUpload.php +++ b/src/Client/Image/ImageUrlUpload.php @@ -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); diff --git a/src/Client/ImageClient.php b/src/Client/ImageClient.php index 15af586..35735b3 100644 --- a/src/Client/ImageClient.php +++ b/src/Client/ImageClient.php @@ -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; @@ -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; } From d7b6db7b89dbc7bc6231277fc602cb7627b3dffd Mon Sep 17 00:00:00 2001 From: CodeRaiser Date: Sun, 24 Mar 2019 13:35:11 +0200 Subject: [PATCH 2/2] Updated tests according new image upload mechanism --- tests/Unit/Client/Image/ImageFileUploadTest.php | 15 ++++++++++++++- tests/Unit/Client/Image/ImageUrlUploadTest.php | 17 +++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/tests/Unit/Client/Image/ImageFileUploadTest.php b/tests/Unit/Client/Image/ImageFileUploadTest.php index e34c9f7..711ec46 100644 --- a/tests/Unit/Client/Image/ImageFileUploadTest.php +++ b/tests/Unit/Client/Image/ImageFileUploadTest.php @@ -12,6 +12,19 @@ */ 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 */ @@ -19,7 +32,7 @@ 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); diff --git a/tests/Unit/Client/Image/ImageUrlUploadTest.php b/tests/Unit/Client/Image/ImageUrlUploadTest.php index e3f27d9..3b13914 100644 --- a/tests/Unit/Client/Image/ImageUrlUploadTest.php +++ b/tests/Unit/Client/Image/ImageUrlUploadTest.php @@ -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[ + 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)); + self::assertSame(['imageFormat' => $imageFormat, 'imageData' => $imageData], json_decode($productImageUpload->getPostBodyData(), true)); } }