On this page
Introduction and Quick Start
laminas-tag provides the ability to work with taggable items. At its foundation, it
provides two classes to work with tags, Laminas\Tag\Item
and Laminas\Tag\ItemList
.
Additionally, it comes with the interface Laminas\Tag\TaggableInterface
, which
allows you to use any of your models as a taggable item in conjunction with the
component.
Laminas\Tag\Item
provides the essential functionality required to work with all
other functionality within the component. A taggable item always consists of a
title and a relative weight (e.g. number of occurrences). It also stores
parameters which are used by the different sub-components.
Laminas\Tag\ItemList
exists to group multiple items together as an array
iterator, and provides additional functionality to calculate absolute weight
values based on the given relative weights of each item in it.
Quick Start
This example illustrates how to create a list of tags and spread absolute weight values over them.
// Create the item list
$list = new Laminas\Tag\ItemList();
// Assign tags to it
$list[] = new Laminas\Tag\Item(['title' => 'Code', 'weight' => 50]);
$list[] = new Laminas\Tag\Item(['title' => 'Laminas', 'weight' => 1]);
$list[] = new Laminas\Tag\Item(['title' => 'PHP', 'weight' => 5]);
// Spread absolute values on the items
$list->spreadWeightValues([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
// Output the items with their absolute values
foreach ($list as $item) {
printf("%s: %d\n", $item->getTitle(), $item->getParam('weightValue'));
}
This will output the three items "Code", "Laminas", and "PHP", with the absolute values 10, 1 and 2:
Code: 10
Laminas: 1
PHP: 2