-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #920 from robfrawley/feature-flip-filter
Add Flip Filter
- Loading branch information
Showing
8 changed files
with
506 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the `liip/LiipImagineBundle` project. | ||
* | ||
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors | ||
* | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Liip\ImagineBundle\Imagine\Filter\Loader; | ||
|
||
use Imagine\Image\ImageInterface; | ||
use Liip\ImagineBundle\Utility\OptionsResolver\OptionsResolver; | ||
use Liip\ImagineBundle\Exception\InvalidArgumentException; | ||
use Symfony\Component\OptionsResolver\Options; | ||
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface; | ||
|
||
class FlipFilterLoader implements LoaderInterface | ||
{ | ||
/** | ||
* @param ImageInterface $image | ||
* @param array $options | ||
* | ||
* @return ImageInterface | ||
*/ | ||
public function load(ImageInterface $image, array $options = array()) | ||
{ | ||
$options = $this->sanitizeOptions($options); | ||
|
||
return $options['axis'] === 'x' ? $image->flipHorizontally() : $image->flipVertically(); | ||
} | ||
|
||
/** | ||
* @param array $options | ||
* | ||
* @return array | ||
*/ | ||
private function sanitizeOptions(array $options) | ||
{ | ||
$resolver = new OptionsResolver(); | ||
$resolver->setDefault('axis', 'x'); | ||
$resolver->setAllowedValues('axis', array('x', 'horizontal', 'y', 'vertical')); | ||
$resolver->setNormalizer('axis', function (Options $options, $value) { | ||
return $value === 'horizontal' ? 'x' : ($value === 'vertical' ? 'y' : $value); | ||
}); | ||
|
||
try { | ||
return $resolver->resolve($options); | ||
} catch (ExceptionInterface $e) { | ||
throw new InvalidArgumentException('The "axis" option must be set to "x", "horizontal", "y", or "vertical".'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
Tests/Functional/Imagine/Filter/Loader/FlipFilterLoaderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the `liip/LiipImagineBundle` project. | ||
* | ||
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors | ||
* | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Liip\ImagineBundle\Tests\Functional\Imagine\Filter\Loader; | ||
|
||
use Liip\ImagineBundle\Tests\Functional\AbstractWebTestCase; | ||
|
||
/** | ||
* @covers \Liip\ImagineBundle\Imagine\Filter\Loader\FlipFilterLoader | ||
*/ | ||
class FlipFilterLoaderTest extends AbstractWebTestCase | ||
{ | ||
public function testCouldBeGetFromContainerAsService() | ||
{ | ||
$this->createClient(); | ||
$this->assertInstanceOf( | ||
'\Liip\ImagineBundle\Imagine\Filter\Loader\FlipFilterLoader', | ||
self::$kernel->getContainer()->get('liip_imagine.filter.loader.flip') | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the `liip/LiipImagineBundle` project. | ||
* | ||
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors | ||
* | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Liip\ImagineBundle\Tests\Filter; | ||
|
||
use Liip\ImagineBundle\Imagine\Filter\Loader\FlipFilterLoader; | ||
use Liip\ImagineBundle\Tests\AbstractTest; | ||
|
||
/** | ||
* @covers \Liip\ImagineBundle\Imagine\Filter\Loader\FlipFilterLoader | ||
*/ | ||
class FlipFilterLoaderTest extends AbstractTest | ||
{ | ||
/** | ||
* @return array | ||
*/ | ||
public static function provideLoadWithAxisXOptionData() | ||
{ | ||
return array( | ||
array('x'), | ||
array('horizontal'), | ||
); | ||
} | ||
|
||
/** | ||
* @param string $axis | ||
* | ||
* @dataProvider provideLoadWithAxisXOptionData | ||
*/ | ||
public function testLoadWithAxisXOption($axis) | ||
{ | ||
$image = $this->getImageInterfaceMock(); | ||
$image->expects($this->once()) | ||
->method('flipHorizontally') | ||
->willReturn($image); | ||
|
||
$this->createFlipFilterLoaderInstance()->load($image, array('axis' => $axis)); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public static function provideLoadWithAxisYOptionData() | ||
{ | ||
return array( | ||
array('y'), | ||
array('vertical'), | ||
); | ||
} | ||
|
||
/** | ||
* @param string $axis | ||
* | ||
* @dataProvider provideLoadWithAxisYOptionData | ||
*/ | ||
public function testLoadWithAxisYOption($axis) | ||
{ | ||
$image = $this->getImageInterfaceMock(); | ||
$image->expects($this->once()) | ||
->method('flipVertically') | ||
->willReturn($image); | ||
|
||
$this->createFlipFilterLoaderInstance()->load($image, array('axis' => $axis)); | ||
} | ||
|
||
/** | ||
* @expectedException \Liip\ImagineBundle\Exception\InvalidArgumentException | ||
* @expectedExceptionMessage The "axis" option must be set to "x", "horizontal", "y", or "vertical". | ||
*/ | ||
public function testThrowsOnInvalidOptions() | ||
{ | ||
$loader = new FlipFilterLoader(); | ||
$loader->load($this->getImageInterfaceMock(), array( | ||
'axis' => 'invalid', | ||
)); | ||
} | ||
|
||
/** | ||
* @return FlipFilterLoader | ||
*/ | ||
private function createFlipFilterLoaderInstance() | ||
{ | ||
return new FlipFilterLoader(); | ||
} | ||
} |
Oops, something went wrong.