From a8ab0abcf5df6993b36e03518cf45960e79556e5 Mon Sep 17 00:00:00 2001 From: Juan Millan Date: Fri, 22 Nov 2024 17:36:23 +0100 Subject: [PATCH 1/2] add process builder --- .gitignore | 2 + README.md | 10 +++++ src/Image.php | 22 ++++++++++ src/ProcessBuilder.php | 85 ++++++++++++++++++++++++++++++++++++ src/Resized.php | 9 ++++ tests/ProcessBuilderTest.php | 57 ++++++++++++++++++++++++ tests/ResizedTest.php | 16 +++++++ 7 files changed, 201 insertions(+) create mode 100644 src/Image.php create mode 100644 src/ProcessBuilder.php create mode 100644 tests/ProcessBuilderTest.php diff --git a/.gitignore b/.gitignore index f02a2f8..1e1645c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ +.idea build composer.lock +.phpunit.result.cache docs vendor diff --git a/README.md b/README.md index 39bfc7f..768e883 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,16 @@ $ composer require square1/resized $img = $resized->process('http://www.example.com/some-image.jpg', '100', '100', 'This is a title'); ``` +Using the builder you can omit parameters and is more flexible: + +```php +$img = $resized->builder('http://www.example.com/some-image.jpg') + ->width(100) + ->quality(90) + ->output('webp') + ->url(); +``` + ## Testing ``` bash diff --git a/src/Image.php b/src/Image.php new file mode 100644 index 0000000..a049631 --- /dev/null +++ b/src/Image.php @@ -0,0 +1,22 @@ +src = $src; + $this->width = $width; + $this->height = $height; + $this->title = $title; + $this->options = $options; + } + +} diff --git a/src/ProcessBuilder.php b/src/ProcessBuilder.php new file mode 100644 index 0000000..a30ba24 --- /dev/null +++ b/src/ProcessBuilder.php @@ -0,0 +1,85 @@ +resized = $resized; + } + + public static function new(Resized $resized) : self + { + return new self($resized); + } + + public function getImage() : Image + { + return $this->image; + } + + public function process($url) : self + { + $this->image = new Image($url); + return $this; + } + + public function width($width) : self + { + $this->image->width = $width; + return $this; + } + + public function height($height) : self + { + $this->image->height = $height; + return $this; + } + + public function title($title) : self + { + $this->image->title = $title; + return $this; + } + + public function output($output) : self + { + $this->image->options['output'] = $output; + return $this; + } + + public function quality($quality) : self + { + $this->image->options['quality'] = $quality; + return $this; + } + + public function options(array $options) : self + { + $this->image->options = array_merge( + $this->image->options, + $options + ); + + return $this; + } + + public function url() : string + { + return $this->resized->process( + $this->image->src, + $this->image->width, + $this->image->height, + $this->image->title, + $this->image->options + ); + } + + + + +} diff --git a/src/Resized.php b/src/Resized.php index b37d7f6..a0080cc 100644 --- a/src/Resized.php +++ b/src/Resized.php @@ -146,6 +146,15 @@ public function process($url, $width = '', $height = '', $title = '', $options = return implode('/', $fullUrl); } + /** + * @param $url + * @return ProcessBuilder + */ + public function builder($url) : ProcessBuilder + { + return ProcessBuilder::new($this)->process($url); + } + /** * Get seo slug and file extension diff --git a/tests/ProcessBuilderTest.php b/tests/ProcessBuilderTest.php new file mode 100644 index 0000000..beee152 --- /dev/null +++ b/tests/ProcessBuilderTest.php @@ -0,0 +1,57 @@ +resized = new Resized('key', 'secret-d0be2dc421be4fcd0172e5afceea3970e2f3d940'); + } + + public function testProcessBuilder() + { + $processBuilder = $this->resized->builder('http://www.example.com/some-image-to-resize.jpg'); + + $this->assertInstanceOf(ProcessBuilder::class, $processBuilder); + } + + public function testProcessBuilderFull() + { + $partialImage = $this->resized->builder('http://www.example.com/some-image-to-resize.jpg') + ->width(100) + ->height(100) + ->output('webp') + ->title('A nice title') + ->options(['quality' => 80]) + ->getImage(); + + $this->assertEquals($partialImage->src, 'http://www.example.com/some-image-to-resize.jpg'); + $this->assertEquals($partialImage->width, 100); + $this->assertEquals($partialImage->height, 100); + $this->assertEquals($partialImage->options, ['output' => 'webp', 'quality' => 80]); + $this->assertEquals($partialImage->title, 'A nice title'); + } + + public function testProcessBuilderOptionsWillBeMerge() + { + $partialImage = $this->resized->builder('http://www.example.com/some-image-to-resize.jpg') + ->width(100) + ->height(100) + ->quality(70) + ->output('webp') + ->title('A nice title') + ->options(['quality' => 80]) + ->options(['output' => 'jpeg']) + ->getImage(); + + $this->assertEquals($partialImage->options, ['output' => 'jpeg', 'quality' => 80]); + } + +} diff --git a/tests/ResizedTest.php b/tests/ResizedTest.php index 3a78b94..ffb5c66 100644 --- a/tests/ResizedTest.php +++ b/tests/ResizedTest.php @@ -150,4 +150,20 @@ public function testNoConstraintParams() $this->assertEquals($img, 'https://img.resized.co/key/eyJkYXRhIjoie1widXJsXCI6XCJodHRwOlxcXC9cXFwvd3d3LmV4YW1wbGUuY29tXFxcL3NvbWUtaW1hZ2UtdG8tcmVzaXplLmpwZ1wiLFwid2lkdGhcIjpcIlwiLFwiaGVpZ2h0XCI6XCJcIixcImRlZmF1bHRcIjpcImh0dHA6XFxcL1xcXC93d3cuZXhhbXBsZS5jb21cXFwvbm8taW1hZ2UuanBnXCIsXCJvcHRpb25zXCI6W119IiwiaGFzaCI6IjMzMGZhODdhOWFmNGJmNTZiOWI2ODQ5NjAxNTZmMmYwNWRiY2Y0ZTUifQ==/some-image-to-resize.jpg'); } + + public function testProcessBuild() + { + $resized = new Resized('key', 'secret-d0be2dc421be4fcd0172e5afceea3970e2f3d940'); + + $resized->setDefaultImage('http://www.example.com/no-image.jpg'); + $img = $resized->builder('http://www.example.com/some-image-to-resize.jpg') + ->width(100) + ->height(100) + ->output('webp') + ->url(); + + $this->assertEquals($img, 'https://img.resized.co/key/eyJkYXRhIjoie1widXJsXCI6XCJodHRwOlxcXC9cXFwvd3d3LmV4YW1wbGUuY29tXFxcL3NvbWUtaW1hZ2UtdG8tcmVzaXplLmpwZ1wiLFwid2lkdGhcIjoxMDAsXCJoZWlnaHRcIjoxMDAsXCJkZWZhdWx0XCI6XCJodHRwOlxcXC9cXFwvd3d3LmV4YW1wbGUuY29tXFxcL25vLWltYWdlLmpwZ1wiLFwib3B0aW9uc1wiOntcIm91dHB1dFwiOlwid2VicFwifX0iLCJoYXNoIjoiOWE5MWM4NWM2NzJlZTY4YzU3OTExYjEwYjA3N2M1ZTRlNjcwN2ZiYSJ9/some-image-to-resize.jpg'); + + } + } From 7488fae37158f54174c0aa91b320837c5d3984bf Mon Sep 17 00:00:00 2001 From: Juan Millan Date: Wed, 4 Dec 2024 12:45:03 +0100 Subject: [PATCH 2/2] simplifying the builder --- src/Builder.php | 82 ++++++++++++++++++++++++++++++++++ src/Image.php | 22 ---------- src/ProcessBuilder.php | 85 ------------------------------------ src/Resized.php | 6 +-- tests/BuilderTest.php | 57 ++++++++++++++++++++++++ tests/ProcessBuilderTest.php | 57 ------------------------ tests/ResizedTest.php | 2 +- 7 files changed, 143 insertions(+), 168 deletions(-) create mode 100644 src/Builder.php delete mode 100644 src/Image.php delete mode 100644 src/ProcessBuilder.php create mode 100644 tests/BuilderTest.php delete mode 100644 tests/ProcessBuilderTest.php diff --git a/src/Builder.php b/src/Builder.php new file mode 100644 index 0000000..d2a8225 --- /dev/null +++ b/src/Builder.php @@ -0,0 +1,82 @@ +resized = $resized; + $this->params['url'] = $url; + } + + public function getParams() : array + { + return $this->params; + } + + public function width($width): self + { + $this->params['width'] = $width; + return $this; + } + + public function height($height): self + { + $this->params['height'] = $height; + return $this; + } + + public function title($title): self + { + $this->params['title'] = $title; + return $this; + } + + public function output($output): self + { + $this->params['options']['output'] = $output; + return $this; + } + + public function quality($quality): self + { + $this->params['options']['quality'] = $quality; + return $this; + } + + public function options(array $options): self + { + $this->params['options'] = $this->mergeOptions($options); + return $this; + } + + private function mergeOptions(array $options) : array + { + return array_merge( + $this->params['options'], + $options + ); + + } + + public function url(): string + { + return $this->resized->process( + $this->params['url'], + $this->params['width'] ?? '', + $this->params['height'] ?? '', + $this->params['title'] ?? '', + $this->params['options'] ?? [] + ); + } + + +} diff --git a/src/Image.php b/src/Image.php deleted file mode 100644 index a049631..0000000 --- a/src/Image.php +++ /dev/null @@ -1,22 +0,0 @@ -src = $src; - $this->width = $width; - $this->height = $height; - $this->title = $title; - $this->options = $options; - } - -} diff --git a/src/ProcessBuilder.php b/src/ProcessBuilder.php deleted file mode 100644 index a30ba24..0000000 --- a/src/ProcessBuilder.php +++ /dev/null @@ -1,85 +0,0 @@ -resized = $resized; - } - - public static function new(Resized $resized) : self - { - return new self($resized); - } - - public function getImage() : Image - { - return $this->image; - } - - public function process($url) : self - { - $this->image = new Image($url); - return $this; - } - - public function width($width) : self - { - $this->image->width = $width; - return $this; - } - - public function height($height) : self - { - $this->image->height = $height; - return $this; - } - - public function title($title) : self - { - $this->image->title = $title; - return $this; - } - - public function output($output) : self - { - $this->image->options['output'] = $output; - return $this; - } - - public function quality($quality) : self - { - $this->image->options['quality'] = $quality; - return $this; - } - - public function options(array $options) : self - { - $this->image->options = array_merge( - $this->image->options, - $options - ); - - return $this; - } - - public function url() : string - { - return $this->resized->process( - $this->image->src, - $this->image->width, - $this->image->height, - $this->image->title, - $this->image->options - ); - } - - - - -} diff --git a/src/Resized.php b/src/Resized.php index a0080cc..bd5e969 100644 --- a/src/Resized.php +++ b/src/Resized.php @@ -148,11 +148,11 @@ public function process($url, $width = '', $height = '', $title = '', $options = /** * @param $url - * @return ProcessBuilder + * @return Builder */ - public function builder($url) : ProcessBuilder + public function src($url) : Builder { - return ProcessBuilder::new($this)->process($url); + return new Builder($this, $url); } diff --git a/tests/BuilderTest.php b/tests/BuilderTest.php new file mode 100644 index 0000000..eacf506 --- /dev/null +++ b/tests/BuilderTest.php @@ -0,0 +1,57 @@ +resized = new Resized('key', 'secret-d0be2dc421be4fcd0172e5afceea3970e2f3d940'); + } + + public function testProcessBuilder() + { + $processBuilder = $this->resized->src('http://www.example.com/some-image-to-resize.jpg'); + + $this->assertInstanceOf(Builder::class, $processBuilder); + } + + public function testProcessBuilderFull() + { + $params = $this->resized->src('http://www.example.com/some-image-to-resize.jpg') + ->width(100) + ->height(100) + ->output('webp') + ->title('A nice title') + ->options(['quality' => 80]) + ->getParams(); + + $this->assertEquals($params['url'], 'http://www.example.com/some-image-to-resize.jpg'); + $this->assertEquals($params['width'], 100); + $this->assertEquals($params['height'], 100); + $this->assertEquals($params['options'], ['output' => 'webp', 'quality' => 80]); + $this->assertEquals($params['title'], 'A nice title'); + } + + public function testProcessBuilderOptionsWillBeMerge() + { + $partialImage = $this->resized->src('http://www.example.com/some-image-to-resize.jpg') + ->width(100) + ->height(100) + ->quality(70) + ->output('webp') + ->title('A nice title') + ->options(['quality' => 80]) + ->options(['output' => 'jpeg']) + ->getParams(); + + $this->assertEquals($partialImage['options'], ['output' => 'jpeg', 'quality' => 80]); + } + +} diff --git a/tests/ProcessBuilderTest.php b/tests/ProcessBuilderTest.php deleted file mode 100644 index beee152..0000000 --- a/tests/ProcessBuilderTest.php +++ /dev/null @@ -1,57 +0,0 @@ -resized = new Resized('key', 'secret-d0be2dc421be4fcd0172e5afceea3970e2f3d940'); - } - - public function testProcessBuilder() - { - $processBuilder = $this->resized->builder('http://www.example.com/some-image-to-resize.jpg'); - - $this->assertInstanceOf(ProcessBuilder::class, $processBuilder); - } - - public function testProcessBuilderFull() - { - $partialImage = $this->resized->builder('http://www.example.com/some-image-to-resize.jpg') - ->width(100) - ->height(100) - ->output('webp') - ->title('A nice title') - ->options(['quality' => 80]) - ->getImage(); - - $this->assertEquals($partialImage->src, 'http://www.example.com/some-image-to-resize.jpg'); - $this->assertEquals($partialImage->width, 100); - $this->assertEquals($partialImage->height, 100); - $this->assertEquals($partialImage->options, ['output' => 'webp', 'quality' => 80]); - $this->assertEquals($partialImage->title, 'A nice title'); - } - - public function testProcessBuilderOptionsWillBeMerge() - { - $partialImage = $this->resized->builder('http://www.example.com/some-image-to-resize.jpg') - ->width(100) - ->height(100) - ->quality(70) - ->output('webp') - ->title('A nice title') - ->options(['quality' => 80]) - ->options(['output' => 'jpeg']) - ->getImage(); - - $this->assertEquals($partialImage->options, ['output' => 'jpeg', 'quality' => 80]); - } - -} diff --git a/tests/ResizedTest.php b/tests/ResizedTest.php index ffb5c66..1955aa2 100644 --- a/tests/ResizedTest.php +++ b/tests/ResizedTest.php @@ -156,7 +156,7 @@ public function testProcessBuild() $resized = new Resized('key', 'secret-d0be2dc421be4fcd0172e5afceea3970e2f3d940'); $resized->setDefaultImage('http://www.example.com/no-image.jpg'); - $img = $resized->builder('http://www.example.com/some-image-to-resize.jpg') + $img = $resized->src('http://www.example.com/some-image-to-resize.jpg') ->width(100) ->height(100) ->output('webp')