class-collector.php 940 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. /** @var WPSEO_Collection[] */
  12. protected $collections = array();
  13. /**
  14. * Adds a collection object to the collections.
  15. *
  16. * @param WPSEO_Collection $collection The collection object to add.
  17. */
  18. public function add_collection( WPSEO_Collection $collection ) {
  19. $this->collections[] = $collection;
  20. }
  21. /**
  22. * Collects the data from the collection objects.
  23. *
  24. * @return array The collected data.
  25. */
  26. public function collect() {
  27. $data = array();
  28. foreach ( $this->collections as $collection ) {
  29. $data = array_merge( $data, $collection->get() );
  30. }
  31. return $data;
  32. }
  33. /**
  34. * Returns the collected data as a JSON encoded string.
  35. *
  36. * @return false|string The encode string.
  37. */
  38. public function get_as_json() {
  39. return wp_json_encode( $this->collect() );
  40. }
  41. }