Guest User

Untitled

a guest
Sep 24th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. <?php
  2. namespace Acme;
  3.  
  4. $int_filter = function($value) {
  5. if (!preg_match('/\A(?:0|[1-9][0-9]*)\z/', (string)$value)) {
  6. throw new \InvalidArgumentException('invalid unsigned integer.');
  7. }
  8. return (int)$value;
  9. };
  10.  
  11. $length_checker = function($value, $min = null, $max = null) {
  12. $length = mb_strlen($value);
  13. if (isset($min) && $length < $min) {
  14. throw new \InvalidArgumentException(
  15. sprintf('character length must not less than %s.', $min));
  16. }
  17. if (isset($max) && $length > $max) {
  18. throw new \InvalidArgumentException(
  19. sprintf('character length must not greater than %s.', $max));
  20. }
  21. return $value;
  22. };
  23.  
  24. $sanitizer = function($value) {
  25. return preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S', '', $value);
  26. };
  27.  
  28. $normalize_newline = function($value) {
  29. return str_replace("\r", "\n", str_replace("\r\n", "\n", $value));
  30. };
  31.  
  32. $user = FilterableAccessor::instance(array(
  33. 'number' => function($value) use ($int_filter) {
  34. return $int_filter($value);
  35. },
  36. 'name' => function($value) use ($length_checker, $sanitizer) {
  37. return $length_checker(
  38. $sanitizer($value), 1, 10);
  39. },
  40. 'comment' => function($value) use ($length_checker, $sanitizer, $normalize_newline) {
  41. return $length_checker(
  42. $normalize_newline(
  43. $sanitizer($value), null, 50));
  44. },
  45. ));
  46.  
  47. // プロパティ形式
  48. $user->number = '1';
  49. $user->name = 'Tanaka';
  50. echo '<pre>';
  51. var_dump($user->number); // int(1)
  52. var_dump($user->name); // string(6) "Tanaka"
  53. echo '</pre>';
  54.  
  55. // メソッド形式
  56. $user->number('2')->name('Suzuki');
  57. echo '<pre>';
  58. var_dump($user->number()); // int(2)
  59. var_dump($user->name()); // string(6) "Suzuki"
  60. echo '</pre>';
  61.  
  62. // フィルタで制御コードをサニタイズ(←言うな)
  63. $user->name("Holy\x00\x00\x00"); // NULL byte
  64. echo '<pre>';
  65. var_dump($user->name()); // string(4) "Holy"
  66. echo '</pre>';
  67.  
  68. // フィルタで改行コードを正規化
  69. $user->comment("Hoge\r\nFuga\r\nPiyo\r\n"); // CR+LF newline
  70. echo '<pre>';
  71. var_dump($user->comment()); // string(15) "Hoge\nFuga\nPiyo\n"
  72. echo '</pre>';
  73.  
  74.  
  75. // 未定義プロパティへのアクセス
  76. echo '<pre>';
  77. try {
  78. $user->age = '30';
  79. } catch (\InvalidArgumentException $e) {
  80. echo $e->getMessage(); // The property "age" does not exists.
  81. }
  82. echo '</pre>';
  83.  
  84. // フィルタでチェック
  85. echo '<pre>';
  86. try {
  87. $user->number = '-1';
  88. } catch (\InvalidArgumentException $e) {
  89. echo $e->getMessage(); // The property "number" is not valid : invalid unsigned integer.
  90. }
  91. echo '</pre>';
  92.  
  93. // フィルタでチェック
  94. echo '<pre>';
  95. try {
  96. $user->name('VeryVeryLongName');
  97. } catch (\InvalidArgumentException $e) {
  98. echo $e->getMessage(); // The property "name" is not valid : character length must not greater than 10.
  99. }
  100. echo '</pre>';
Add Comment
Please, Sign In to add comment