PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Aura/Filter/Rule/Trim.php

https://bitbucket.org/harikt/aura.filter
PHP | 84 lines | 31 code | 5 blank | 48 comment | 5 complexity | 0c294e88d9e520ebbcdb882f7543d1fc MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. *
  4. * This file is part of the Aura project for PHP.
  5. *
  6. * @package Aura.Filter
  7. *
  8. * @license http://opensource.org/licenses/bsd-license.php BSD
  9. *
  10. */
  11. namespace Aura\Filter\Rule;
  12. use Aura\Filter\AbstractRule;
  13. /**
  14. *
  15. * Sanitizes a value to a string using trim().
  16. *
  17. * @package Aura.Filter
  18. *
  19. * @license http://opensource.org/licenses/bsd-license.php BSD
  20. *
  21. */
  22. class Trim extends AbstractRule
  23. {
  24. /**
  25. *
  26. * Error message
  27. *
  28. * @var string
  29. */
  30. protected $message = 'FILTER_TRIM';
  31. /**
  32. *
  33. * same as PHP trim()
  34. *
  35. * @var string
  36. */
  37. protected $chars = " \t\n\r\0\x0B";
  38. /**
  39. *
  40. * Is the value already trimmed?
  41. *
  42. * @param string $chars
  43. *
  44. * @return bool True if valid, false if not.
  45. *
  46. */
  47. protected function validate($chars = null)
  48. {
  49. $value = $this->getValue();
  50. if (! is_scalar($value)) {
  51. return false;
  52. }
  53. if (! $chars) {
  54. $chars = $this->chars;
  55. }
  56. return trim($value, $chars) == $value;
  57. }
  58. /**
  59. *
  60. * Trims characters from the beginning and end of the value.
  61. *
  62. * @param string $chars
  63. *
  64. * @return bool True if the value was fixed, false if not.
  65. *
  66. */
  67. public function sanitize($chars = null)
  68. {
  69. $value = $this->getValue();
  70. if (! is_scalar($value)) {
  71. return false;
  72. }
  73. if (! $chars) {
  74. $chars = $this->chars;
  75. }
  76. $this->setValue(trim($value, $chars));
  77. return true;
  78. }
  79. }