arpablue / fieldlist
这是一个用于通过键来管理值的对象,但通过不区分大小写的键来识别每个值。这允许使用不区分大小写的方法获取值,并比较两个列表,使用严格模式来精确比较键和值,或者使用比较模式只验证当前键和值。
Requires (Dev)
- phpunit/phpunit: ^10.5
README
这是一个用于使用不区分大小写来管理键和值的组件,通过使用字段的名称来唯一标识每个字段。这有助于获取值,无论键是称为、大写还是小写。此对象允许您与其他列表进行比较。只能使用 SET 方法修改元素或使用 PUT 方法修改元素或添加此元素(如果它不在列表中)。
安装 FieldList
要安装库,可以使用 composer 执行以下命令。
composer require ArpaBlue
初始化 FieldList
要初始化对象,需要添加库的引用。
use ArpaBlue
初始化 FieldList 与其他对象类似。
$fieldList = new FieldList();
添加元素
要将元素添加到列表中,必须使用 PUT 方法。
$list = new FieldList();
$list->put("name","Alan");
$list->put("lastname","Brooks");
$list->put("email","alan.brooks@test.com");
$list->put("phone","2223336666");
$list->put("age","21");
修改元素
要修改元素,我们可以使用两种方法。
PUT 方法
PUT 方法可以修改列表中的值。
$list = new FieldList();
$list->put("name","Alan");
$list->put("lastname","Brooks");
$list->put("email","alan.brooks@test.com");
echo "\nContains: " . $list; // 输出 -> Contains: {{"name":"Alan"},{"lastname":"Brooks"},{"email":"alan.brooks@test.com"}}
$list->put("lastname","Woods");
echo "\nContains: " . $list; // 输出 -> Contains: {{"name":"Alan"},{"lastname":"Woods"},{"email":"alan.brooks@test.com"}}
但如果元素不存在,则将其添加到列表中。
$list->put("phone",2223336666);
echo "\nContains: " . $list; // 输出 -> Contains: {{"name":"Alan"},{"lastname":"Woods"},{"email":"alan.brooks@test.com"},{"phone":2223336666}}
SET 方法
SET 方法可以修改列表中元素的值,如果元素不在列表中,则不会将其添加到列表中。
use ArpaBlue
$list = new FieldList();
$list->put("name","Alan");
$list->put("lastname","Brooks");
$list->put("email","alan.brooks@test.com");
echo "\nContains: " . $list; // 输出 -> Contains: {{"name":"Alan"},{"lastname":"Brooks"},{"email":"alan.brooks@test.com"}}
$list->set("lastname","silverman");
echo "\n包含: " . $list; // 输出 -> 包含: {{"name":"Alan"},{"lastname":"silverman"},{"email":"alan.brooks@test.com"},{"phone":2223336666}}
但如果元素不在列表中,则不会添加到列表中。
$list->set("country","England");
echo "\n包含: " . $list; // 输出 -> 包含: {{"name":"Alan"},{"lastname":"silverman"},{"email":"alan.brooks@test.com"},{"phone":2223336666}}
类方法
ToJSON
此方法返回列表的结构作为JSON格式的字符串,此方法用于将当前列表转换为字符串。
$list = new FieldList();
$list->put("name","Alan");
$list->put("lastname","Brooks");
$list->put("email","alan.brooks@test.com");
echo "\nList: " . $list; // 输出 -> List: {{"name":"Alan"},{"lastname":"Brooks"},{"email":"alan.brooks@test.com"}}
移除元素
该类包含两个用于移除元素的方法。
移除元素
它移除指定名称或字段键的元素,必须指定字段名称或键。
$list = new FieldList();
$list->put("name","Alan");
$list->put("lastname","Brooks");
$list->put("email","alan.brooks@test.com");
$list->put("phone","3334445555");
$list->put("age",21);
$list->removeAll();
echo "\nList: " . $list; // 输出 -> List: {}
移除所有元素
它移除列表中的所有元素。
$list = new FieldList();
$list->put("name","Alan");
$list->put("lastname","Brooks"); $list->put("email","alan.brooks@test.com"); $list->put("phone","3334445555"); $list->put("age",21);
$list->remove("email");
echo "*nList: " . $list; // 输出 -> List: {{"name":"Alan"},{"lastname":"Brooks"},{"phone":"3334445555"},{"age":21}}*
size()
它返回当前列表的字段数量。
$list = new FieldList();
$list->put("name","Alan");
$list->put("lastname","Brooks");
$list->put("email","alan.brooks@test.com");
$list->put("phone","3334445555");
$list->put("age",21);
echo "\nList length: " . $list->size(); // 输出 -> List length: 5
getNames()
它返回一个包含所有字段名称的数组。
list = new FieldList();
$list->put("name","Alan"); // 位置: 0 $list->put("lastname","Brooks"); // 位置: 1 $list->put("email","alan.brooks@test.com"); // 位置: 2 $list->put("age",21); // 位置: 3 $list->put("phone","3332225555"); // 位置: 4 $list->put("country","Paris"); // 位置: 5 $list->put("language","French"); // 位置: 6
$names = $list->getNames();
print_r( $names ); /* 输出: Array ( [0] => name [1] => lastname [2] => email [3] => age [4] => phone [5] => country [6] => language ) */
exists()
它验证指定的字段名称是否存在。
$list = new FieldList();
$list->put("name","Alan"); $list->put("lastname","Brooks"); $list->put("email","alan.brooks@test.com"); $list->put("age",21); $list->put("phone","3332225555"); $list->put("country","Paris"); $list->put("language","French");
$flag = $list->exists("age"); // 返回 true
if( $flag ){ echo "字段存在!!!"; }
cloneMe()
它返回另一个具有与当前列表元素相同值的FieldList,这些元素不是相同的。
$exp = new FieldList();
$exp->put("name","Alan");
$exp->put("lastname","Brooks");
$exp->put("email","alan.brooks@test.com");
$current = $exp->cloneMe();
echo "\n克隆列表: " . $exp; // 输出 -> 克隆列表: {{"name":"Alan"},{"lastname":"Brooks"},{"email":"alan.brooks@test.com"}}
copy()
此方法将当前列表中存在的另一个列表的字段值复制,另一个列表中的值为null的字段值不会修改或分配给当前列表的字段。
$list = new FieldList();
$list->put("name","Alan"); $list->put("lastname","Brooks"); $list->put("email","alan.brooks@test.com"); $list->put("age",21); $list->put("phone","3332225555"); $list->put("country","Paris"); $list->put("language","French");
$anotherList = new FieldList();
$anotherList->put("lastname","none"); $anotherList->put("email","none"); $anotherList->put("city","Arizona");
$anotherList->copy( $list );
echo "当前列表: " . $anotherList; // 输出 -> 当前列表: {{"lastname":"Brooks"},{"email":"alan.brooks@test.com"},{"city":"Arizona"}}}
copyNullToo()
它将当前列表和另一个列表中存在的字段值复制,这包括null值。
$list = new FieldList();
$list->put("name","Alan"); $list->put("lastname","Brooks"); $list->put("email","alan.brooks@test.com"); $list->put("age",21); $list->put("phone","3332225555"); $list->put("country","Paris"); $list->put("language","French");
$anotherList = new FieldList();
$anotherList->put("lastname","none"); $anotherList->put("email","none"); $anotherList->put("city","Arizona");
$anotherList->copyNullToo( $list );
echo "当前列表: " . $anotherList; // 输出 -> 当前列表: {{"lastname":"Brooks"},{"email":"alan.brooks@test.com"},{"city":null}}
copyAll()
它删除当前列表的所有属性并复制目标列表的所有字段。
$list = new FieldList();
$list->put("name","Alan");
$list->put("lastname","Brooks");
$list->put("email","alan.brooks@test.com");
$copyList = new FieldList();
$copyList->copy( $list );
echo "\n复制列表: " . $copyList; // 输出 -> 复制列表: {{"name":"Alan"},{"lastname":"Brooks"},{"email":"alan.brooks@test.com"}}
compareTo()
它比较当前列表的字段与目标列表中具有相同名称的字段;目标列表上的其他字段将被丢弃。如果当前列表中某个字段不存在于目标列表中或具有不同的值,则该方法返回false。
$list = new FieldList();
$list->put("name","Alan");
$list->put("lastname","Brooks");
$list->put("email","alan.brooks@test.com");
$list->put("age",21);
$list->put("phone","3332225555");
$anotherList = new FieldList();
$anotherList->put("lastname","Brooks");
$anotherList->put("email","alan.brooks@test.com");
if( $anotherList->compareTo( $list ) ){ // 预期值为true
echo "\n字段存在且值相同。";
}else{
echo "\n可能某些字段不存在或值不同。";
}
fieldsExistIn()
它验证当前列表的字段是否存在于目标列表中,在此过程中忽略值和其他字段。
$list = new FieldList();
$list->put("name","Alan");
$list->put("lastname","Brooks");
$list->put("email","alan.brooks@test.com");
$list->put("age",21);
$list->put("phone","3332225555");
$anotherList = new FieldList();
$anotherList->put("lastname","Brooks");
$anotherList->put("email","alan.brooks@test.com");
if( $anotherList->fieldsExistIn( $list ) ){ // 预期值为true
echo "\n字段存在于列表中。";
}else{
echo "\n可能某些字段不存在于列表中。";
}
getIndex()
它从列表的特定位置返回值。
$list = new FieldList();
$list->put("name","Alan");
$list->put("lastname","Brooks");
$list->put("email","alan.brooks@test.com");
$list->put("age",21);
$list->put("phone","3332225555");
for( $i = 0; $i < $list->size(); $i++ ){
echo "\n..".$i.") ".$list->getIndex($i); // 获取特定索引的值。
}
getValueByIndex()
它使用字段的位位置返回列表的值,如果指定的位置不存在字段,则返回null。
$list = new FieldList();
$list->put("name","Alan"); // 位置: 0 $list->put("lastname","Brooks"); // 位置: 1 $list->put("email","alan.brooks@test.com"); // 位置: 2 $list->put("age",21); // 位置: 3 $list->put("phone","3332225555"); // 位置: 4 $list->put("country","Paris"); // 位置: 5 $list->put("language","French"); // 位置: 6
$phone = $list->getValueByIndex( 4 );
输出 "\n电话号码: " . $phone; // 输出: 电话号码: 3332225555
许可证
本项目采用MIT许可证授权。
最后更新日期:2024年6月1日