Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to preserve keys in WireArray slice method #545

Open
ethnoxcom opened this issue Oct 9, 2024 · 0 comments
Open

Add option to preserve keys in WireArray slice method #545

ethnoxcom opened this issue Oct 9, 2024 · 0 comments

Comments

@ethnoxcom
Copy link

ethnoxcom commented Oct 9, 2024

Short description of the enhancement

Sometimes it's necessary to preserve the original keys in the WireArray result of the slice method.

Current vs. suggested behavior

Current method:

public function slice($start, $limit = 0) {
    if($limit) {
        $slice = array_slice($this->data, $start, $limit);
    } else {
        $slice = array_slice($this->data, $start);
    }
    $items = $this->makeNew();
    $items->import($slice);
    $items->setTrackChanges(true); 
    return $items; 
}

Proposed changes to the method:

public function slice($start, $limit = 0, $preserveKeys = false)
{
    if ($limit) {
        $slice = array_slice($this->data, $start, $limit);
    } else {
        $slice = array_slice($this->data, $start);
    }
    $items = $this->makeNew();
    if ($preserveKeys) {
        $items->setArray($slice);
    } else {
        $items->import($slice);
    }
    $items->setTrackChanges(true);
    return $items;
}

Why would the enhancement be useful to users?

This will improve the flexibility of the slice method and will be more aligned with PHP's array_slice method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant