class-collector.php 984 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Collects the data from the added collection objects.
  9. */
  10. class WPSEO_Collector {
  11. /**
  12. * Holds the collections.
  13. *
  14. * @var WPSEO_Collection[]
  15. */
  16. protected $collections = [];
  17. /**
  18. * Adds a collection object to the collections.
  19. *
  20. * @param WPSEO_Collection $collection The collection object to add.
  21. */
  22. public function add_collection( WPSEO_Collection $collection ) {
  23. $this->collections[] = $collection;
  24. }
  25. /**
  26. * Collects the data from the collection objects.
  27. *
  28. * @return array The collected data.
  29. */
  30. public function collect() {
  31. $data = [];
  32. foreach ( $this->collections as $collection ) {
  33. $data = array_merge( $data, $collection->get() );
  34. }
  35. return $data;
  36. }
  37. /**
  38. * Returns the collected data as a JSON encoded string.
  39. *
  40. * @return false|string The encode string.
  41. */
  42. public function get_as_json() {
  43. return WPSEO_Utils::format_json_encode( $this->collect() );
  44. }
  45. }