CAttributeCollection
CAttributeCollection implements a collection for storing attribute names and values.
Besides all functionalities provided by
CMap, CAttributeCollection allows you to get and set attribute values like getting and setting properties. For example, the following usages are all valid for a CAttributeCollection object:
$collection->text='text'; // same as: $collection->add('text','text');
echo $collection->text; // same as: echo $collection->itemAt('text');
The case sensitivity of attribute names can be toggled by setting the
caseSensitive property of the collection.
Public Properties
Property |
Type |
Description |
Defined By |
caseSensitive | boolean | whether the keys are case-sensitive. | CAttributeCollection |
count | integer | Returns the number of items in the map. | CMap |
iterator | CMapIterator | Returns an iterator for traversing the items in the list. | CMap |
keys | array | the key list | CMap |
readOnly | boolean | whether this map is read-only or not. | CMap |
Protected Methods
Method |
Description |
Defined By |
setReadOnly() | Sets whether this list is read-only or not | CMap |
Property Details
public boolean $caseSensitive;
whether the keys are case-sensitive. Defaults to false.
Method Details
public mixed __get(string $name) |
$name | string | the property name or the event name |
{return} | mixed | the property value or the event handler list |
Returns a property value or an event handler list by property or event name. This method overrides the parent implementation by returning a key value if the key exists in the collection.
public boolean __isset(string $name) |
$name | string | the property name or the event name |
{return} | boolean | whether the property value is null |
Checks if a property value is null. This method overrides the parent implementation by checking if the key exists in the collection and contains a non-null value.
public void __set(string $name, mixed $value) |
$name | string | the property name or event name |
$value | mixed | the property value or event handler |
Sets value of a component property. This method overrides the parent implementation by adding a new key value to the collection.
public void __unset(string $name) |
$name | string | the property name or the event name |
Sets a component property to be null. This method overrides the parent implementation by clearing the specified key value.
public void add(mixed $key, mixed $value) |
$key | mixed | key |
$value | mixed | value |
Adds an item into the map. This overrides the parent implementation by converting the key to lower case first if caseSensitive is false.
public boolean canGetProperty(string $name) |
$name | string | the property name |
{return} | boolean | whether the property can be read |
Determines whether a property can be read. This method overrides parent implementation by returning true if the collection contains the named key.
public boolean canSetProperty(string $name) |
$name | string | the property name |
{return} | boolean | true |
Determines whether a property can be set. This method overrides parent implementation by always returning true because you can always add a new value to the collection.
public boolean contains(mixed $key) |
$key | mixed | the key |
{return} | boolean | whether the map contains an item with the specified key |
Returns whether the specified is in the map. This overrides the parent implementation by converting the key to lower case first if caseSensitive is false.
public boolean hasProperty(string $name) |
$name | string | the property name |
{return} | boolean | whether the property is defined |
Determines whether a property is defined. This method overrides parent implementation by returning true if the collection contains the named key.
public mixed itemAt(mixed $key) |
$key | mixed | the key |
{return} | mixed | the element at the offset, null if no element is found at the offset |
Returns the item with the specified key. This overrides the parent implementation by converting the key to lower case first if caseSensitive is false.
public void mergeWith(mixed $data, boolean $recursive=true) |
$data | mixed | the data to be merged with, must be an array or object implementing Traversable |
$recursive | boolean | whether the merging should be recursive. |
Source Code: framework/collections/CAttributeCollection.php#202 (
show)
public function mergeWith($data,$recursive=true)
{
if(!$this->caseSensitive && (is_array($data) || $data instanceof Traversable))
{
$d=array();
foreach($data as $key=>$value)
$d[strtolower($key)]=$value;
return parent::mergeWith($d,$recursive);
}
parent::mergeWith($data,$recursive);
}
Merges iterable data into the map.
Existing elements in the map will be overwritten if their keys are the same as those in the source. If the merge is recursive, the following algorithm is performed:
- the map data is saved as $a, and the source data is saved as $b;
- if $a and $b both have an array indexed at the same string key, the arrays will be merged using this algorithm;
- any integer-indexed elements in $b will be appended to $a and reindexed accordingly;
- any string-indexed elements in $b will overwrite elements in $a with the same index;
public mixed remove(mixed $key) |
$key | mixed | the key of the item to be removed |
{return} | mixed | the removed value, null if no such key exists. |
Removes an item from the map by its key. This overrides the parent implementation by converting the key to lower case first if caseSensitive is false.