ListAbstract.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin;
  4. use ArrayObject;
  5. use PhpMyAdmin\Query\Utilities;
  6. use function in_array;
  7. /**
  8. * @extends ArrayObject<int, string>
  9. */
  10. abstract class ListAbstract extends ArrayObject
  11. {
  12. /** @var mixed empty item */
  13. protected $itemEmpty = '';
  14. /**
  15. * defines what is an empty item (0, '', false or null)
  16. *
  17. * @return mixed an empty item
  18. */
  19. public function getEmpty()
  20. {
  21. return $this->itemEmpty;
  22. }
  23. /**
  24. * checks if the given db names exists in the current list, if there is
  25. * missing at least one item it returns false otherwise true
  26. *
  27. * @param mixed[] ...$params params
  28. */
  29. public function exists(...$params): bool
  30. {
  31. $this_elements = $this->getArrayCopy();
  32. foreach ($params as $result) {
  33. if (! in_array($result, $this_elements)) {
  34. return false;
  35. }
  36. }
  37. return true;
  38. }
  39. /**
  40. * @return array<int, array<string, bool|string>>
  41. */
  42. public function getList(): array
  43. {
  44. $selected = $this->getDefault();
  45. $list = [];
  46. foreach ($this as $eachItem) {
  47. if (Utilities::isSystemSchema($eachItem)) {
  48. continue;
  49. }
  50. $list[] = [
  51. 'name' => $eachItem,
  52. 'is_selected' => $selected === $eachItem,
  53. ];
  54. }
  55. return $list;
  56. }
  57. /**
  58. * returns default item
  59. *
  60. * @return string default item
  61. */
  62. public function getDefault()
  63. {
  64. return $this->getEmpty();
  65. }
  66. /**
  67. * builds up the list
  68. */
  69. abstract public function build(): void;
  70. }