PHPackages                             robertogallea/laravel-visitor - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. robertogallea/laravel-visitor

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

robertogallea/laravel-visitor
=============================

Visitor pattern implementation for Laravel

1.0.0(6y ago)1391MITPHPCI failing

Since Sep 5Pushed 5y ago2 watchersCompare

[ Source](https://github.com/robertogallea/Laravel-Visitor)[ Packagist](https://packagist.org/packages/robertogallea/laravel-visitor)[ RSS](/packages/robertogallea-laravel-visitor/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (3)Versions (2)Used By (0)

LaravelVisitor
==============

[](#laravelvisitor)

1. Introduction
---------------

[](#1-introduction)

LaravelVisitor is a [Visitor Design Pattern](https://it.wikipedia.org/wiki/Visitor) implementation for Laravel. It allows to easily execute processing of collections of arbitrary elements, without requiring to use repeated conditionals, thus improving code abstraction.

Without Visitor:

```
public function process()
{
  $result = '';
  foreach ($this->elements as $element) {
    if ($element instanceof FooClass) {
      $result .= ((FooClass)$element)->getData();
    } elseif ($element instanceof BarClass) {
      $result .= ((BarClass)$element)->getData();
    } elseif ($element instanceof BazClass) {
      $result .= ((BazClass)$element)->getData();
    }
  }
}
```

With Visitor:

```
public function process()
{
  $visitor = new MyVisitor([
    new FooClass(),
    new BarClass(),
    new BazClass(),
  ]);

  $visitor->execute();

  $result = $visitor->getResult();
}
```

All of the complexity is hidden in the `MyVisitor` class, which must define methods for processing classes. In the previous example, `MyVisitor` would be implemented as:

```
class MyVisitor extends Visitor
{
  private $result;

  public function getResult()
  {
    return $this->result;
  }

  public function visitFooClass(FooClass $fooClass)
  {
    $this->result .= ... ;
  }

  public function visitBarClass(BarClass $fooClass)
  {
    $this->result .= ... ;
  }

  public function visitBazClass(BazClass $fooClass)
  {
    $this->result .= ... ;
  }
}
```

Additionally this enforces SRP principle, since Domain Objects don't have to implement representational methods, which are only responsibility of the Visitor classes implementation (especially if several are required).

2. Installation
---------------

[](#2-installation)

Install the package via composer:

`composer require robertogallea/laravel-visitor`

3. Usage
--------

[](#3-usage)

For using the package, you need to define at least one `Visitor` and some `Visitee` classes.

### 3.1. `Visitee`s implementation

[](#31-visitees-implementation)

The only requirement for `Visitee`s is to use the `Visitable` trait, so you can make any class visitable.

### 3.2. `Visitor`s implementation

[](#32-visitors-implementation)

A `Visitor` class must impelemnt the `CanVisit` interface and subclass the `Visitor` abstract class, by defining the `getResult()` method.

Additionally, for each defined `Visitee`'s you have to implement a processing method of your choice. For example, if you have a `Book` Visitee, you must define the method:

```
public function visitBook(Book $book) {
  ...
}
```

### 3.3. `Visitor`s generation

[](#33-visitors-generation)

To generate `Visitor`, you can launch the following artisan commands:

`php artisan make:visitor MyVisitor`

which by default creates classes in the `Visitors` folder.

4. Example usage:
-----------------

[](#4-example-usage)

### 4.1. `Visitee` implementation

[](#41-visitee-implementation)

Magazine.php

```
use robertogallea\LaravelVisitor\Models\Visitable;

class Magazine
{
    use Visitable;

    private $title;
    private $month;
    private $year;

    public function __construct($title, $month, $year)
    {
        $this->title = $title;
        $this->month = $month;
        $this->year = $year;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function getMonth()
    {
        return $this->month;
    }

    public function getYear()
    {
        return $this->year;
    }

}
```

### 4.2. `Visitor` implementation

[](#42-visitor-implementation)

XMLVisitor.php

```
use robertogallea\LaravelVisitor\Models\Visitor;

class XMLVisitor extends Visitor
{
    private $xml = '';

    public function visitMagazine(Magazine $magazine)
    {
        $this->xml .= '' . PHP_EOL;
    }

    public function getResult()
    {
        return $this->xml;
    }
}
```

### 4.3. Client code

[](#43-client-code)

```
  $xmlCatalog = new XMLVisitor([
    new Magazine('PHP programming', 'July', 2019)
    new Magazine('The art of woodworking', 'August', 2019)
  ]);

  $xmlCatalog->execute();

  echo($xmlCatalog->getResult());
```

will produce the following output:

```

```

5. Issues, Questions and Pull Requests
--------------------------------------

[](#5-issues-questions-and-pull-requests)

You can report issues and ask questions in the [issues section](https://github.com/robertogallea/laravel-visitor/issues). Please start your issue with `ISSUE: ` and your question with `QUESTION: `

If you have a question, check the closed issues first.

To submit a Pull Request, please fork this repository, create a new branch and commit your new/updated code in there. Then open a Pull Request from your new branch. Refer to [this guide](https://help.github.com/articles/about-pull-requests/) for more info.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 91.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2442d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/818f547bcf73a82393d9014c85c90c83d760102a8d4dfe806353afb83848a901?d=identicon)[robertogallea](/maintainers/robertogallea)

---

Top Contributors

[![robertogallea](https://avatars.githubusercontent.com/u/19411470?v=4)](https://github.com/robertogallea "robertogallea (11 commits)")[![demiurge-ash](https://avatars.githubusercontent.com/u/40300551?v=4)](https://github.com/demiurge-ash "demiurge-ash (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/robertogallea-laravel-visitor/health.svg)

```
[![Health](https://phpackages.com/badges/robertogallea-laravel-visitor/health.svg)](https://phpackages.com/packages/robertogallea-laravel-visitor)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[orchestra/canvas

Code Generators for Laravel Applications and Packages

21017.2M158](/packages/orchestra-canvas)[illuminate/pipeline

The Illuminate Pipeline package.

9446.6M213](/packages/illuminate-pipeline)[illuminate/pagination

The Illuminate Pagination package.

10532.5M862](/packages/illuminate-pagination)[spatie/laravel-pjax

A pjax middleware for Laravel 5

513371.8k11](/packages/spatie-laravel-pjax)[spatie/laravel-mix-preload

Add preload and prefetch links based your Mix manifest

169176.0k2](/packages/spatie-laravel-mix-preload)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
