Skip to content

Commit 77ffa10

Browse files
committed
Merge pull request #35 from jdecool/twig-image-url
Add imageUrl Twig filter
2 parents a8978cf + 9846349 commit 77ffa10

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

Tests/Units/Twig/Extension/FileManagementExtension.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,36 @@ public function testFileUrl()
134134
;
135135
}
136136

137+
public function testImageUrl()
138+
{
139+
$this
140+
->if($testedClass = $this->createTestedClassInstance())
141+
->given(
142+
$myEntity = $this->createEntityMock(array(
143+
'nullPhotoFilename' => null,
144+
'photoFilename' => '/my-photo.png',
145+
'photoWithFormatFilename' => '/picture-{-imgformat-}.jpg',
146+
))
147+
)
148+
->then
149+
->variable($testedClass->imageUrl($myEntity, 'nullPhoto', 'mini', false))
150+
->isNull()
151+
->string($testedClass->imageUrl($myEntity, 'photo', 'mini', false))
152+
->isEqualTo('http://localhost/mydir/my-photo.png')
153+
->string($testedClass->imageUrl($myEntity, 'photoWithFormat', 'mini', false))
154+
->isEqualTo('http://localhost/mydir/picture-mini.jpg')
155+
->string($testedClass->imageUrl($myEntity, 'photoWithFormat', 'normal', false))
156+
->isEqualTo('http://localhost/mydir/picture-normal.jpg')
157+
->string($testedClass->imageUrl($myEntity, 'photo', 'mini'))
158+
->match('#^http:\/\/localhost\/mydir\/my-photo.png\?v=\d+$#')
159+
->exception(function() use ($testedClass, $myEntity) {
160+
$testedClass->imageUrl($myEntity, 'undefinedField', 'photo');
161+
})
162+
->isInstanceOf('Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException')
163+
->hasCode(0)
164+
;
165+
}
166+
137167
private function createTestedClassInstance($webdir = '/mydir')
138168
{
139169
$controller = new \mageekguy\atoum\mock\controller();

Twig/Extension/FileManagementExtension.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function getFilters()
2424
'filepath' => new \Twig_Filter_Method($this, 'filepath'),
2525
'imagepath' => new \Twig_Filter_Method($this, 'imagepath'),
2626
'fileUrl' => new \Twig_Filter_Method($this, 'fileUrl'),
27+
'imageUrl' => new \Twig_Filter_Method($this, 'imageUrl'),
2728
);
2829
}
2930

@@ -59,6 +60,24 @@ public function imagepath($entity, $propertyName, $format, $updateCache = true)
5960
public function fileUrl($entity, $propertyName)
6061
{
6162
$path = $this->filepath($entity, $propertyName);
63+
64+
return $this->getUrl($path);
65+
}
66+
67+
public function imageUrl($entity, $propertyName, $format, $updateCache = true)
68+
{
69+
$path = $this->imagepath($entity, $propertyName, $format, $updateCache);
70+
71+
return $this->getUrl($path);
72+
}
73+
74+
public function getName()
75+
{
76+
return 'twigextension';
77+
}
78+
79+
private function getUrl($path)
80+
{
6281
if (empty($path)) {
6382
return null;
6483
}
@@ -69,10 +88,5 @@ public function fileUrl($entity, $propertyName)
6988
$path
7089
);
7190
}
72-
73-
public function getName()
74-
{
75-
return 'twigextension';
76-
}
7791
}
7892

0 commit comments

Comments
 (0)