PHP code example of ok / dto-annotation-mapper

1. Go to this page and download the library: Download ok/dto-annotation-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/ */

    

ok / dto-annotation-mapper example snippets


use Doctrine\Common\Annotations\AnnotationReader;

$mapper = new AnnotationMapper(new AnnotationReader(), $entityManager);

/**
 * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
 */
class Product
{
    /**
     * @var string
     *
     * @ORM\Column(type="string", nullable=true)
     */
    protected $article;

    /**
     * @var Material
     *
     * @ORM\ManyToOne(targetEntity="App\Entity\Product\Options\Material")
     */
    protected $material;

    /**
     * @DTO(name="article", type="string")
     */
    public function setArticle(?string $article)
    {
        $this->article = $article;
    }

    /**
     * @DTO(name="material", type="App\Entity\Product\Options\Material", relation="ManyToOne")
     */
    public function setMaterial(?Material $material)
    {
        $this->material = $material;
    }

    /**
     * @Route("/products/{id}", name="api_product_patch", methods={"PATCH"})
     */
    public function updateProductAction(Request $request, Product $product, MapperInterface $mapper): JsonResponse
    {
        $data = $this->getRequestData($request);

        $updatedProduct = $mapper->fillObject($product, $data);

        /* Do validation and other actions if need */

        $this->getDoctrine()->getManager()->flush();

        return $this->json($updatedProduct);
    }

@DTO(name="material")

@DTO(name="material_name", property="material")

new \OK\Dto\AnnotationMapper(new \Doctrine\Common\Annotations\AnnotationReader());

@DTO(name="customerNumber", type="string")

$data = ['customer_number' => 123];

@DTO(name="customerNumber|userNumber|managerNumber", type="string")

$data = ['customers' => [1,2,3]];

$data = ['customers' => [['firstName' => 'John', 'lastName' => 'Smith']]];

$data = ['customers' => [1, 2, ['firstName' => 'John', 'lastName' => 'Smith']];