PHP code example of robertogallea / laravel-visitor
1. Go to this page and download the library: Download robertogallea/laravel-visitor library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
public function process()
{
$visitor = new MyVisitor([
new FooClass(),
new BarClass(),
new BazClass(),
]);
$visitor->execute();
$result = $visitor->getResult();
}
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 .= ... ;
}
}
public function visitBook(Book $book) {
...
}
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;
}
}
use robertogallea\LaravelVisitor\Models\Visitor;
class XMLVisitor extends Visitor
{
private $xml = '';
public function visitMagazine(Magazine $magazine)
{
$this->xml .= '<magazine title="' . $magazine->getTitle() . '" ' .
'issue="' . $magazine->getMonth() . ' ' . $magazine->getYear() . '"></magazine>' . PHP_EOL;
}
public function getResult()
{
return $this->xml;
}
}
$xmlCatalog = new XMLVisitor([
new Magazine('PHP programming', 'July', 2019)
new Magazine('The art of woodworking', 'August', 2019)
]);
$xmlCatalog->execute();
echo($xmlCatalog->getResult());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.