Collection.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Asset;
  7. /**
  8. * List of page asset instances associated with unique identifiers
  9. */
  10. class Collection
  11. {
  12. /**
  13. * Assets
  14. *
  15. * @var AssetInterface[]
  16. */
  17. protected $assets = [];
  18. /**
  19. * Add an instance, identified by a unique identifier, to the list
  20. *
  21. * @param string $identifier
  22. * @param AssetInterface $asset
  23. * @return void
  24. */
  25. public function add($identifier, AssetInterface $asset)
  26. {
  27. $this->assets[$identifier] = $asset;
  28. }
  29. /**
  30. * @param string $identifier
  31. * @param AssetInterface $asset
  32. * @param string $key
  33. * @return void
  34. */
  35. public function insert($identifier, AssetInterface $asset, $key)
  36. {
  37. $result = [];
  38. foreach ($this->assets as $assetKey => $assetVal) {
  39. if ($assetKey == $key) {
  40. $result[$key] = $assetVal;
  41. $result[$identifier] = $asset;
  42. } else {
  43. $result[$assetKey] = $assetVal;
  44. }
  45. }
  46. $this->assets = $result;
  47. }
  48. /**
  49. * Whether an item belongs to a collection or not
  50. *
  51. * @param string $identifier
  52. * @return bool
  53. */
  54. public function has($identifier)
  55. {
  56. return isset($this->assets[$identifier]);
  57. }
  58. /**
  59. * Remove an item from the list
  60. *
  61. * @param string $identifier
  62. * @return void
  63. */
  64. public function remove($identifier)
  65. {
  66. unset($this->assets[$identifier]);
  67. }
  68. /**
  69. * Retrieve all items in the collection
  70. *
  71. * @return AssetInterface[]
  72. */
  73. public function getAll()
  74. {
  75. return $this->assets;
  76. }
  77. }