TypographicBehavior.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * @author Mark Scherer
  6. * @cakephp 2.x
  7. * @license MIT
  8. */
  9. App::uses('ModelBehavior', 'Model');
  10. /**
  11. * Replace regionalized chars with standard ones on input.
  12. *
  13. * “smart quotes” become "dumb quotes" on save
  14. * „low-high“ become "high-high"
  15. * same for single quotes (apostrophes)
  16. * in order to unify them. Basic idea is a unified non-regional version in the database.
  17. *
  18. * Using the TypographyHelper we can then format the output
  19. * according to the language/regional setting (in some languages
  20. * the high-high smart quotes, in others the low-high ones are preferred)
  21. *
  22. * Settings are:
  23. * - string $before (validate/save)
  24. * - array $fields (leave empty for auto detection)
  25. * - bool $mergeQuotes (merge single and double into " or any custom char)
  26. *
  27. * TODOS:
  28. * - respect primary and secondary quotations marks as well as alternatives
  29. *
  30. * @link http://www.dereuromark.de/2012/08/12/typographic-behavior-and-typography-helper/
  31. * @link http://en.wikipedia.org/wiki/Non-English_usage_of_quotation_marks
  32. */
  33. class TypographicBehavior extends ModelBehavior {
  34. protected $_map = array(
  35. 'in' => array(
  36. '‘' => '\'',
  37. // Translates to '&lsquo;'.
  38. '’' => '\'',
  39. // Translates to '&rsquo;'.
  40. '‚' => '\'',
  41. // Translates to '&sbquo;'.
  42. '‛' => '\'',
  43. // Translates to '&#8219;'.
  44. '“' => '"',
  45. // Translates to '&ldquo;'.
  46. '”' => '"',
  47. // Translates to '&rdquo;'.
  48. '„' => '"',
  49. // Translates to '&bdquo;'.
  50. '‟' => '"',
  51. // Translates to '&#8223;'.
  52. '«' => '"',
  53. // Translates to '&laquo;'.
  54. '»' => '"',
  55. // Translates to '&raquo;'.
  56. '‹' => '\'',
  57. // Translates to '&laquo;'.
  58. '›' => '\'',
  59. // Translates to '&raquo;'.
  60. ),
  61. 'out' => array(
  62. // Use the TypographyHelper for this at runtime.
  63. ),
  64. );
  65. protected $_defaults = array(
  66. 'before' => 'save',
  67. 'fields' => array(),
  68. 'mergeQuotes' => false, // Set to true for " or explicitly set a char (" or ').
  69. );
  70. /**
  71. * Initiate behavior for the model using specified settings.
  72. * Available settings:
  73. *
  74. * @param object $Model Model using the behaviour
  75. * @param array $settings Settings to override for model.
  76. * @return void
  77. */
  78. public function setup(Model $Model, $settings = array()) {
  79. if (!isset($this->settings[$Model->alias])) {
  80. $this->settings[$Model->alias] = $this->_defaults;
  81. }
  82. $this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], $settings);
  83. if (empty($this->settings[$Model->alias]['fields'])) {
  84. $schema = $Model->schema();
  85. $fields = array();
  86. foreach ($schema as $field => $v) {
  87. if (!in_array($v['type'], array('string', 'text'))) {
  88. continue;
  89. }
  90. if (!empty($v['key'])) {
  91. continue;
  92. }
  93. if (isset($v['length']) && $v['length'] === 1) { // TODO: also skip UUID (lenght 36)?
  94. continue;
  95. }
  96. $fields[] = $field;
  97. }
  98. $this->settings[$Model->alias]['fields'] = $fields;
  99. }
  100. if ($this->settings[$Model->alias]['mergeQuotes'] === true) {
  101. $this->settings[$Model->alias]['mergeQuotes'] = '"';
  102. }
  103. }
  104. /**
  105. * TypographicBehavior::beforeValidate()
  106. *
  107. * @param Model $Model
  108. * @return boolean Success
  109. */
  110. public function beforeValidate(Model $Model, $options = array()) {
  111. parent::beforeValidate($Model, $options);
  112. if ($this->settings[$Model->alias]['before'] === 'validate') {
  113. $this->process($Model);
  114. }
  115. return true;
  116. }
  117. /**
  118. * TypographicBehavior::beforeSave()
  119. *
  120. * @param Model $Model
  121. * @return boolean Success
  122. */
  123. public function beforeSave(Model $Model, $options = array()) {
  124. parent::beforeSave($Model, $options);
  125. if ($this->settings[$Model->alias]['before'] === 'save') {
  126. $this->process($Model);
  127. }
  128. return true;
  129. }
  130. /**
  131. * Run the behavior over all records of this model
  132. * This is useful if you attach it after some records have already been saved without it.
  133. *
  134. * @param Model $Model The model about to be saved.
  135. * @return integer $count Number of affected/changed records
  136. */
  137. public function updateTypography(Model $Model, $dryRun = false) {
  138. $options = array('recursive' => -1, 'limit' => 100, 'offset' => 0);
  139. $count = 0;
  140. while ($records = $Model->find('all', $options)) {
  141. foreach ($records as $record) {
  142. $changed = false;
  143. foreach ($this->settings[$Model->alias]['fields'] as $field) {
  144. if (empty($record[$Model->alias][$field])) {
  145. continue;
  146. }
  147. $tmp = $this->_prepareInput($Model, $record[$Model->alias][$field]);
  148. if ($tmp == $record[$Model->alias][$field]) {
  149. continue;
  150. }
  151. $record[$Model->alias][$field] = $tmp;
  152. $changed = true;
  153. }
  154. if ($changed) {
  155. if (!$dryRun) {
  156. $Model->save($record, false);
  157. }
  158. $count++;
  159. }
  160. }
  161. $options['offset'] += 100;
  162. }
  163. return $count;
  164. }
  165. /**
  166. * Run before a model is saved
  167. *
  168. * @param object $Model Model about to be saved.
  169. * @return boolean true if save should proceed, false otherwise
  170. */
  171. public function process(Model $Model, $return = true) {
  172. foreach ($this->settings[$Model->alias]['fields'] as $field) {
  173. if (!empty($Model->data[$Model->alias][$field])) {
  174. $Model->data[$Model->alias][$field] = $this->_prepareInput($Model, $Model->data[$Model->alias][$field]);
  175. }
  176. }
  177. return $return;
  178. }
  179. /**
  180. * @param string $input
  181. * @return string $cleanedInput
  182. * 2011-12-06 ms
  183. */
  184. protected function _prepareInput(Model $Model, $string) {
  185. $map = $this->_map['in'];
  186. if ($this->settings[$Model->alias]['mergeQuotes']) {
  187. foreach ($map as $key => $val) {
  188. $map[$key] = $this->settings[$Model->alias]['mergeQuotes'];
  189. }
  190. }
  191. return str_replace(array_keys($map), array_values($map), $string);
  192. }
  193. }