Skip to content
Snippets Groups Projects
SelectFile.php 1.73 KiB
<?php

/*
 * This file is part of Anis Server.
 *
 * (c) Laboratoire d'Astrophysique de Marseille / CNRS
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
declare(strict_types=1);

namespace App\Search\Query;

use App\Entity\Dataset;

/**
 * Represents the Anis Select File attributes Query Part
 *
 * @author François Agneray <francois.agneray@lam.fr>
 * @package App\Search\Query
 */
class SelectFile extends AbstractQueryPart
{
    /**
     * Adds the select clause to the request and set only attribute with search_flag = FILE
     *
     * @param AnisQueryBuilder $anisQueryBuilder Represents the query being built
     * @param Dataset          $dataset      Represents the requested dataset
     * @param string[]         $queryParams  The query params of the url (after ?)
     */
    public function __invoke(AnisQueryBuilder $anisQueryBuilder, Dataset $dataset, array $queryParams): void
    {
        if ($queryParams['a'] === 'all') {
            $listOfIds = array_map(fn($attribute) => $attribute->getId(), $dataset->getAttributes());
        } else {
            $listOfIds = explode(';', $queryParams['a']);
        }

        $columns = array();
        $attributes = array();
        foreach ($listOfIds as $id) {
            $attribute = $this->getAttribute($dataset, (int) $id);
            if ($attribute->getSearchFlag() === 'FILE') {
                $columns[] = $dataset->getTableRef() . '.' . $attribute->getName() . ' as ' . $attribute->getLabel();
                $attributes[] = $attribute;
            }
        }
        $anisQueryBuilder->getDoctrineQueryBuilder()->select($columns);
        $anisQueryBuilder->setAttributesSelected($attributes);
    }
}