Browse Source

Rename to takeLast

Jose Lorenzo Rodriguez 7 years ago
parent
commit
cc1372d663

+ 19 - 19
src/Collection/CollectionInterface.php

@@ -558,6 +558,25 @@ interface CollectionInterface extends Iterator, JsonSerializable
     public function take($size = 1, $from = 0);
 
     /**
+     * Returns the last N elements of a collection
+     *
+     * ### Examples:
+     *
+     * ```
+     * $items = [1, 2, 3, 4, 5];
+     *
+     * $last = (new Collection($items))->lastN(3);
+     *
+     * // Result will look like this when converted to array
+     * [3, 4, 5];
+     * ```
+     *
+     * @param int $howMany The number of elements at the end of the collection
+     * @return \Cake\Collection\CollectionInterface
+     */
+    public function takeLast($howMany);
+
+    /**
      * Returns a new collection that will skip the specified amount of elements
      * at the beginning of the iteration.
      *
@@ -620,25 +639,6 @@ interface CollectionInterface extends Iterator, JsonSerializable
     public function last();
 
     /**
-     * Returns the last N elements of a collection
-     *
-     * ### Examples:
-     *
-     * ```
-     * $items = [1, 2, 3, 4, 5];
-     *
-     * $last = (new Collection($items))->lastN(3);
-     *
-     * // Result will look like this when converted to array
-     * [3, 4, 5];
-     * ```
-     *
-     * @param int $howMany The number of elements at the end of the collection
-     * @return \Cake\Collection\CollectionInterface
-     */
-    public function lastN($howMany);
-
-    /**
      * Returns a new collection as the result of concatenating the list of elements
      * in this collection with the passed list of elements
      *

+ 1 - 1
src/Collection/CollectionTrait.php

@@ -402,7 +402,7 @@ trait CollectionTrait
     /**
      * {@inheritDoc}
      */
-    public function lastN($howMany)
+    public function takeLast($howMany)
     {
         if ($howMany < 1) {
             throw new \InvalidArgumentException("lastN requires a number greater than 0");

+ 11 - 11
tests/TestCase/Collection/CollectionTest.php

@@ -2169,7 +2169,7 @@ class CollectionTest extends TestCase
     }
 
     /**
-     * Tests the lastN() method
+     * Tests the takeLast() method
      *
      * @dataProvider simpleProvider
      * @return void
@@ -2177,13 +2177,13 @@ class CollectionTest extends TestCase
     public function testLastN($data)
     {
         $collection = new Collection($data);
-        $result = $collection->lastN(3)->toArray();
+        $result = $collection->takeLast(3)->toArray();
         $expected = ['b' => 2, 'c' => 3, 'd' => 4];
         $this->assertEquals($expected, $result);
     }
 
     /**
-     * Tests the lastN() method with overflow
+     * Tests the takeLast() method with overflow
      *
      * @dataProvider simpleProvider
      * @return void
@@ -2191,13 +2191,13 @@ class CollectionTest extends TestCase
     public function testLasNtWithOverflow($data)
     {
         $collection = new Collection($data);
-        $result = $collection->lastN(10)->toArray();
+        $result = $collection->takeLast(10)->toArray();
         $expected = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];
         $this->assertEquals($expected, $result);
     }
 
     /**
-     * Tests the lastN() with and odd numbers collection
+     * Tests the takeLast() with and odd numbers collection
      *
      * @dataProvider simpleProvider
      * @return void
@@ -2205,29 +2205,29 @@ class CollectionTest extends TestCase
     public function testLasNtWithOddData($data)
     {
         $collection = new Collection($data);
-        $result = $collection->take(3)->lastN(2)->toArray();
+        $result = $collection->take(3)->takeLast(2)->toArray();
         $expected = ['b' => 2, 'c' => 3];
         $this->assertEquals($expected, $result);
     }
 
     /**
-     * Tests the lastN() with countable collection
+     * Tests the takeLast() with countable collection
      *
      * @return void
      */
     public function testLasNtWithCountable()
     {
         $collection = new Collection(new CountableIterator(range(0, 5)));
-        $result = $collection->lastN(2)->toList();
+        $result = $collection->takeLast(2)->toList();
         $this->assertEquals([4, 5], $result);
 
         $collection = new Collection(new CountableIterator(range(0, 5)));
-        $result = $collection->lastN(1)->toList();
+        $result = $collection->takeLast(1)->toList();
         $this->assertEquals([5], $result);
     }
 
     /**
-     * Tests the lastN() with countable collection
+     * Tests the takeLast() with countable collection
      *
      * @dataProvider simpleProvider
      * @return void
@@ -2236,7 +2236,7 @@ class CollectionTest extends TestCase
     {
         $collection = new Collection($data);
         $this->expectException(\InvalidArgumentException::class);
-        $result = $collection->lastN(-1)->toArray();
+        $collection->takeLast(-1)->toArray();
     }
 
     /**