-
Notifications
You must be signed in to change notification settings - Fork 20
ElementCollection
The class provides a flexible tool for working with cached lists of model elements.
- arElementIDList - array of element ID
Static method. Used to create a new object of the ElementCollection class.
$obList = ElementCollection::make([1, 2, 10, 15]);- arElementIDList - array of element ID
Method allow to set list of element IDs in the collection
$obList = ElementCollection::make()->set([1,2]);Method returns true, if the element list is empty.
$obList = ElementCollection::make([1, 2, 10, 15]);
if($obList->isEmpty()) {
return false;
}The method returns true, if the element list isn't empty.
$obList = ElementCollection::make([1, 2, 10, 15]);
if($obList->isNotEmpty()) {
//...
}Method returns an array of element IDs.
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->getIDList();- iElementID - element ID
Method returns true, if the collection contains the element with ID = $iElementID.
$obList = ElementCollection::make([1, 2, 10, 15]);
if($obList->has(10)) {
//...
}- iElementID - element ID
Method returns an object ElementItem with ID = $iElementID.
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->find(10);Method clears the collection.
$obList = ElementCollection::make([1, 2, 10, 15]);
$obList->clear();Method returns the count of elements in a collection.
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->count();- $arElementIDList - list of element IDs for intersection
Method applies the array_intersect() function to the collection and the array of element IDs $arElementIDList.
$obList = ElementCollection::make([1, 2, 10, 15]);
$arElementIDList = [2,14,18];
$obList->intersect($arElementIDList);- $arElementIDList - list of element IDs for intersection
Method applies the array_intersect() function to the array of element IDs $arElementIDList and the collection.
$obList = ElementCollection::make([1, 2, 10, 15]);
$arElementIDList = [2,14,18];
$obList->applySorting($arElementIDList);- $arElementIDList - list of element IDs to merge
Method applies array_merge() function to the collection and the array of element IDs $arElementIDList.
$obList = ElementCollection::make([1, 2, 10, 15]);
$arElementIDList = [2,14,18];
$obList->merge($arElementIDList);- $arElementIDList - list of element IDs
Method applies array_diff() function to the collection and the array of element IDs $arElementIDList.
$obList = ElementCollection::make([1, 2, 10, 15]);
$arElementIDList = [2,14,18];
$obList->diff($arElementIDList);Method returns an array of objects ElementItem, for all the elements in a collection.
$obList = ElementCollection::make([1, 2, 10, 15]);
$arElementIDList = [2,14,18];
$obList->merge($arElementIDList);- $iCount - count of elements to skip
Method is used in combination with the take() method to specify the count of elements needed to be skipped.
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->skip(2)->take(1);- $iCount - count of elements to be get
Method returns an array of ElementItem objects. Count of elements = $iCount, starting from the position specified in the skip() method. If you send $iCount = 0, you will obtain all elements, starting from the position specified in the skip() method.
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->skip(2)->take(1);- $iElementID - element ID
Method excludes the element with ID = $iElementID from the collection.
$obList = ElementCollection::make([1, 2, 10, 15]);
$obList->exclude(2);- $iCount - count of elements generated
Method returns an array of random ElementItem objects.
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->random(2);- $iPage - current page number
- $iElementOnPage - count of elements on the page
Method returns an array of ElementItem objects for the $iPage page.
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->page(2, 10);Method returns the first ElementItem object in a collection.
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->first();Method returns the last ElementItem object in a collection.
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->last();Method returns the first ElementItem object in a collection and excludes it from the collection
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->shift();Method adds element to the beginning of collection
$obList = ElementCollection::make([1, 2, 10, 15]);
$obList->unshift(4);Method returns the first ElementItem object in a collection and excludes it from the collection.
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->pop();Method adds element to the end of collection
$obList = ElementCollection::make([1, 2, 10, 15]);
$obList->push(4);Method returns array of the values for a given field name.
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->pluck('name');Method returns array of the values for a given field name and applies implode function to array.
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->implode('name', '-');Method returns new collection with next nearest elements.
$obList = ElementCollection::make([1, 2, 10, 15]);
//Collection has elements: 10
$obNewList = $obList->getNearestNext(2);
//Collection has elements: 2,10,15
$obNewList = $obList->getNearestNext(1, 3);
//Collection has elements: 10,15
$obNewList = $obList->getNearestNext(2, 3);
//Collection has elements: 10,15,1
$obNewList = $obList->getNearestNext(2, 3, true);Method returns new collection with previous nearest elements.
$obList = ElementCollection::make([1, 2, 10, 15]);
//Collection has elements: 1
$obNewList = $obList->getNearestPrev(2);
//Collection has elements: 10,2,1
$obNewList = $obList->getNearestPrev(15, 3);
//Collection has elements: 2,1
$obNewList = $obList->getNearestPrev(10, 3);
//Collection has elements: 2,1,15
$obNewList = $obList->getNearestPrev(10, 3, true);- $sKey - collection state key to save
Method saves the state of a collection for it to be obtained later.
$obList = ElementCollection::make([1, 2, 10, 15]);
$obList->save('my_key');
...
$obSavedList = ElementCollection::make()->saved('my_key');
//result: $obSavedList == clone $obListMethod allows to set a break point while using xDebug in the case of calling collection methods in Twig templates.
$obList = ElementCollection::make([1, 2, 10, 15]);
$obList->skip(2)->debug()->take(2);You can add methods and properties in collection class with extending constructors.
Example
ElementCollection::extend(function($obCollection) {
$obCollection->addDynamicMethod('my_method', function($arElementIDList) use ($obCollection) {
return $obCollection->diff($arElementIDList);
});
});