PHP code example of xsolve-pl / associate

1. Go to this page and download the library: Download xsolve-pl/associate 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/ */

    

xsolve-pl / associate example snippets



$facade = new \Xsolve\Associate\Facade();
$basicCollector = $facade->getBasicCollector();


$facade = new \Xsolve\Associate\Facade($entityManager);
$doctrineOrmCollector = $facade->getDoctrineOrmCollector();



class Car
{
    /**
     * @var Engine|null
     */
    protected $engine;

    /**
     * @param Engine $engine
     */
    public function __construct(Engine $engine = null)
    {
        $this->engine = $engine;
    }

    /**
     * @return Engine|null
     */
    public function getEngine()
    {
        return $this->engine;
    }
}

class Engine
{
    /**
     * @var Part[]
     */
    public $parts;

    /**
     * @param Part[] $parts
     */
    public function __construct(array $parts)
    {
        $this->parts = $parts;
    }
}

class Part
{
    /**
     * @var string
     */
    protected $name;

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

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

    /**
     * @return string[]
     */
    public function getNameAsWords(): array
    {
        return explode(' ', $this->name);
    }

    /**
     * @return array
     */
    public function getNameStats(): array
    {
        return ['wordCount' => count($this->getNameAsWords())];
    }
}


$cars = [
    $sportCar = new Car(
        $fastEngine = new Engine([
            $valve = new Part('valve'),
            $cylinder= new Part('cylinder'),
        ])
    ),
    $sedan = new Car(
        $turboEngine = new Engine([
            $valve,
            $sparkPlug = new Part('nano spark plug'),
            $smartCylinder = new Part('smart cylinder'),
        ])
    ),
    $suv = new Car(),
];


$engines = $basicCollector->collect($cars, ['engine']);
// $engines ~= [$fastEngine, $turboEngine]; - order is not guaranteed.


$parts = $basicCollector->collect($cars, ['engine', 'parts']);
// $parts ~= [$valve, $cylinder, $sparkPlug, $smartCylinder]; - order is not guaranteed.


$names = $basicCollector->collect($cars, ['engine', 'parts', 'name']);
// $names ~= ['valve', 'cylinder', 'spark plug', 'smart cylinder']; - order is not guaranteed.


$words = $basicCollector->collect($cars, ['engine', 'parts', 'nameAsWords']);
// $words ~= [
//     'valve', 'cylinder', 'nano', 'spark', 'plug', 'smart', 'cylinder'
// ]; - order is not guaranteed.


$wordCounts = $basicCollector->collect($cars, ['engine', 'parts', 'nameStats', '[wordCount]']);
// $wordCounts ~= [1, 1, 3, 2]; - order is not guaranteed.


$availableProducts = array_filter(
    $products,
    function(Product $product) {
        foreach ($product->getVariants() as $variant) {
            if ($variant->getInventoryQuantity() > 0) {
                return true;
            }
        }

        return false;
    }
);


$facade = new \Xsolve\Associate\Facade($entityManager);
$doctrineOrmCollector = $facade->getDoctrineOrmCollector();
$doctrineOrmCollector->collect($products, ['variants']);


$facade = new \Xsolve\Associate\Facade($entityManager);
$doctrineOrmCollector = $facade->getDoctrineOrmCollector();
$prices = $doctrineOrmCollector->collect($products, ['variants', 'price']);


$resolve = function(Product $product) {
    return $product->getVariants();
};


$facade = new \Xsolve\Associate\Facade($entityManager);
$bufferedCollector = $facade->getBufferedDoctrineOrmCollector();

$resolve = function(Product $product) use ($bufferedCollector) {
    $bufferedCollectClosure = $bufferedCollector->createCollectClosure([$product], ['variants']);

    return new \GraphQL\Deferred(function() use ($bufferedCollectClosure) {
        return $bufferedCollectClosure();
    });
};