PHP code example of dnaklik / dna-exchange-bundle

1. Go to this page and download the library: Download dnaklik/dna-exchange-bundle 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/ */

    

dnaklik / dna-exchange-bundle example snippets


// src/Controller/SomeController.php

use DnaKlik\DnaExchangeBundle\Service\DnaKlikExchange;
use Symfony\Component\HttpFoundation\Request;
// ...

class SomeController
{
    
    public function __construct(Request $request, DnaKlikExchange $dnaKlikExchange, DnaExchangeContentRepository $dnaExchangeContentRepository)
    {
        $this->dnaKlikExchange = $dnaKlikExchange;
        $this->dnaExchangeContentRepository = $dnaExchangeContentRepository;
    }
    
    public function detail()
    {
        $stamp = $this->dnaKlikExchange->getStamp($request);

        // ...
    }
}

## To find related content:
    public function relatedContent(Request $request): response
    {
        $items = $this->dnaKlikExchange->getRelatedContent($request, 30);
        foreach($items as $values) {
            $values["slug"];
            $content = $this->doctrine
                ->getRepository(Content::class)
                ->findOneBy(array("urlName" => $slugParts[2]));
            $values["matchCorr"];
            foreach($values["stamps"] as $stamp => $stampCount) {
                if (isset($values["matchStamps"][$stamp])) {
                    $dna .= "<span class='match'>".$stamp." (".$stampCount.")</span> ";
                }
                else {
                    $dna .= $stamp." (".$stampCount.") ";
                }
            }
            $values["totalStampCount"]);
        }
    }

## An array with slugs is returned. You can use the slugs to retrieve the related content from your own application

namespace App\Service;

use DnaKlik\DnaExchangeBundle\Service\DnaKlikStampProvider;

class CustomDnaKlikStampProvider extends DnaKlikStampProvider
{
    public function getContent(): array
    {
        $result = $this->dnaExchangeContentStampRepository->StampsInContent();
        foreach($result as $index => $content) {
            $slugParts = explode("/", $content["slug"]);
            $color = $this->manager
                ->getRepository(Color::class)
                ->findOneBy(array("urlName" => $slugParts[2]));
            $result[$index]["property"] = $color->getColor();
        }
        return $result;
    }

    function getContentFromId($id) {
        $result = $this->dnaExchangeContentRepository->findBy(array("id" => $id),array('id'=>'DESC'),3,0);
        foreach($result as $index => $content) {
            $slugParts = explode("/", $content->getSlug());
            $color = $this->manager
                ->getRepository(Color::class)
                ->findOneBy(array("urlName" => $slugParts[2]));
        }
        return $color;
    }

    public function findMatchItems($dna, $max)
    {
        $items = $this->matchDna->findMatchItems($dna, $max);
        foreach($items as $index => $item) {
            $slugParts = explode("/", $item["slug"]);
            $color = $this->manager
                ->getRepository(Color::class)
                ->findOneBy(array("urlName" => $slugParts[2]));
            $items[$index]["property"] = $color->getColor();
        }
        // dump($items);
        return $items;
    }
}