StringBehavior.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. * @var array
  28. */
  29. protected $_defaultConfig = [
  30. 'fields' => [], // Fields to convert
  31. 'input' => [], // Basic input filters
  32. 'output' => [], // Basic output filters
  33. ];
  34. /**
  35. * JsonableBehavior::initialize()
  36. *
  37. * @param array $config
  38. * @return void
  39. */
  40. public function initialize(array $config = []) {
  41. }
  42. /**
  43. * Decode the fields on after find
  44. *
  45. * @param \Cake\Event\Event $event
  46. * @param \Cake\ORM\Query $query
  47. * @return void
  48. */
  49. public function beforeFind(Event $event, Query $query) {
  50. $query->formatResults(function (ResultSetInterface $results) {
  51. return $results->map(function ($row) {
  52. //TODO?
  53. //$this->processItems($row, 'output');
  54. return $row;
  55. });
  56. });
  57. }
  58. /**
  59. * Decodes the fields of an array/entity (if the value itself was encoded)
  60. *
  61. * @param \Cake\ORM\Entity $entity
  62. * @param string $type Type (input/output)
  63. * @return void
  64. */
  65. public function processItems(Entity $entity, $type = 'input') {
  66. $fields = $this->_config['fields'];
  67. foreach ($fields as $field => $map) {
  68. if (is_numeric($field)) {
  69. $field = $map;
  70. $map = [];
  71. } else {
  72. $map = (array)$map;
  73. }
  74. $val = $entity->get($field);
  75. if (!$val && !is_numeric($val)) {
  76. continue;
  77. }
  78. if (!$map) {
  79. $map = $this->_config[$type];
  80. }
  81. if (!$map) {
  82. continue;
  83. }
  84. $entity->set($field, $this->_process($val, $map));
  85. }
  86. }
  87. /**
  88. * Saves all fields that do not belong to the current Model into 'with' helper model.
  89. *
  90. * @param \Cake\Event\Event $event
  91. * @param \Cake\ORM\Entity $entity
  92. * @param \ArrayObject $options
  93. * @return void
  94. */
  95. public function beforeSave(Event $event, Entity $entity, ArrayObject $options) {
  96. $this->processItems($entity, 'input');
  97. }
  98. /**
  99. * Process val via map
  100. *
  101. * @param string $val
  102. * @param array $map
  103. * @return string
  104. */
  105. public function _process($val, $map) {
  106. foreach ($map as $m => $arg) {
  107. if (is_numeric($m)) {
  108. $m = $arg;
  109. $arg = null;
  110. }
  111. if ($arg !== null) {
  112. $ret = call_user_func($m, $val, $arg);
  113. } else {
  114. $ret = call_user_func($m, $val);
  115. }
  116. if ($ret !== false) {
  117. $val = $ret;
  118. }
  119. }
  120. return $val;
  121. }
  122. }