class-field.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\ConfigurationUI
  6. */
  7. /**
  8. * Class WPSEO_Config_Field.
  9. */
  10. class WPSEO_Config_Field {
  11. /**
  12. * Field name.
  13. *
  14. * @var string
  15. */
  16. protected $field;
  17. /**
  18. * Component to use.
  19. *
  20. * @var string
  21. */
  22. protected $component;
  23. /**
  24. * Properties of this field.
  25. *
  26. * @var array
  27. */
  28. protected $properties = [];
  29. /**
  30. * Field requirements.
  31. *
  32. * @var array
  33. */
  34. protected $requires = [];
  35. /**
  36. * Value of this field.
  37. *
  38. * @var array|mixed
  39. */
  40. protected $data = [];
  41. /**
  42. * WPSEO_Config_Field constructor.
  43. *
  44. * @param string $field The field name.
  45. * @param string $component The component to use.
  46. */
  47. public function __construct( $field, $component ) {
  48. $this->field = $field;
  49. $this->component = $component;
  50. }
  51. /**
  52. * Get the identifier.
  53. *
  54. * @return string
  55. */
  56. public function get_identifier() {
  57. return $this->field;
  58. }
  59. /**
  60. * Get the component.
  61. *
  62. * @return string
  63. */
  64. public function get_component() {
  65. return $this->component;
  66. }
  67. /**
  68. * Set a property value.
  69. *
  70. * @param string $name Property to set.
  71. * @param mixed $value Value to apply.
  72. */
  73. public function set_property( $name, $value ) {
  74. $this->properties[ $name ] = $value;
  75. }
  76. /**
  77. * Get all the properties.
  78. *
  79. * @return array
  80. */
  81. public function get_properties() {
  82. return $this->properties;
  83. }
  84. /**
  85. * Get the data.
  86. *
  87. * @return mixed
  88. */
  89. public function get_data() {
  90. return $this->data;
  91. }
  92. /**
  93. * Array representation of this object.
  94. *
  95. * @return array
  96. */
  97. public function to_array() {
  98. $output = [
  99. 'componentName' => $this->get_component(),
  100. ];
  101. $properties = $this->get_properties();
  102. if ( $properties ) {
  103. $output['properties'] = $properties;
  104. }
  105. $requires = $this->get_requires();
  106. if ( ! empty( $requires ) ) {
  107. $output['requires'] = $requires;
  108. }
  109. return $output;
  110. }
  111. /**
  112. * Set the adapter to use.
  113. *
  114. * @param WPSEO_Configuration_Options_Adapter $adapter Adapter to register lookup on.
  115. */
  116. public function set_adapter( WPSEO_Configuration_Options_Adapter $adapter ) {
  117. }
  118. /**
  119. * Requires another field to have a certain value.
  120. *
  121. * @param string $field Field to check for a certain value.
  122. * @param mixed $value Value of the field.
  123. */
  124. public function set_requires( $field, $value ) {
  125. $this->requires = [
  126. 'field' => $field,
  127. 'value' => $value,
  128. ];
  129. }
  130. /**
  131. * Get the required field settings (if present).
  132. *
  133. * @return array
  134. */
  135. public function get_requires() {
  136. return $this->requires;
  137. }
  138. }