<commit_before><?php

namespace UniMapper\Mapper;

use UniMapper\Query\Object\Order,
    UniMapper\Reflection\EntityReflection,
    UniMapper\Connection\FlexibeeConnection,
    UniMapper\Exceptions\MapperException;

/**
 * Flexibee mapper can be generally used to communicate between repository and
 * Flexibee REST API.
 */
class FlexibeeMapper extends \UniMapper\Mapper
{

    /** @var \DibiConnection $connection Dibi connection */
    protected $connection;

    public function __construct($name, FlexibeeConnection $connection)
    {
        parent::__construct($name);
        $this->connection = $connection;
    }

    /**
     * Delete
     *
     * @param \UniMapper\Query\Delete $query Query
     *
     * @return mixed
     */
    public function delete(\UniMapper\Query\Delete $query)
    {
        $resource = $this->getResource($query->entityReflection);

        $xml = new \SimpleXMLElement('<winstrom version="1.0" />');
        $xmlResource = $xml->addChild($resource);
        $xmlResource->addAttribute("filter", $this->getConditions($query));
        $xmlResource->addAttribute("action", "delete");

        $result = $this->connection->put(
            rawurlencode($resource) . ".xml",
            $xml->asXML(),
            "application/xml"
        );

        $this->getStatus($result);

        return true;
    }

    /**
     * Use 'code:' identifier as primary identifier of entities
     *
     * @param mixed  $data         JSON from Flexibee
     * @param string $resourceName Resource name in Flexibee
     *
     * @return mixed
     *
     * @throws \UniMapper\Exceptions\MapperException
     */
    protected function setCodeId($data, $resourceName)
    {
        if (!isset($data->{$resourceName})) {
            throw new MapperException("Unknown response, 'code:' prefix missing?!");
        }

        foreach ($data->{$resourceName} as $iterator => $row) {
            if (isset($row->{"external-ids"}[0])
                && substr($row->{"external-ids"}[0],0,5) === "code:"
            ) {
                $data->{$resourceName}[$iterator]->id =
                    $row->{"external-ids"}[0];
            }
        }
        return $data;
    }

    /**
     * Find single record
     *
     * @param \UniMapper\Query\FindOne $query Query
     *
     * @return \UniMapper\Entity|false
     *
     * @throws \UniMapper\Exceptions\MapperException
     */
    public function findOne(\UniMapper\Query\FindOne $query)
    {
        $resource = $this->getResource($query->entityReflection);

        // Create URL
        $url = rawurlencode($resource)
            . "/" . rawurlencode($query->primaryValue)
            . ".json";

        // Add custom fields from entity property definitions
        $parameters = array();

        // Add custom fields from entity properties definitions
        $parameters[] = "detail=custom:" . rawurlencode(implode(",", $this->getSelection($query->entityReflection)));

        // Try to get IDs as 'code:...'
        $parameters[] = "code-as-id=true";

        // Get data
        $data = $this->connection->get($url . "?" . implode("&", $parameters));

        // Check if request failed
        if (isset($data->success)
            && $data->success === "false") {
            throw new MapperException($data->message);
        }

        if (!isset($data->{$resource}[0])) {
            return false;
        }

        $entityClass = $query->entityReflection->getName();

        return $this->createEntity(
            $entityClass,
            $this->setCodeId($data, $resource)->{$resource}[0]
        );
    }

    /**
     * FindAll
     *
     * @param \UniMapper\Query\FindAll $query Query
     *
     * @return \UniMapper\EntityCollection|false
     *
     * @throws \UniMapper\Exceptions\MapperException
     */
    public function findAll(\UniMapper\Query\FindAll $query)
    {
        $resource = $this->getResource($query->entityReflection);

        // Get URL
        $url = rawurlencode($resource);

        // Apply conditions
        if (count($query->conditions > 0)) {
            $url .= "/" . rawurlencode("(" . $this->getConditions($query) . ")");
        }

        // Set response type
        $url .= ".json";

        // Define additional parameters
        $parameters = array();

        // Add order
        if (count($query->orders) > 0) {
            $parameters = $this->convertOrder($query->orders, $query);
        }

        if ($query->offset) {
            $parameters[] = "start=" . $query->offset;
        }
        if ($query->limit) {
            $parameters[] = "limit=" . $query->limit;
        }

        // Add custom fields from entity properties definitions
        $parameters[] = "detail=custom:" . rawurlencode(implode(",", $this->getSelection($query->entityReflection)));

        // Try to get IDs as 'code:...'
        $parameters[] = "code-as-id=true";

        // Request data
        $data = $this->connection->get($url . "?" . implode("&", $parameters));

        // Check if request failed
        if (isset($data->success)
            && $data->success === "false") {
            throw new MapperException($data->message);
        }

        if (count($data->{$resource}) === 0) {
            return false;
        }

        // Set ID and return data
        return $this->createCollection(
            $query->entityReflection->getName(),<commit_msg>FindAll: apply limit & offset automatically<commit_after><?php

namespace UniMapper\Mapper;

use UniMapper\Query\Object\Order,
    UniMapper\Reflection\EntityReflection,
    UniMapper\Connection\FlexibeeConnection,
    UniMapper\Exceptions\MapperException;

/**
 * Flexibee mapper can be generally used to communicate between repository and
 * Flexibee REST API.
 */
class FlexibeeMapper extends \UniMapper\Mapper
{

    /** @var \DibiConnection $connection Dibi connection */
    protected $connection;

    public function __construct($name, FlexibeeConnection $connection)
    {
        parent::__construct($name);
        $this->connection = $connection;
    }

    /**
     * Delete
     *
     * @param \UniMapper\Query\Delete $query Query
     *
     * @return mixed
     */
    public function delete(\UniMapper\Query\Delete $query)
    {
        $resource = $this->getResource($query->entityReflection);

        $xml = new \SimpleXMLElement('<winstrom version="1.0" />');
        $xmlResource = $xml->addChild($resource);
        $xmlResource->addAttribute("filter", $this->getConditions($query));
        $xmlResource->addAttribute("action", "delete");

        $result = $this->connection->put(
            rawurlencode($resource) . ".xml",
            $xml->asXML(),
            "application/xml"
        );

        $this->getStatus($result);

        return true;
    }

    /**
     * Use 'code:' identifier as primary identifier of entities
     *
     * @param mixed  $data         JSON from Flexibee
     * @param string $resourceName Resource name in Flexibee
     *
     * @return mixed
     *
     * @throws \UniMapper\Exceptions\MapperException
     */
    protected function setCodeId($data, $resourceName)
    {
        if (!isset($data->{$resourceName})) {
            throw new MapperException("Unknown response, 'code:' prefix missing?!");
        }

        foreach ($data->{$resourceName} as $iterator => $row) {
            if (isset($row->{"external-ids"}[0])
                && substr($row->{"external-ids"}[0],0,5) === "code:"
            ) {
                $data->{$resourceName}[$iterator]->id =
                    $row->{"external-ids"}[0];
            }
        }
        return $data;
    }

    /**
     * Find single record
     *
     * @param \UniMapper\Query\FindOne $query Query
     *
     * @return \UniMapper\Entity|false
     *
     * @throws \UniMapper\Exceptions\MapperException
     */
    public function findOne(\UniMapper\Query\FindOne $query)
    {
        $resource = $this->getResource($query->entityReflection);

        // Create URL
        $url = rawurlencode($resource)
            . "/" . rawurlencode($query->primaryValue)
            . ".json";

        // Add custom fields from entity property definitions
        $parameters = array();

        // Add custom fields from entity properties definitions
        $parameters[] = "detail=custom:" . rawurlencode(implode(",", $this->getSelection($query->entityReflection)));

        // Try to get IDs as 'code:...'
        $parameters[] = "code-as-id=true";

        // Get data
        $data = $this->connection->get($url . "?" . implode("&", $parameters));

        // Check if request failed
        if (isset($data->success)
            && $data->success === "false") {
            throw new MapperException($data->message);
        }

        if (!isset($data->{$resource}[0])) {
            return false;
        }

        $entityClass = $query->entityReflection->getName();

        return $this->createEntity(
            $entityClass,
            $this->setCodeId($data, $resource)->{$resource}[0]
        );
    }

    /**
     * FindAll
     *
     * @param \UniMapper\Query\FindAll $query Query
     *
     * @return \UniMapper\EntityCollection|false
     *
     * @throws \UniMapper\Exceptions\MapperException
     */
    public function findAll(\UniMapper\Query\FindAll $query)
    {
        $resource = $this->getResource($query->entityReflection);

        // Get URL
        $url = rawurlencode($resource);

        // Apply conditions
        if (count($query->conditions > 0)) {
            $url .= "/" . rawurlencode("(" . $this->getConditions($query) . ")");
        }

        // Set response type
        $url .= ".json";

        // Define additional parameters
        $parameters = array();

        // Add order
        if (count($query->orders) > 0) {
            $parameters = $this->convertOrder($query->orders, $query);
        }

        $parameters[] = "start=" . (int) $query->offset;
        $parameters[] = "limit=" . (int) $query->limit;

        // Add custom fields from entity properties definitions
        $parameters[] = "detail=custom:" . rawurlencode(implode(",", $this->getSelection($query->entityReflection)));

        // Try to get IDs as 'code:...'
        $parameters[] = "code-as-id=true";

        // Request data
        $data = $this->connection->get($url . "?" . implode("&", $parameters));

        // Check if request failed
        if (isset($data->success)
            && $data->success === "false") {
            throw new MapperException($data->message);
        }

        if (count($data->{$resource}) === 0) {
            return false;
        }

        // Set ID and return data
        return $this->createCollection(
            $query->entityReflection->getName(),
{'fraction_non_alphanumeric': 0.15315902112217364, 'fraction_numerical': 0.002233181352935703, 'mean_word_length': 2.077892325315006, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 121, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, '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': '5966287', 'n_tokens_mistral': 3018, 'n_tokens_neox': 2917, 'n_words': 615}