StringBehavior.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Tools\Model\Behavior;
  3. use ArrayObject;
  4. use Cake\Datasource\ResultSetInterface;
  5. use Cake\Event\Event;
  6. use Cake\ORM\Behavior;
  7. use Cake\ORM\Entity;
  8. use Cake\ORM\Query;
  9. /**
  10. * A behavior that will apply basic string operations for your input.
  11. *
  12. * Note that most string modification should be done once, on save.
  13. * Prevent using output modification if possible as it is done on every fetch.
  14. *
  15. * Tip: If you have other behaviors that might modify the array data prior to saving, better use a higher priority:
  16. * $this->addBehavior('Tools.String', array('priority' => 11, ...));
  17. * So that it is run last.
  18. *
  19. * Usage: See docs
  20. *
  21. * @author Mark Scherer
  22. * @license MIT
  23. */
  24. class StringBehavior extends Behavior {
  25. /**
  26. * //TODO: json input/ouput directly, clean
  27. *
  28. * @var array
  29. */
  30. protected $_defaultConfig = [
  31. 'fields' => [], // Fields to convert
  32. 'input' => [], // Basic input filters
  33. 'output' => [], // Basic output filters
  34. ];
  35. /**
  36. * @param array $config
  37. * @return void
  38. */
  39. public function initialize(array $config = []) {
  40. }
  41. /**
  42. * Decode the fields on after find
  43. *
  44. * @param \Cake\Event\Event $event
  45. * @param \Cake\ORM\Query $query
  46. * @return void
  47. */
  48. public function beforeFind(Event $event, Query $query) {
  49. $query->formatResults(function (ResultSetInterface $results) {
  50. return $results->map(function ($row) {
  51. $this->processItems($row, 'output');
  52. return $row;
  53. });
  54. });
  55. }
  56. /**
  57. * Decodes the fields of an array/entity (if the value itself was encoded)
  58. *
  59. * @param \Cake\ORM\Entity $entity
  60. * @param string $type Type (input/output)
  61. * @return void
  62. */
  63. public function processItems(Entity $entity, $type = 'input') {
  64. $fields = $this->_config['fields'];
  65. foreach ($fields as $field => $map) {
  66. if (is_numeric($field)) {
  67. $field = $map;
  68. $map = [];
  69. } else {
  70. $map = (array)$map;
  71. }
  72. $val = $entity->get($field);
  73. if (!$val && !is_numeric($val)) {
  74. continue;
  75. }
  76. if (!$map) {
  77. $map = $this->_config[$type];
  78. }
  79. if (!$map) {
  80. continue;
  81. }
  82. $entity->set($field, $this->_process($val, $map));
  83. }
  84. }
  85. /**
  86. * Saves all fields that do not belong to the current Model into 'with' helper model.
  87. *
  88. * @param \Cake\Event\Event $event
  89. * @param \Cake\ORM\Entity $entity
  90. * @param \ArrayObject $options
  91. * @return void
  92. */
  93. public function beforeSave(Event $event, Entity $entity, ArrayObject $options) {
  94. $this->processItems($entity, 'input');
  95. }
  96. /**
  97. * Process val via map
  98. *
  99. * @param string $val
  100. * @param array $map
  101. * @return string
  102. */
  103. public function _process($val, $map) {
  104. foreach ($map as $m => $arg) {
  105. if (is_numeric($m)) {
  106. $m = $arg;
  107. $arg = null;
  108. }
  109. if ($arg !== null) {
  110. $ret = call_user_func($m, $val, $arg);
  111. } else {
  112. $ret = call_user_func($m, $val);
  113. }
  114. if ($ret !== false) {
  115. $val = $ret;
  116. }
  117. }
  118. return $val;
  119. }
  120. }