Fenos\Notifynder\Builder\NotifynderBuilder::loop PHP Method

loop() public method

Loop the data for create multi notifications array.
public loop ( $dataToIterate, Closure $builder )
$dataToIterate
$builder Closure
    public function loop($dataToIterate, Closure $builder)
    {
        if (!$this->isIterable($dataToIterate)) {
            throw new EntityNotIterableException('The data passed must be iterable');
        }
        if (count($dataToIterate) <= 0) {
            throw new IterableIsEmptyException('The Iterable passed must contain at least one element');
        }
        $notifications = [];
        $newBuilder = new self($this->notifynderCategory);
        foreach ($dataToIterate as $key => $data) {
            $builder($newBuilder, $data, $key);
            $notifications[] = $newBuilder->toArray();
        }
        $this->notifications = $notifications;
        return $this;
    }

Usage Example

Example #1
0
 /** @test */
 function it_send_now_a_mutiple_notification()
 {
     $category_name = 'my.category';
     $this->createCategory(['name' => $category_name]);
     $user_ids = [1, 2];
     $sendMultiple = $this->builder->loop($user_ids, function (NotifynderBuilder $builder, $value) use($category_name) {
         return $builder->category($category_name)->to($value)->from(2)->url('www.notifynder.io')->toArray();
     });
     // Send Single
     $this->senders->sendNow($sendMultiple);
     $this->assertCount(2, Notification::all());
 }
All Usage Examples Of Fenos\Notifynder\Builder\NotifynderBuilder::loop