Utils.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace Grav\Plugin\Admin;
  3. use Grav\Common\Grav;
  4. use Grav\Common\User\Interfaces\UserCollectionInterface;
  5. use Grav\Common\User\Interfaces\UserInterface;
  6. /**
  7. * Admin utils class
  8. *
  9. * @license MIT
  10. */
  11. class Utils
  12. {
  13. /**
  14. * Matches an email to a user
  15. *
  16. * @param string $email
  17. *
  18. * @return UserInterface
  19. */
  20. public static function findUserByEmail(string $email)
  21. {
  22. $grav = Grav::instance();
  23. /** @var UserCollectionInterface $users */
  24. $users = $grav['accounts'];
  25. return $users->find($email, ['email']);
  26. }
  27. /**
  28. * Generates a slug of the given string
  29. *
  30. * @param string $str
  31. * @return string
  32. */
  33. public static function slug(string $str)
  34. {
  35. if (function_exists('transliterator_transliterate')) {
  36. $str = transliterator_transliterate('Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove;', $str);
  37. } else {
  38. $str = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $str);
  39. }
  40. $str = strtolower($str);
  41. $str = preg_replace('/[-\s]+/', '-', $str);
  42. $str = preg_replace('/[^a-z0-9-]/i', '', $str);
  43. $str = trim($str, '-');
  44. return $str;
  45. }
  46. }