TypographicBehavior.php 5.2 KB

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