ListAbstract.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * hold the ListAbstract base class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin;
  9. use ArrayObject;
  10. /**
  11. * Generic list class
  12. *
  13. * @todo add caching
  14. * @abstract
  15. * @package PhpMyAdmin
  16. * @since phpMyAdmin 2.9.10
  17. */
  18. abstract class ListAbstract extends ArrayObject
  19. {
  20. /**
  21. * @var mixed empty item
  22. */
  23. protected $item_empty = '';
  24. /**
  25. * ListAbstract constructor
  26. *
  27. * @param array $array The input parameter accepts an array or an
  28. * Object.
  29. * @param int $flags Flags to control the behaviour of the
  30. * ArrayObject object.
  31. * @param string $iterator_class Specify the class that will be used for
  32. * iteration of the ArrayObject object.
  33. * ArrayIterator is the default class used.
  34. */
  35. public function __construct(
  36. array $array = array(), $flags = 0, $iterator_class = "ArrayIterator"
  37. ) {
  38. parent::__construct($array, $flags, $iterator_class);
  39. }
  40. /**
  41. * defines what is an empty item (0, '', false or null)
  42. *
  43. * @return mixed an empty item
  44. */
  45. public function getEmpty()
  46. {
  47. return $this->item_empty;
  48. }
  49. /**
  50. * checks if the given db names exists in the current list, if there is
  51. * missing at least one item it returns false otherwise true
  52. *
  53. * @return boolean true if all items exists, otherwise false
  54. */
  55. public function exists()
  56. {
  57. $this_elements = $this->getArrayCopy();
  58. foreach (func_get_args() as $result) {
  59. if (! in_array($result, $this_elements)) {
  60. return false;
  61. }
  62. }
  63. return true;
  64. }
  65. /**
  66. * returns HTML <option>-tags to be used inside <select></select>
  67. *
  68. * @param mixed $selected the selected db or true for
  69. * selecting current db
  70. * @param boolean $include_information_schema whether include information schema
  71. *
  72. * @return string HTML option tags
  73. */
  74. public function getHtmlOptions(
  75. $selected = '', $include_information_schema = true
  76. ) {
  77. if (true === $selected) {
  78. $selected = $this->getDefault();
  79. }
  80. $options = '';
  81. foreach ($this as $each_item) {
  82. if (false === $include_information_schema
  83. && $GLOBALS['dbi']->isSystemSchema($each_item)
  84. ) {
  85. continue;
  86. }
  87. $options .= '<option value="' . htmlspecialchars($each_item) . '"';
  88. if ($selected === $each_item) {
  89. $options .= ' selected="selected"';
  90. }
  91. $options .= '>' . htmlspecialchars($each_item) . '</option>' . "\n";
  92. }
  93. return $options;
  94. }
  95. /**
  96. * returns default item
  97. *
  98. * @return string default item
  99. */
  100. public function getDefault()
  101. {
  102. return $this->getEmpty();
  103. }
  104. /**
  105. * builds up the list
  106. *
  107. * @return void
  108. */
  109. abstract public function build();
  110. }