Нема описа

Registry.php 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Defines the construct for building an item registry or collection.
  10. *
  11. * @since 1.7.0
  12. */
  13. abstract class PUM_Abstract_Registry {
  14. /**
  15. * Array of registry items.
  16. *
  17. * @var array
  18. */
  19. protected $items = array();
  20. /**
  21. * Adds an item to the registry.
  22. *
  23. * @param int $item_id Item ID.
  24. * @param array|object|mixed $attributes {
  25. * Item attributes.
  26. *
  27. * @type string $class Item handler class.
  28. * @type string $file Item handler class file.
  29. * }
  30. *
  31. * @return true Always true.
  32. */
  33. public function add_item( $item_id, $attributes ) {
  34. foreach ( $attributes as $attribute => $value ) {
  35. $this->items[ $item_id ][ $attribute ] = $value;
  36. }
  37. return true;
  38. }
  39. /**
  40. * Removes an item from the registry by ID.
  41. *
  42. * @param string $item_id Item ID.
  43. */
  44. public function remove_item( $item_id ) {
  45. unset( $this->items[ $item_id ] );
  46. }
  47. /**
  48. * Retrieves an item and its associated attributes.
  49. *
  50. * @param string $item_id Item ID.
  51. *
  52. * @return array|false Array of attributes for the item if registered, otherwise false.
  53. */
  54. public function get( $item_id ) {
  55. if ( array_key_exists( $item_id, $this->items ) ) {
  56. return $this->items[ $item_id ];
  57. }
  58. return false;
  59. }
  60. /**
  61. * Retrieves registered items.
  62. *
  63. * @return array The list of registered items.
  64. */
  65. public function get_items() {
  66. return $this->items;
  67. }
  68. /**
  69. * Only intended for use by tests.
  70. */
  71. public function _reset_items() {
  72. if ( ! defined( 'WP_TESTS_DOMAIN' ) ) {
  73. _doing_it_wrong( 'PUM_Abstract_Registry::_reset_items', 'This method is only intended for use in phpunit tests', '1.7.0' );
  74. } else {
  75. $this->items = array();
  76. }
  77. }
  78. }