Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. Here's the base sanitizer:
  2.  
  3. ```php
  4. <?php
  5.  
  6. namespace FooProject\Internal\Sanitizers;
  7.  
  8. abstract class BaseSanitizer
  9. {
  10. /**
  11. * An array of sanitizer methods to be
  12. * executed.
  13. *
  14. * @var array
  15. */
  16. protected $sanitizers = [];
  17.  
  18. /**
  19. * Trigger the sanitization process by
  20. * iterating the sanitizers array and
  21. * mutating our data array.
  22. *
  23. * @param array $data
  24. * @return array
  25. */
  26. public function sanitize($data)
  27. {
  28. // Iterate all of the sanitizer methods.
  29. foreach ($this->sanitizers as $sanitizer) {
  30. $method = 'sanitize'.$sanitizer;
  31. // If the sanitization method exists, call it
  32. // to mutate our data set.
  33. if (method_exists($this, $method)) {
  34. $data = call_user_func([$this, $method], $data);
  35. }
  36. }
  37. return $data;
  38. }
  39. }
  40. ```
  41.  
  42. and here's an example:
  43.  
  44. ```php
  45. <?php
  46.  
  47. namespace FooProject\Internal\Sanitizers;
  48.  
  49. class UsersSanitizer extends BaseSanitizer
  50. {
  51. /**
  52. * An array of sanitizer methods to be
  53. * executed.
  54. *
  55. * @var array
  56. */
  57. protected $sanitizers = ['Email'];
  58.  
  59. /**
  60. * Sanitize an email address.
  61. *
  62. * @param array $data
  63. * @return FooProject\Internal\Sanitizer\UserSanitizer
  64. */
  65. public function sanitizeEmail($data)
  66. {
  67. if (isset($data['email'])) {
  68. $data['email'] = strtolower($data['email']);
  69. }
  70. return $this;
  71. }
  72. }
  73. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement