Key value collections

Key value collections are widely used in Sitemagic CMS. Especially when working with data sources.

$kvc = new SMKeyValueCollection();

// Assigning values
$kvc["key1"] = "val1";
$kvc["key2"] = "val2";
$kvc->Add(new SMKeyValue("key3", "val3");

// Reading values
foreach ($kvc->GetKeys() as $key)
    echo $key . " = " . $kvc[$key];
The example above demonstrates how the key value collection can be used. Both keys and values must be of type String. The collection implements the PHP interfaces ArrayAccess, Iterator, Countable, and is as such compatible with ordinary PHP arrays.


SMKeyValueCollection class

Function
Return type
Description
__construct()

Create instance of SMKeyValueCollection. Keys are per default case sensitive.
__construct($type:SMKeyValueCollectionType)

See description of __construct(). Use the argument to specify whether to use case sensitive keys or not.
GetCollectionType()
SMKeyValueCollectionType Get type indicating whether key value collection is case sensitive or not (on keys).
Add($kv:SMKeyValue)

Add new key value pair to collection. The following is also possible: $kvc["newkey"] = "newvalue".
GetKeys()
String[]
Get all keys from collection.
GetValues() String[] Get all values from collection.


SMKeyValue class

Function
Return type
Description
__construct($key:string)

Create instance of SMKeyValue with the specified key.
__construct($key:string, $value:string)

See description for __construct(). Define value associated with key.
GetKey()
String Get key.
GetValue()
String
Get value.
SetValue($value:string)

Set value.


SMKeyValueCollectionType enum

Enum value
Description
::$CaseInsensitive
Represents a case insensitive key value collection
::$CaseSensitive
Represents a case sensitive key value collection