PicoTwigExtension.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * Pico's Twig extension to implement additional filters
  4. *
  5. * @author Daniel Rudolf
  6. * @link http://picocms.org
  7. * @license http://opensource.org/licenses/MIT The MIT License
  8. * @version 1.0
  9. */
  10. class PicoTwigExtension extends Twig_Extension
  11. {
  12. /**
  13. * Current instance of Pico
  14. *
  15. * @see PicoTwigExtension::getPico()
  16. * @var Pico
  17. */
  18. private $pico;
  19. /**
  20. * Constructs a new instance of this Twig extension
  21. *
  22. * @param Pico $pico current instance of Pico
  23. */
  24. public function __construct(Pico $pico)
  25. {
  26. $this->pico = $pico;
  27. }
  28. /**
  29. * Returns the extensions instance of Pico
  30. *
  31. * @see Pico
  32. * @return Pico the extensions instance of Pico
  33. */
  34. public function getPico()
  35. {
  36. return $this->pico;
  37. }
  38. /**
  39. * Returns the name of the extension
  40. *
  41. * @see Twig_ExtensionInterface::getName()
  42. * @return string the extension name
  43. */
  44. public function getName()
  45. {
  46. return 'PicoTwigExtension';
  47. }
  48. /**
  49. * Returns the Twig filters markdown, map and sort_by
  50. *
  51. * @see Twig_ExtensionInterface::getFilters()
  52. * @return Twig_SimpleFilter[] array of Pico's Twig filters
  53. */
  54. public function getFilters()
  55. {
  56. return array(
  57. 'markdown' => new Twig_SimpleFilter('markdown', array($this, 'markdownFilter')),
  58. 'map' => new Twig_SimpleFilter('map', array($this, 'mapFilter')),
  59. 'sort_by' => new Twig_SimpleFilter('sort_by', array($this, 'sortByFilter')),
  60. );
  61. }
  62. /**
  63. * Parses a markdown string to HTML
  64. *
  65. * This method is registered as the Twig `markdown` filter. You can use it
  66. * to e.g. parse a meta variable (`{{ meta.description|markdown }}`).
  67. * Don't use it to parse the contents of a page, use the `content` filter
  68. * instead, what ensures the proper preparation of the contents.
  69. *
  70. * @param string $markdown markdown to parse
  71. * @return string parsed HTML
  72. */
  73. public function markdownFilter($markdown)
  74. {
  75. if ($this->getPico()->getParsedown() === null) {
  76. throw new LogicException(
  77. 'Unable to apply Twig "markdown" filter: '
  78. . 'Parsedown instance wasn\'t registered yet'
  79. );
  80. }
  81. return $this->getPico()->getParsedown()->text($markdown);
  82. }
  83. /**
  84. * Returns a array with the values of the given key or key path
  85. *
  86. * This method is registered as the Twig `map` filter. You can use this
  87. * filter to e.g. get all page titles (`{{ pages|map("title") }}`).
  88. *
  89. * @param array|Traversable $var variable to map
  90. * @param mixed $mapKeyPath key to map; either a scalar or a
  91. * array interpreted as key path (i.e. ['foo', 'bar'] will return all
  92. * $item['foo']['bar'] values)
  93. * @return array mapped values
  94. */
  95. public function mapFilter($var, $mapKeyPath)
  96. {
  97. if (!is_array($var) && (!is_object($var) || !($var instanceof Traversable))) {
  98. throw new Twig_Error_Runtime(sprintf(
  99. 'The map filter only works with arrays or "Traversable", got "%s"',
  100. is_object($var) ? get_class($var) : gettype($var)
  101. ));
  102. }
  103. $result = array();
  104. foreach ($var as $key => $value) {
  105. $mapValue = $this->getKeyOfVar($value, $mapKeyPath);
  106. $result[$key] = ($mapValue !== null) ? $mapValue : $value;
  107. }
  108. return $result;
  109. }
  110. /**
  111. * Sorts an array by one of its keys or a arbitrary deep sub-key
  112. *
  113. * This method is registered as the Twig `sort_by` filter. You can use this
  114. * filter to e.g. sort the pages array by a arbitrary meta value. Calling
  115. * `{{ pages|sort_by("meta:nav"|split(":")) }}` returns all pages sorted by
  116. * the meta value `nav`. Please note the `"meta:nav"|split(":")` part of
  117. * the example. The sorting algorithm will never assume equality of two
  118. * values, it will then fall back to the original order. The result is
  119. * always sorted in ascending order, apply Twigs `reverse` filter to
  120. * achieve a descending order.
  121. *
  122. * @param array|Traversable $var variable to sort
  123. * @param mixed $sortKeyPath key to use for sorting; either
  124. * a scalar or a array interpreted as key path (i.e. ['foo', 'bar']
  125. * will sort $var by $item['foo']['bar'])
  126. * @param string $fallback specify what to do with items
  127. * which don't contain the specified sort key; use "bottom" (default)
  128. * to move those items to the end of the sorted array, "top" to rank
  129. * them first, or "keep" to keep the original order of those items
  130. * @return array sorted array
  131. */
  132. public function sortByFilter($var, $sortKeyPath, $fallback = 'bottom')
  133. {
  134. if (is_object($var) && ($var instanceof Traversable)) {
  135. $var = iterator_to_array($var, true);
  136. } elseif (!is_array($var)) {
  137. throw new Twig_Error_Runtime(sprintf(
  138. 'The sort_by filter only works with arrays or "Traversable", got "%s"',
  139. is_object($var) ? get_class($var) : gettype($var)
  140. ));
  141. }
  142. if (($fallback !== 'top') && ($fallback !== 'bottom') && ($fallback !== 'keep')) {
  143. throw new Twig_Error_Runtime('The sort_by filter only supports the "top", "bottom" and "keep" fallbacks');
  144. }
  145. $twigExtension = $this;
  146. $varKeys = array_keys($var);
  147. uksort($var, function ($a, $b) use ($twigExtension, $var, $varKeys, $sortKeyPath, $fallback, &$removeItems) {
  148. $aSortValue = $twigExtension->getKeyOfVar($var[$a], $sortKeyPath);
  149. $aSortValueNull = ($aSortValue === null);
  150. $bSortValue = $twigExtension->getKeyOfVar($var[$b], $sortKeyPath);
  151. $bSortValueNull = ($bSortValue === null);
  152. if ($aSortValueNull xor $bSortValueNull) {
  153. if ($fallback === 'top') {
  154. return ($aSortValueNull - $bSortValueNull) * -1;
  155. } elseif ($fallback === 'bottom') {
  156. return ($aSortValueNull - $bSortValueNull);
  157. }
  158. } elseif (!$aSortValueNull && !$bSortValueNull) {
  159. if ($aSortValue != $bSortValue) {
  160. return ($aSortValue > $bSortValue) ? 1 : -1;
  161. }
  162. }
  163. // never assume equality; fallback to original order
  164. $aIndex = array_search($a, $varKeys);
  165. $bIndex = array_search($b, $varKeys);
  166. return ($aIndex > $bIndex) ? 1 : -1;
  167. });
  168. return $var;
  169. }
  170. /**
  171. * Returns the value of a variable item specified by a scalar key or a
  172. * arbitrary deep sub-key using a key path
  173. *
  174. * @param array|Traversable|ArrayAccess|object $var base variable
  175. * @param mixed $keyPath scalar key or a
  176. * array interpreted as key path (when passing e.g. ['foo', 'bar'],
  177. * the method will return $var['foo']['bar']) specifying the value
  178. * @return mixed the requested
  179. * value or NULL when the given key or key path didn't match
  180. */
  181. public static function getKeyOfVar($var, $keyPath)
  182. {
  183. if (empty($keyPath)) {
  184. return null;
  185. } elseif (!is_array($keyPath)) {
  186. $keyPath = array($keyPath);
  187. }
  188. foreach ($keyPath as $key) {
  189. if (is_object($var)) {
  190. if ($var instanceof ArrayAccess) {
  191. // use ArrayAccess, see below
  192. } elseif ($var instanceof Traversable) {
  193. $var = iterator_to_array($var);
  194. } elseif (isset($var->{$key})) {
  195. $var = $var->{$key};
  196. continue;
  197. } elseif (is_callable(array($var, 'get' . ucfirst($key)))) {
  198. try {
  199. $var = call_user_func(array($var, 'get' . ucfirst($key)));
  200. continue;
  201. } catch (BadMethodCallException $e) {
  202. return null;
  203. }
  204. } else {
  205. return null;
  206. }
  207. } elseif (!is_array($var)) {
  208. return null;
  209. }
  210. if (isset($var[$key])) {
  211. $var = $var[$key];
  212. continue;
  213. }
  214. return null;
  215. }
  216. return $var;
  217. }
  218. }