OptionsPropertyGroup.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Superclass for the Property Group classes.
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Properties\Options;
  7. use Countable;
  8. use function array_diff;
  9. use function count;
  10. use function in_array;
  11. /**
  12. * Parents group property items and provides methods to manage groups of
  13. * properties.
  14. *
  15. * @todo modify descriptions if needed, when the options are integrated
  16. */
  17. abstract class OptionsPropertyGroup extends OptionsPropertyItem implements Countable
  18. {
  19. /**
  20. * Holds a group of properties (PhpMyAdmin\Properties\Options\OptionsPropertyItem instances)
  21. *
  22. * @var array
  23. */
  24. private $properties;
  25. /**
  26. * Adds a property to the group of properties
  27. *
  28. * @param OptionsPropertyItem $property the property instance to be added
  29. * to the group
  30. *
  31. * @return void
  32. */
  33. public function addProperty($property)
  34. {
  35. if (! $this->getProperties() == null
  36. && in_array($property, $this->getProperties(), true)
  37. ) {
  38. return;
  39. }
  40. $this->properties[] = $property;
  41. }
  42. /**
  43. * Removes a property from the group of properties
  44. *
  45. * @param OptionsPropertyItem $property the property instance to be removed
  46. * from the group
  47. *
  48. * @return void
  49. */
  50. public function removeProperty($property)
  51. {
  52. $this->properties = array_diff(
  53. $this->getProperties(),
  54. [$property]
  55. );
  56. }
  57. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  58. /**
  59. * Gets the instance of the class
  60. *
  61. * @return OptionsPropertyGroup
  62. */
  63. public function getGroup()
  64. {
  65. return $this;
  66. }
  67. /**
  68. * Gets the group of properties
  69. *
  70. * @return array
  71. */
  72. public function getProperties()
  73. {
  74. return $this->properties;
  75. }
  76. /**
  77. * Gets the number of properties
  78. *
  79. * @return int
  80. */
  81. public function getNrOfProperties()
  82. {
  83. if ($this->properties === null) {
  84. return 0;
  85. }
  86. return count($this->properties);
  87. }
  88. /**
  89. * Countable interface implementation.
  90. *
  91. * @return int
  92. */
  93. public function count()
  94. {
  95. return $this->getNrOfProperties();
  96. }
  97. }