QueryHydrator.php 4.05 KB
Newer Older
Blaise de Carné 's avatar
Blaise de Carné committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<?php
/**
 * This file is part of the DATAtourisme project.
 *
 *  @author Conjecto <contact@conjecto.com>
 *
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
 */

namespace Datatourisme\Api\Resolver\SparqlResolver;

use Datatourisme\Api\Resolver\SparqlResolver\Utils\SparqlUtils;
use EasyRdf\Graph;
use EasyRdf\Literal;
use EasyRdf\Resource;
use Youshido\GraphQL\Parser\Ast\ArgumentValue\InputList;

class QueryHydrator
{
    /**
     * @param $entryPoint
     * @param array $arguments
     * @param $astFields
     * @param string $sparqlResult
     *
     * @return array
     *
28
     * @throws \EasyRdf_Exception
Blaise de Carné 's avatar
Blaise de Carné committed
29 30 31 32 33 34
     */
    public function generate(array $arguments, $astFields, $sparqlResult = '')
    {
        /** @var resource[] $listRes */
        $listRes = [];
        if ($sparqlResult) {
35
            $graph = new \EasyRdf_Graph();
Blaise de Carné 's avatar
Blaise de Carné committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
            $graph->parse($sparqlResult);

            //$type = SparqlUtils::expandUri($entryPoint);
            $listRes = $graph->allOfType('urn:resource');
        }

        $tabResult = [];
        foreach ($listRes as $res) {
            $tabResult[] = $this->consume($astFields, $res);
        }

        $sortList = (isset($arguments['sort'])) ? $arguments['sort'] : [];
        $tabResult = $this->sortResult($sortList, $tabResult);

        return $tabResult;
    }

    /**
     * @param array    $fields
     * @param resource $res
     *
     * @return mixed
     */
    private function consume(array $fields, $res)
    {
        $tabResult = [];
        foreach ($fields as $field) {
            $fieldName = $field->getName();

            if ('_uri' == $fieldName) {
                $tabResult['_uri'] = $res->getUri();
                continue;
            }
69
            $property = new \EasyRdf_Resource(SparqlUtils::expandUri($fieldName));
Blaise de Carné 's avatar
Blaise de Carné committed
70 71 72 73 74 75 76 77 78 79

            if ($field->hasFields()) {
                $tabResult[$fieldName] = [];
                $listRes = $res->all($property);
                foreach ($listRes as $resourceValue) {
                    $tabResult[$fieldName][] = $this->consume($field->getFields(), $resourceValue);
                }
            } else {
                if ($res && $res->hasProperty($property)) {
                    $tabResult[$fieldName] = [];
80
                    /** @var \EasyRdf_Resource $res */
Blaise de Carné 's avatar
Blaise de Carné committed
81 82
                    $propertyValues = $res->all($property);
                    foreach ($propertyValues as $value) {
83
                        if ($value instanceof \EasyRdf_Literal) {
Blaise de Carné 's avatar
Blaise de Carné committed
84
                            $tabResult[$fieldName][] = $value->getValue();
85
                        } elseif ($value instanceof  \EasyRdf_Resource) {
Blaise de Carné 's avatar
Blaise de Carné committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
                            if (!$value->isBNode()) {
                                $tabResult[$fieldName][] = $value->getUri();
                            }
                        }
                    }
                }
            }
        }

        return $tabResult;
    }

    /**
     * @param array $sortList
     * @param $tab
     *
     * @return mixed
     */
    private function sortResult($sortList, $tab)
    {
        if (null == $sortList) {
            return $tab;
        }
        /* @var InputList $pup */
        foreach ($sortList as $sort) {
            uasort($tab, function ($a, $b) use ($sort) {
                $key = key($sort);
                $order = reset($sort[$key]);

                if (!isset($a[$key])) {
                    return -1;
                }
                if (!isset($b[$key])) {
                    return 0;
                }
                if (is_array($a[$key])) {
                    $a = reset($a[$key]);
                    $b = reset($b[$key]);
                } else {
                    $a = $a[$key];
                    $b = $b[$key];
                }
                if ($a == $b) {
                    return 0;
                }
                if ('asc' == $order) {
                    return ($a < $b) ? -1 : 1;
                } else {
                    return ($a > $b) ? -1 : 1;
                }
            });
        }

        return $tab;
    }
}