Skip to content

Commit 847e5c5

Browse files
committed
Add ksort to ArrayObject util
1 parent 7b34e68 commit 847e5c5

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Utils/ArrayObject.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ public function collapse(): self
100100
return new static($results);
101101
}
102102

103+
public function ksort(): self
104+
{
105+
$data = $this->getArrayCopy();
106+
ksort($data);
107+
return new self($data);
108+
}
109+
103110
/**
104111
* Reduce the items to a single value.
105112
*

test/Util/ArrayObjectTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,21 @@ public function testEach(): void
175175

176176
$this->assertEquals($original, $result);
177177
}
178+
179+
public function testKstort()
180+
{
181+
$arrayObject = new ArrayObject([
182+
'z' => 'more test data',
183+
'a' => 'test data',
184+
't' => 'yup moar test data'
185+
]);
186+
187+
$expected = [
188+
'a' => 'test data',
189+
't' => 'yup moar test data',
190+
'z' => 'more test data',
191+
];
192+
193+
$this->assertEquals($expected, $arrayObject->ksort()->getArrayCopy());
194+
}
178195
}

0 commit comments

Comments
 (0)