TypographicBehavior.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * “smart quotes” become "dumb quotes" on save
  4. * „low-high“ become "high-high"
  5. * same for single quotes (apostrophes)
  6. * in order to unify them
  7. *
  8. * using the TypographyHelper we can then format the output
  9. * according to the language/regional setting (in some languages
  10. * the high-high smart quotes, in others the low-high ones are preferred)
  11. *
  12. * @link http://en.wikipedia.org/wiki/Non-English_usage_of_quotation_marks
  13. * @cakephp 2.0
  14. * 2011-01-13 ms
  15. */
  16. class TypographicBehavior extends ModelBehavior {
  17. protected $map = array(
  18. 'in' => array(
  19. '‘' => '"',
  20. //'&lsquo;' => '"', # ‘
  21. '’' => '"',
  22. //'&rsquo;' => '"', # ’
  23. '‚' => '"',
  24. //'&sbquo;' => '"', # ‚
  25. '‛' => '"',
  26. //'&#8219;' => '"', # ‛
  27. '“' => '"',
  28. //'&ldquo;' => '"', # “
  29. '”' => '"',
  30. //'&rdquo;' => '"', # ”
  31. '„' => '"',
  32. //'&bdquo;' => '"', # „
  33. '‟' => '"',
  34. //'&#8223;' => '"', # ‟
  35. '«' => '"',
  36. //'&laquo;' => '"', # «
  37. '»' => '"',
  38. //'&raquo;' => '"', # »
  39. '‹' => '"',
  40. //'&laquo;' => '"', # ‹
  41. '›' => '"',
  42. //'&raquo;' => '"', # ›
  43. ),
  44. 'out'=> array(
  45. # use the TypographyHelper for this at runtime
  46. ),
  47. );
  48. /**
  49. * Initiate behavior for the model using specified settings. Available settings:
  50. *
  51. *
  52. * @param object $Model Model using the behaviour
  53. * @param array $settings Settings to override for model.
  54. * @access public
  55. * 2011-12-06 ms
  56. */
  57. public function setup(Model $Model, $settings = array()) {
  58. $default = array(
  59. 'before' => 'save',
  60. 'fields' => array()
  61. );
  62. if (!isset($this->settings[$Model->alias])) {
  63. $this->settings[$Model->alias] = $default;
  64. }
  65. $this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], is_array($settings) ? $settings : array());
  66. }
  67. public function beforeValidate(Model $Model) {
  68. parent::beforeValidate($Model);
  69. if ($this->settings[$Model->alias]['before'] == 'validate') {
  70. $this->process($Model);
  71. }
  72. return true;
  73. }
  74. public function beforeSave(Model $Model) {
  75. parent::beforeSave($Model);
  76. if ($this->settings[$Model->alias]['before'] == 'save') {
  77. $this->process($Model);
  78. }
  79. return true;
  80. }
  81. /**
  82. * Run before a model is saved
  83. *
  84. * @param object $Model Model about to be saved.
  85. * @return boolean true if save should proceed, false otherwise
  86. * @access public
  87. */
  88. public function process(Model $Model, $return = true) {
  89. foreach ($this->settings[$Model->alias]['fields'] as $field) {
  90. if (!empty($Model->data[$Model->alias][$field])) {
  91. $Model->data[$Model->alias][$field] = $this->_prepareInput($Model->data[$Model->alias][$field]);
  92. }
  93. }
  94. return $return;
  95. }
  96. /**
  97. * @param string $input
  98. * @return string $cleanedInput
  99. * 2011-12-06 ms
  100. */
  101. protected function _prepareInput($string) {
  102. $map = $this->map['in'];
  103. //return $string;
  104. $string = str_replace(array_keys($map), array_values($map), $string);
  105. return $string;
  106. }
  107. }