<commit_before><?php
/*
 * This file is part of the Tacit package.
 *
 * Copyright (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Tacit\Controller;


use Tacit\Controller\Exception\ServerErrorException;
use Tacit\Controller\Exception\UnacceptableEntityException;
use Tacit\Model\Exception\ModelValidationException;
use Tacit\Model\Query;

/**
 * An abstract representation of a RESTful collection controller.
 *
 * @package Tacit\Controller
 */
abstract class RestfulCollection extends Restful
{
    protected static $allowedMethods = ['OPTIONS', 'HEAD', 'GET', 'POST'];

    /**
     * GET a representation of a pageable and sortable collection.
     *
     * @throws Exception\ServerErrorException
     */
    public function get()
    {
        $limit = $this->app->request->get('limit', 25);
        $offset = $this->app->request->get('offset', 0);
        $orderBy = $this->app->request->get('sort', 'created_at');
        $orderDir = $this->app->request->get('sort_dir', 'desc');

        /** @var \Tacit\Model\Persistent $modelClass */
        $modelClass = static::$modelClass;

        try {
            $total = $modelClass::count();

            $collection = $modelClass::find(function ($query) use ($offset, $limit, $orderBy, $orderDir) {
                /** @var Query|object $query */
                $query->orderBy($orderBy, $orderDir)->skip($offset)->limit($limit);
            });
        } catch (\Exception $e) {
            throw new ServerErrorException($this, null, null, null, $e);
        }

        $this->respondWithCollection($collection, $modelClass::transformer(), ['total' => $total]);
    }

    /**
     * POST a new representation of an item into the collection.
     *
     * @throws Exception\ServerErrorException
     * @throws Exception\UnacceptableEntityException
     */
    public function post()
    {
        /** @var \Tacit\Model\Persistent $modelClass */
        $modelClass = static::$modelClass;

        try {
            $item = $modelClass::create($this->app->request->post(null, []));
        } catch (ModelValidationException $e) {
            throw new UnacceptableEntityException($this, 'Resource validation failed', $e->getMessage(), $e->getMessages(), $e);
        } catch (\Exception $e) {
            throw new ServerErrorException($this, null, null, null, $e);
        }

        $this->respondWithItemCreated($item, $modelClass::transformer());
    }
}
<commit_msg>Improve exception messages in RestfulCollection<commit_after><?php
/*
 * This file is part of the Tacit package.
 *
 * Copyright (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Tacit\Controller;


use Tacit\Controller\Exception\ServerErrorException;
use Tacit\Controller\Exception\UnacceptableEntityException;
use Tacit\Model\Exception\ModelValidationException;
use Tacit\Model\Query;

/**
 * An abstract representation of a RESTful collection controller.
 *
 * @package Tacit\Controller
 */
abstract class RestfulCollection extends Restful
{
    protected static $allowedMethods = ['OPTIONS', 'HEAD', 'GET', 'POST'];

    /**
     * GET a representation of a pageable and sortable collection.
     *
     * @throws Exception\ServerErrorException
     */
    public function get()
    {
        $limit = $this->app->request->get('limit', 25);
        $offset = $this->app->request->get('offset', 0);
        $orderBy = $this->app->request->get('sort', 'created_at');
        $orderDir = $this->app->request->get('sort_dir', 'desc');

        /** @var \Tacit\Model\Persistent $modelClass */
        $modelClass = static::$modelClass;

        try {
            $total = $modelClass::count();

            $collection = $modelClass::find(function ($query) use ($offset, $limit, $orderBy, $orderDir) {
                /** @var Query|object $query */
                $query->orderBy($orderBy, $orderDir)->skip($offset)->limit($limit);
            });
        } catch (\Exception $e) {
            throw new ServerErrorException($this, 'Error retrieving collection', $e->getMessage(), null, $e);
        }

        $this->respondWithCollection($collection, $modelClass::transformer(), ['total' => $total]);
    }

    /**
     * POST a new representation of an item into the collection.
     *
     * @throws Exception\ServerErrorException
     * @throws Exception\UnacceptableEntityException
     */
    public function post()
    {
        /** @var \Tacit\Model\Persistent $modelClass */
        $modelClass = static::$modelClass;

        try {
            $item = $modelClass::create($this->app->request->post(null, []));
        } catch (ModelValidationException $e) {
            throw new UnacceptableEntityException($this, 'Resource validation failed', $e->getMessage(), $e->getMessages(), $e);
        } catch (\Exception $e) {
            throw new ServerErrorException($this, 'Error creating item in collection', $e->getMessage(), null, $e);
        }

        $this->respondWithItemCreated($item, $modelClass::transformer());
    }
}

{'fraction_non_alphanumeric': 0.14565718677940045, 'fraction_numerical': 0.0011529592621060721, 'mean_word_length': 2.9342403628117912, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 57, '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': '813051', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1405, 'n_words': 358}