123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\View\Asset;
- /**
- * List of page assets that combines into groups ones having the same properties
- *
- * @api
- * @since 100.0.2
- */
- class GroupedCollection extends Collection
- {
- /**#@+
- * Special properties, enforced to be grouped by
- */
- const PROPERTY_CONTENT_TYPE = 'content_type';
- const PROPERTY_CAN_MERGE = 'can_merge';
- /**#@-*/
- /**#@-*/
- protected $propertyFactory;
- /**
- * Property Groups
- *
- * @var PropertyGroup[]
- */
- protected $groups = [];
- /**
- * Constructor
- *
- * @param PropertyGroupFactory $propertyFactory
- */
- public function __construct(PropertyGroupFactory $propertyFactory)
- {
- $this->propertyFactory = $propertyFactory;
- }
- /**
- * Add an instance, identified by a unique identifier, to the list and to the corresponding group
- *
- * @param string $identifier
- * @param AssetInterface $asset
- * @param array $properties
- * @return void
- */
- public function add($identifier, AssetInterface $asset, array $properties = [])
- {
- parent::add($identifier, $asset);
- $properties = $this->getFilteredProperties($asset, $properties);
- $this->getGroupFor($properties)->add($identifier, $asset);
- }
- /**
- * @param string $identifier
- * @param AssetInterface $asset
- * @param string $key
- * @return void
- */
- public function insert($identifier, AssetInterface $asset, $key)
- {
- parent::insert($identifier, $asset, $key);
- $properties = $this->getFilteredProperties($asset);
- $this->getGroupFor($properties)->insert($identifier, $asset, $key);
- }
- /**
- * @param AssetInterface $asset
- * @param array $properties
- * @return array
- */
- public function getFilteredProperties(AssetInterface $asset, $properties = [])
- {
- $properties = array_filter($properties);
- $properties[self::PROPERTY_CONTENT_TYPE] = $asset->getContentType();
- $properties[self::PROPERTY_CAN_MERGE] = $asset instanceof MergeableInterface;
- return $properties;
- }
- /**
- * Retrieve existing or new group matching the properties
- *
- * @param array $properties
- * @return PropertyGroup
- */
- private function getGroupFor(array $properties)
- {
- /** @var $existingGroup PropertyGroup */
- foreach ($this->groups as $existingGroup) {
- if ($existingGroup->getProperties() == $properties) {
- return $existingGroup;
- }
- }
- /** @var $newGroup PropertyGroup */
- $newGroup = $this->propertyFactory->create(['properties' => $properties]);
- $this->groups[] = $newGroup;
- return $newGroup;
- }
- /**
- * Remove an instance from the list and from the corresponding group
- *
- * @param string $identifier
- * @return void
- */
- public function remove($identifier)
- {
- parent::remove($identifier);
- /** @var PropertyGroup $group */
- foreach ($this->groups as $group) {
- if ($group->has($identifier)) {
- $group->remove($identifier);
- return;
- }
- }
- }
- /**
- * Retrieve groups, containing assets that have the same properties
- *
- * @return PropertyGroup[]
- */
- public function getGroups()
- {
- return $this->groups;
- }
- /**
- * Get asset group by content type
- *
- * @param string $contentType
- * @return bool|PropertyGroup
- */
- public function getGroupByContentType($contentType)
- {
- foreach ($this->groups as $group) {
- if ($group->getProperty(self::PROPERTY_CONTENT_TYPE) == $contentType) {
- return $group;
- }
- }
- return false;
- }
- }
|