<commit_before><?php
/**
 * Created by PhpStorm.
 * User: Erwan
 * Date: 17/02/2015
 * Time: 14:30
 */

namespace Devyn\Component\ESIndexing;
use Devyn\Component\RAL\Manager\Manager;

/**
 * Class ESCache
 * @package Devyn\Component\ESIndexing
 */
class ESCache
{
    /**
     * @var array
     */
    protected $index;

    /**
     * @var array
     */
    protected $requests;

    /**
     * @var Manager
     */
    protected $rm;

    /**
     * @param string $type
     * @param array $index
     */
    function __construct(Manager $rm, $index)
    {
        $this->rm = $rm;
        $this->index = $index;
        $this->guessIndexingRequests();

        var_dump($this->getRequest('uri', 'person', 'fullName'));
    }

    public function getRequest($uri, $class, $property = '')
    {
        if (empty($property)) {
            if (isset($this->requests[$class]['guessResourceType'])) {
                return $this->requests[$class]['guessResourceType']->andWhere('?s ?p ' . $uri)->getSparqlQuery();
            }
            throw new \Exception('No matching found for ' . $class);
        }

        if (isset($this->requests[$class]['guessIndexingRequest'][$property])) {
            return $this->requests[$class]['guessIndexingRequest'][$property]->andWhere('?s ?p ' . $uri)->getSparqlQuery();
        }

        throw new \Exception('No matching found for ' . $class . ' and ' . $property);
    }

    protected function guessIndexingRequests()
    {
        $qb = $this->rm->getQueryBuilder();

        foreach ($this->index as $typeName => $settings) {
            $this->requests[$typeName]['class'] = $settings['class'];
            $this->requests[$typeName]['frame'] = $settings['frame'];
            $_qb = clone $qb;
            $_qb->reset();
            $_qb->construct('?s ?p ' . $settings['class'])->where('?s a ' . $settings['class']);
            $this->requests[$typeName]['guessResourceType'] = $_qb;
            foreach ($settings['properties'] as $key => $property) {
                $_qb = clone $qb;
                $_qb->reset();
                $_qb->construct('?s ?p ' . $key)->where('?s a ' . $key);
                $this->requests[$typeName]['guessIndexingRequest'][$key] = $_qb;
            }
        }

        var_dump($this->requests);
    }
}<commit_msg>error handling<commit_after><?php
/**
 * Created by PhpStorm.
 * User: Erwan
 * Date: 17/02/2015
 * Time: 14:30
 */

namespace Devyn\Component\ESIndexing;
use Devyn\Component\RAL\Manager\Manager;

/**
 * Class ESCache
 * @package Devyn\Component\ESIndexing
 */
class ESCache
{
    /**
     * @var array
     */
    protected $index;

    /**
     * @var array
     */
    protected $requests;

    /**
     * @var Manager
     */
    protected $rm;

    /**
     * @param string $type
     * @param array $index
     */
    function __construct(Manager $rm, $index)
    {
        $this->rm = $rm;
        $this->index = $index;
        $this->guessIndexingRequests();
    }

    public function getRequest($uri, $class, $property = '')
    {
        if (empty($property)) {
            if (isset($this->requests[$class]['guessResourceType'])) {
                return $this->requests[$class]['guessResourceType']->andWhere('?s ?p ' . $uri)->getSparqlQuery();
            }
            throw new \Exception('No matching found for ' . $class);
        }

        if (isset($this->requests[$class]['guessIndexingRequest'][$property])) {
            return $this->requests[$class]['guessIndexingRequest'][$property]->andWhere('?s ?p ' . $uri)->getSparqlQuery();
        }

        throw new \Exception('No matching found for ' . $class . ' and ' . $property);
    }

    /**
     * @return array
     */
    public function getRequests()
    {
        return $this->requests;
    }

    protected function guessIndexingRequests()
    {
        $qb = $this->rm->getQueryBuilder();

        foreach ($this->index as $typeName => $settings) {

            if (!isset($settings['class']) || empty($settings['class'])) {
                throw new \Exception('You have to specify a class for ' . $typeName);
            }
            if (!isset($settings['frame']) ||empty($settings['frame'])) {
                throw new \Exception('You have to specify a frame for ' . $typeName);
            }
            if (!isset($settings['properties']) || empty($settings['properties'])) {
                throw new \Exception('You have to specify properties for ' . $typeName);
            }

            $this->requests[$typeName]['class'] = $settings['class'];
            $this->requests[$typeName]['frame'] = $settings['frame'];
            $_qb = clone $qb;
            $_qb->reset();
            $_qb->construct('?s ?p ' . $settings['class'])->where('?s a ' . $settings['class']);
            $this->requests[$typeName]['guessResourceType'] = $_qb;

            foreach ($settings['properties'] as $key => $property) {
                $_qb = clone $qb;
                $_qb->reset();
                $_qb->construct('?s ?p ' . $key)->where('?s a ' . $key);
                $this->requests[$typeName]['guessIndexingRequest'][$key] = $_qb;
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.18023255813953487, 'fraction_numerical': 0.004651162790697674, 'mean_word_length': 2.137386018237082, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2755046', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1514, 'n_words': 296}