BakeHelper.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Cake\View\Helper;
  3. use Cake\Core\ConventionsTrait;
  4. use Cake\Utility\Inflector;
  5. use Cake\View\Helper;
  6. /**
  7. * Bake helper
  8. */
  9. class BakeHelper extends Helper {
  10. use ConventionsTrait;
  11. /**
  12. * Default configuration.
  13. *
  14. * @var array
  15. */
  16. protected $_defaultConfig = [];
  17. /**
  18. * Used for generating formatted properties such as component and helper arrays
  19. *
  20. * @param string $name the name of the property
  21. * @param array $value the array of values
  22. * @param array $options extra options to be passed ot the element
  23. * @return string
  24. */
  25. public function arrayProperty($name, array $value = [], array $options = []) {
  26. if (!$value) {
  27. return '';
  28. }
  29. foreach ($value as &$val) {
  30. $val = Inflector::camelize($val);
  31. }
  32. $options += [
  33. 'name' => $name,
  34. 'value' => $value
  35. ];
  36. return $this->_View->element('array_property', $options);
  37. }
  38. /**
  39. * Returns an array converted into a formatted multiline string
  40. *
  41. * @param array $list array of items to be stringified
  42. * @param array $options options to use
  43. * @return string
  44. */
  45. public function stringifyList(array $list, array $options = []) {
  46. $options += [
  47. 'indent' => 2
  48. ];
  49. if (!$list) {
  50. return '';
  51. }
  52. foreach ($list as $k => &$v) {
  53. $v = "'$v'";
  54. if (!is_numeric($k)) {
  55. $v = "'$k' => $v";
  56. }
  57. }
  58. $start = $end = '';
  59. $join = ', ';
  60. if ($options['indent']) {
  61. $join = ',';
  62. $start = "\n" . str_repeat("\t", $options['indent']);
  63. $join .= $start;
  64. $end = "\n" . str_repeat("\t", $options['indent'] - 1);
  65. }
  66. return $start . implode($join, $list) . $end;
  67. }
  68. /**
  69. * Extract the aliases for associations
  70. *
  71. * @param \Cake\ORM\Table $table object to find associations on
  72. * @param string $assoc association to extract
  73. * @return array
  74. */
  75. public function aliasExtractor($table, $assoc) {
  76. $extractor = function ($val) {
  77. return $val->target()->alias();
  78. };
  79. return array_map($extractor, $table->associations()->type($assoc));
  80. }
  81. }