-
Notifications
You must be signed in to change notification settings - Fork 71
Description
I'm reviewing Yii2 BaseArrayHelper and comparing it to what we have. Their BaseArrayHelper is basically a replacement for TMap. It adds a few things that we don't do, like various ways of ordering, indexing, and organizing arrays, merging, sorting, determining the type of array (associative or indexed), and some html functions. However, there are some things we can learn and do better.
- TMap could use these functions:
public function removeItem($item): null|array
{
$return = [];
foreach ($this->toArray() as $key => $value) {
if ($item === $value) {
$return[$key] = $this->remove($key);
}
}
return $return;
}
public function itemAtIndex($index): null|mixed
{
return array_values($this->toArray())[$index] ?? null;
}
// maybe these introspective methods as well. these use a common modality. Even if custom made by hand, the code would look like Yii2 (that's what I mean by common modality).
public function getIsAssociative (bool $isAllString = true):bool;
public function getIsIndexed(bool $consecutive = false):bool;
-
TList::removeAllItem to remove all of an item, to match TMap::removeItem. an array of indices removed is returned.
or mayberemoveshould remove all of an item rather than just the first instance, that would be more consistent and parallel yii2.
@ctrlaltca Do you have any insight into what to do here? should we update theremovefunction or add a newremoveAllItemfunction?
If we update the existing, it would return false if not found, the index if found, and an array if multiple are found. I like updating the existing function to remove all an item rather than having a second function. It's more consistent with expectation. -
TMap and TList could be updated such that their itemAt, add/insertAt, remove[At], indexOf accepts "." for traversal of the array and returning, setting, and unsetting of a sub-array or item-object property. keyOf and indexOf could, upon an optional parameter, become recursive and search sub-arrays, returning the path. This is something I am looking at as a function of TEXIF to traverse its tree and traversal of JPEG metadata (IPTC, EXIF, XMP) accessibly via "EXIF.GPSInfo.GPSLatitude". This would be easier if it was already built into the TList/TMap class.
The TList and TMap index/key could act like this "0.10.mapkeyitem" to get, and set an item in the structure. and "0.10.mapkeyitem.propertyA" to get and set internal properties.
@ctrlaltca Any thoughts on implications of this functionality in general? Do you like the idea? good? bad? important? low value? does it add any value? what about "competing" with yii[2/3]?
$map->add('0.10.mapkey.propertya', $value) may seem odd instead of $map[0][10]['mapkey']->setPropertyA($value), but this is the intended use case:
$list['0.10.mapkey.propertya'] = $value;
I suggest that when adding, a special end index "NULL" be used for inserting at the end of the map/list.
In this way, $map['key'][] = $value becomes $map['key.NULL'] = $value
While this could be implemented as a subclass of TMap and TList, like TMapTree and TListTree, my thinking is that adding this to the core would be beneficial in the long run. Having these functions on a basic array is awesome and why I was looking at it to begin with.
there needs to be regression and unit tests for this as well, of course.
This is a low priority enhancement.