PHP code example of kepka42 / laravel-mapper

1. Go to this page and download the library: Download kepka42/laravel-mapper 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/ */

    

kepka42 / laravel-mapper example snippets


 

class NameOfMapper extends AbstractMapper
{
    protected $sourceType = "";

    protected $hintType = ""

    /**
     * @param $object
     * @param array $params
     * @return mixed
     */
    public function map($object, $params = [])
    {
        // TODO: Map here
    }
}



return [
    'mappers' => [
        NameOfMapper::class
    ]
];

 

$result = Mapper::map($object, HintType::class);



$result = $mapperContract->map($object, HintType::class);



namespace App\Domain;

/**
 * Class SearchInfo
 * @package App\Domain
 */
class SearchInfo
{
    /** @var string */
    private $name;

    /** @var string */
    private $address;

    /**
     * SearchInfo constructor.
     * @param string $name
     * @param string $address
     */
    public function __construct(
        string $name,
        string $address
    )
    {
        $this->name = $name;
        $this->address = $address;
    }

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName(string $name): void
    {
        $this->name = $name;
    }

    /**
     * @return string
     */
    public function getAddress(): string
    {
        return $this->address;
    }

    /**
     * @param string $address
     */
    public function setAddress(string $address): void
    {
        $this->address = $address;
    }
}




namespace App\Mappers;

use App\Domain\SearchInfo;
use Illuminate\Http\Request;
use kepka42\LaravelMapper\Mapper\AbstractMapper;

/**
 * Class RequestToSearchInfoMapper
 * @package App\Mappers
 */
class RequestToSearchInfoMapper extends AbstractMapper
{
    protected $sourceType = Request::class;

    protected $hintType = SearchInfo::class;

    /**
     * @param Request $object
     * @param array $params
     * @return SearchInfo
     */
    public function map($object, $params = [])
    {
        return new SearchInfo(
            $object->get('name'),
            $object->get('address')
        );
    }
}



return [
    'mappers' => [
        \App\Mappers\RequestToSearchInfoMapper::class,
    ]
];


// ...
use kepka42\LaravelMapper\Facades\Mapper;
// ...
public function index(Request $request): Response
{
    $requestInfo = Mapper::map($request, SearchInfo::class);
    //...
    return new Response('{}');
}


// ...
use kepka42\LaravelMapper\Contracts\MapperContract;
// ...
public function index(Request $request, MapperContract $mapperContract): Response
{
    $requestInfo = $mapperContract->map($request, SearchInfo::class);
    //...
    return new Response('{}');
}
sh
php artisan vendor:publish --provider="kepka42\LaravelMapper\MapperServiceProvider"
sh
php artisan make:mapper NameOfMapper
sh
php artisan make:mapper RequestToSearchInfoMapper