Skip to content

Commit ad7161f

Browse files
committed
- Added sort and filter methods
- Refactoring
1 parent 1eda619 commit ad7161f

File tree

2 files changed

+208
-5
lines changed

2 files changed

+208
-5
lines changed

src/FluentArray.php

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public function all(): array
198198
*/
199199
public function pluck(string $key)
200200
{
201-
$fluentArray = new static();
201+
$fluentArray = clone $this;
202202

203203
$this->each(function ($item) use ($key, $fluentArray) {
204204
if ($item instanceof static && $item->has($key)) {
@@ -234,14 +234,73 @@ public function count(): int
234234
return count($this->storage);
235235
}
236236

237+
/**
238+
* @param int $sortFlags
239+
* @return self
240+
*/
241+
public function sort(int $sortFlags = SORT_REGULAR)
242+
{
243+
asort($this->storage, $sortFlags);
244+
return $this;
245+
}
246+
247+
/**
248+
* @param int $sortFlags
249+
* @return self
250+
*/
251+
public function rsort(int $sortFlags = SORT_REGULAR)
252+
{
253+
arsort($this->storage, $sortFlags);
254+
return $this;
255+
}
256+
257+
/**
258+
* @param callable $callback
259+
* @return self
260+
*/
261+
public function usort(callable $callback)
262+
{
263+
usort($this->storage, $callback);
264+
return $this;
265+
}
266+
267+
/**
268+
* @param int $sortFlags
269+
* @return self
270+
*/
271+
public function ksort(int $sortFlags = SORT_REGULAR)
272+
{
273+
ksort($this->storage, $sortFlags);
274+
return $this;
275+
}
276+
277+
/**
278+
* @param int $sortFlags
279+
* @return self
280+
*/
281+
public function krsort(int $sortFlags = SORT_REGULAR)
282+
{
283+
krsort($this->storage, $sortFlags);
284+
return $this;
285+
}
286+
237287
/**
238288
* @param callable $callback
239289
* @return self
240290
*/
241291
public function map(callable $callback)
242292
{
293+
$keys = $this->keys();
243294
$values = array_map($callback, $this->values(), $this->keys());
244-
return static::fromArray(array_combine($this->keys(), $values));
295+
296+
$fluentArray = clone $this;
297+
298+
foreach ($values as $index => $value) {
299+
$key = $keys[$index];
300+
$fluentArray->set($key, $value);
301+
}
302+
303+
return $fluentArray;
245304
}
246305

247306
/**
@@ -259,6 +318,23 @@ public function each(callable $callback)
259318
return $this;
260319
}
261320

321+
/**
322+
* @param callable|null $callback
323+
* @return self
324+
*/
325+
public function filter(callable $callback = null)
326+
{
327+
$fluentArray = clone $this;
328+
329+
$this->each(function($value, $key) use ($callback, $fluentArray) {
330+
if (isset($callback) ? $callback($value, $key) : !empty($value)) {
331+
$fluentArray->set($key, $value);
332+
}
333+
});
334+
335+
return $fluentArray;
336+
}
337+
262338
/**
263339
* @param array $array
264340
* @return self
@@ -321,6 +397,11 @@ public function unserialize($serialized)
321397
$this->storage = unserialize($serialized);
322398
}
323399

400+
public function __clone()
401+
{
402+
$this->clean();
403+
}
404+
324405
/**
325406
* @param string $method
326407
* @param array $args

tests/FluentArrayTest.php

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,97 @@ public function testCountMethod()
301301
$this->assertCount(3, $fluentArray);
302302
}
303303

304-
public function testAsortMethod()
304+
public function testSortMethod()
305305
{
306-
// todo implement
306+
$fluentArray = (new FluentArray())
307+
->push('10')
308+
->set('foo', 1)
309+
->set('bar', 5);
310+
311+
$this->assertSame(
312+
['foo' => 1, 'bar' => 5, '10'],
313+
$fluentArray->sort()->toArray()
314+
);
315+
316+
$this->assertSame(
317+
['foo' => 1, '10', 'bar' => 5],
318+
$fluentArray->sort(SORT_STRING)->toArray()
319+
);
320+
}
321+
322+
public function testRsortMethod()
323+
{
324+
$fluentArray = (new FluentArray())
325+
->push('10')
326+
->set('foo', 1)
327+
->set('bar', 5);
328+
329+
$this->assertSame(
330+
['10', 'bar' => 5, 'foo' => 1],
331+
$fluentArray->rsort()->toArray()
332+
);
333+
334+
$this->assertSame(
335+
['bar' => 5, '10', 'foo' => 1],
336+
$fluentArray->rsort(SORT_STRING)->toArray()
337+
);
338+
}
339+
340+
public function testUsortMethod()
341+
{
342+
$sourceFluentArray = (new FluentArray())
343+
->push(7)
344+
->push(1)
345+
->push(5);
346+
347+
$resultFluentArray = $sourceFluentArray->usort(function ($a, $b) {
348+
return $a <=> $b;
349+
});
350+
351+
$this->assertSame(
352+
[1, 5, 7],
353+
$resultFluentArray->toArray()
354+
);
307355
}
308356

309357
public function testKsortMethod()
310358
{
311-
// todo implement
359+
$fluentArray = (new FluentArray())
360+
->set('2', 10)
361+
->set('10', 3)
362+
->set('1', 8)
363+
->set('a', 1)
364+
->set('b', 5);
365+
366+
$this->assertSame(
367+
['a' => 1, 'b' => 5, '1' => 8, '2' => 10, '10' => 3],
368+
$fluentArray->ksort()->toArray()
369+
);
370+
371+
$this->assertSame(
372+
['1' => 8, '10' => 3, '2' => 10, 'a' => 1, 'b' => 5],
373+
$fluentArray->ksort(SORT_STRING)->toArray()
374+
);
375+
}
376+
377+
public function testKrsortMethod()
378+
{
379+
$fluentArray = (new FluentArray())
380+
->set('2', 10)
381+
->set('10', 3)
382+
->set('1', 8)
383+
->set('a', 1)
384+
->set('b', 5);
385+
386+
$this->assertSame(
387+
['10' => 3, '2' => 10, '1' => 8, 'b' => 5, 'a' => 1],
388+
$fluentArray->krsort()->toArray()
389+
);
390+
391+
$this->assertSame(
392+
['b' => 5, 'a' => 1, '2' => 10, '10' => 3, '1' => 8],
393+
$fluentArray->krsort(SORT_STRING)->toArray()
394+
);
312395
}
313396

314397
public function testMapMethod()
@@ -343,6 +426,25 @@ public function testEachMethod()
343426
$this->assertSame([1], $values);
344427
}
345428

429+
public function testFilterMethod()
430+
{
431+
$sourceFluentArray = (new FluentArray())
432+
->set('foo', 0)
433+
->set('bar', 3);
434+
435+
$resultFluentArray = $sourceFluentArray->filter();
436+
437+
$this->assertNull($resultFluentArray->get('foo'));
438+
$this->assertSame(3, $resultFluentArray->get('bar'));
439+
440+
$resultFluentArray = $sourceFluentArray->filter(function ($value, $key) {
441+
return $key == 'foo';
442+
});
443+
444+
$this->assertSame(0, $resultFluentArray->get('foo'));
445+
$this->assertNull($resultFluentArray->get('bar'));
446+
}
447+
346448
public function testFromArrayMethod()
347449
{
348450
$array = [
@@ -423,4 +525,24 @@ public function testUnserializeMethod()
423525
$this->assertSame(3, $fluentArray->get('bar')->get(0));
424526
$this->assertSame(4, $fluentArray->get('bar')->get(1));
425527
}
528+
529+
public function testCloning()
530+
{
531+
$fluentArrayConfig = FluentArray::globalConfig()
532+
->set('option', 'value');
533+
534+
$sourceFluentArray = (new FluentArray($fluentArrayConfig))
535+
->push(1)
536+
->set('foo', 'bar');
537+
538+
$clonedFluentArray = clone $sourceFluentArray;
539+
540+
$this->assertSame(
541+
'value',
542+
$clonedFluentArray->config()->get('option')
543+
);
544+
545+
$this->assertNull($clonedFluentArray->get(0));
546+
$this->assertNull($clonedFluentArray->get('foo'));
547+
}
426548
}

0 commit comments

Comments
 (0)