Utils.php 1.4 KB

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