Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GroupedCollection.php
Go to the documentation of this file.
1 <?php
7 
15 {
19  const PROPERTY_CONTENT_TYPE = 'content_type';
20 
21  const PROPERTY_CAN_MERGE = 'can_merge';
22 
26  protected $propertyFactory;
27 
33  protected $groups = [];
34 
41  {
42  $this->propertyFactory = $propertyFactory;
43  }
44 
53  public function add($identifier, AssetInterface $asset, array $properties = [])
54  {
55  parent::add($identifier, $asset);
57  $this->getGroupFor($properties)->add($identifier, $asset);
58  }
59 
66  public function insert($identifier, AssetInterface $asset, $key)
67  {
68  parent::insert($identifier, $asset, $key);
69  $properties = $this->getFilteredProperties($asset);
70  $this->getGroupFor($properties)->insert($identifier, $asset, $key);
71  }
72 
78  public function getFilteredProperties(AssetInterface $asset, $properties = [])
79  {
80  $properties = array_filter($properties);
83 
84  return $properties;
85  }
86 
93  private function getGroupFor(array $properties)
94  {
96  foreach ($this->groups as $existingGroup) {
97  if ($existingGroup->getProperties() == $properties) {
98  return $existingGroup;
99  }
100  }
102  $newGroup = $this->propertyFactory->create(['properties' => $properties]);
103  $this->groups[] = $newGroup;
104  return $newGroup;
105  }
106 
113  public function remove($identifier)
114  {
115  parent::remove($identifier);
117  foreach ($this->groups as $group) {
118  if ($group->has($identifier)) {
119  $group->remove($identifier);
120  return;
121  }
122  }
123  }
124 
130  public function getGroups()
131  {
132  return $this->groups;
133  }
134 
141  public function getGroupByContentType($contentType)
142  {
143  foreach ($this->groups as $group) {
144  if ($group->getProperty(self::PROPERTY_CONTENT_TYPE) == $contentType) {
145  return $group;
146  }
147  }
148 
149  return false;
150  }
151 }
$group
Definition: sections.phtml:16
add($identifier, AssetInterface $asset, array $properties=[])
getFilteredProperties(AssetInterface $asset, $properties=[])
__construct(PropertyGroupFactory $propertyFactory)
$properties
Definition: categories.php:26
insert($identifier, AssetInterface $asset, $key)