Repository: ProcessMaker/nayra (php)

Issue:
Add findFirst method to collections
As a developer I would like to get the first element of a collection that matches a condition, so I can work with that element.

To do that, add the following implementation to `CollectionInterface`:

```
    /**
     * Find first element of the collection that match the $condition.
     *
     * @param callable $condition
     *
     * @return mixed
     */
    public function findFirst(callable $condition);
```


Pull request description:
Resolves #174 Implement Collection::findFirst
This PR includes:

- Implementation of Collection::findFirst
- Unit tests for Collection::findFirst


Patch:
diff --git a/src/ProcessMaker/Nayra/Bpmn/Collection.php b/src/ProcessMaker/Nayra/Bpmn/Collection.php
index 8b2bc1e..28fa266 100755
--- a/src/ProcessMaker/Nayra/Bpmn/Collection.php
+++ b/src/ProcessMaker/Nayra/Bpmn/Collection.php
@@ -57,6 +57,18 @@ class Collection implements CollectionInterface
         return new Collection(array_filter($this->items, $condition));
     }
 
+    /**
+     * Find first element of the collection that match the $condition.
+     *
+     * @param callable $condition
+     *
+     * @return mixed
+     */
+    public function findFirst(callable $condition)
+    {
+        return current(array_filter($this->items, $condition));
+    }
+
     /**
      * Add an element to the collection.
      *
diff --git a/src/ProcessMaker/Nayra/Contracts/Bpmn/CollectionInterface.php b/src/ProcessMaker/Nayra/Contracts/Bpmn/CollectionInterface.php
index 882ceec..874c273 100755
--- a/src/ProcessMaker/Nayra/Contracts/Bpmn/CollectionInterface.php
+++ b/src/ProcessMaker/Nayra/Contracts/Bpmn/CollectionInterface.php
@@ -28,6 +28,15 @@ interface CollectionInterface extends SeekableIterator
      */
     public function find($condition);
 
+    /**
+     * Find first element of the collection that match the $condition.
+     *
+     * @param callable $condition
+     *
+     * @return mixed
+     */
+    public function findFirst(callable $condition);
+
     /**
      * Add an element to the collection.
      *


Test patch:
diff --git a/tests/unit/ProcessMaker/Nayra/Bpmn/CollectionTest.php b/tests/unit/ProcessMaker/Nayra/Bpmn/CollectionTest.php
index f9aa064..84632ca 100755
--- a/tests/unit/ProcessMaker/Nayra/Bpmn/CollectionTest.php
+++ b/tests/unit/ProcessMaker/Nayra/Bpmn/CollectionTest.php
@@ -183,4 +183,18 @@ class CollectionTest extends TestCase
         $this->assertEquals(1, $this->object->key());
         $this->assertEquals(2, $this->object->current());
     }
+
+    /**
+     * Find first element of the collection that match the $condition.
+     *
+     */
+    public function testFindFirst()
+    {
+        $this->assertEquals(2, $this->object->findFirst(function ($item) {
+            return $item === 2;
+        }));
+        $this->assertEquals(1, $this->object->findFirst(function ($item) {
+            return $item % 2;
+        }));
+    }
 }