-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add encode / decode methods in twig template
- Loading branch information
1 parent
0a66044
commit 95f01ee
Showing
3 changed files
with
54 additions
and
0 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Roukmoute\HashidsBundle\Twig; | ||
|
||
use Roukmoute\HashidsBundle\Hashids; | ||
|
||
class HashidsExtension extends \Twig_Extension | ||
{ | ||
/** | ||
* @var Hashids | ||
*/ | ||
private $hashids; | ||
|
||
public function __construct(Hashids $hashids) | ||
{ | ||
$this->hashids = $hashids; | ||
} | ||
|
||
public function getFilters() | ||
{ | ||
return [ | ||
new \Twig_SimpleFilter('hashids_encode', [$this, 'encode']), | ||
new \Twig_SimpleFilter('hashids_decode', [$this, 'decode']), | ||
]; | ||
} | ||
|
||
public function getName() | ||
{ | ||
return 'hashids_extension'; | ||
} | ||
|
||
public function encode($number) | ||
{ | ||
return $this->hashids->encode($number); | ||
} | ||
|
||
public function decode($hash) | ||
{ | ||
return $this->hashids->decode($hash); | ||
} | ||
|
||
} |