PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Croogo/Model/Behavior/ParamsBehavior.php

https://github.com/kareypowell/croogo
PHP | 101 lines | 55 code | 10 blank | 36 comment | 13 complexity | 4902cd851b3ba0a642b285e6683c3246 MD5 | raw file
  1. <?php
  2. App::uses('ModelBehavior', 'Model');
  3. App::uses('StringConverter', 'Croogo.Utility');
  4. /**
  5. * Params Behavior
  6. *
  7. * @category Behavior
  8. * @package Croogo.Croogo.Model.Behavior
  9. * @since 1.3.1
  10. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  11. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  12. * @link http://www.croogo.org
  13. */
  14. class ParamsBehavior extends ModelBehavior {
  15. /**
  16. * Setup
  17. *
  18. * @param Model $model
  19. * @param array $config
  20. * @return void
  21. */
  22. public function setup(Model $model, $config = array()) {
  23. if (is_string($config)) {
  24. $config = array($config);
  25. }
  26. $this->settings[$model->alias] = $config;
  27. }
  28. /**
  29. * afterFind callback
  30. *
  31. * @param Model $model
  32. * @param array $created
  33. * @param boolean $primary
  34. * @return array
  35. */
  36. public function afterFind(Model $model, $results, $primary = false) {
  37. if ($primary && isset($results[0][$model->alias])) {
  38. foreach ($results as $i => $result) {
  39. $params = array();
  40. if (isset($result[$model->alias]['params']) && strlen($result[$model->alias]['params']) > 0) {
  41. $params = $this->paramsToArray($model, $result[$model->alias]['params']);
  42. }
  43. $results[$i]['Params'] = $params;
  44. }
  45. } elseif (isset($results[$model->alias])) {
  46. $params = array();
  47. if (isset($results[$model->alias]['params']) && strlen($results[$model->alias]['params']) > 0) {
  48. $params = $this->paramsToArray($model, $results[$model->alias]['params']);
  49. }
  50. $results['Params'] = $params;
  51. }
  52. return $results;
  53. }
  54. /**
  55. * Converts a string of params to an array of formatted key/value pairs
  56. *
  57. * String is supposed to have one parameter per line in the format:
  58. * my_param_key=value_here
  59. * another_param=another_value
  60. *
  61. * @param Model $model
  62. * @param string $params
  63. * @return array
  64. */
  65. public function paramsToArray(Model $model, $params) {
  66. $converter = new StringConverter();
  67. $output = array();
  68. $params = preg_split('/[\r\n]+/', $params);
  69. foreach ($params as $param) {
  70. if (strlen($param) == 0) {
  71. continue;
  72. }
  73. if ($param[0] === '[') {
  74. $options = $converter->parseString('options', $param, array(
  75. 'convertOptionsToArray' => true,
  76. ));
  77. if (!empty($options)) {
  78. $output = array_merge($output, $options);
  79. }
  80. continue;
  81. }
  82. $paramE = explode('=', $param);
  83. if (count($paramE) == 2) {
  84. $key = $paramE['0'];
  85. $value = $paramE['1'];
  86. $output[$key] = trim($value);
  87. }
  88. }
  89. return $output;
  90. }
  91. }