<commit_before><?php

namespace rjapi\extension;

use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\DB;
use League\Fractal\Resource\Collection;
use rjapi\exceptions\HeadersException;
use rjapi\helpers\Json;
use rjapi\helpers\Request as RequestHelper;

class BaseController extends ApiController
{

    /**
     * BaseController constructor.
     * @param Route $route
     * @throws HeadersException
     */
    public function __construct(Route $route)
    {
        parent::__construct($route);
        if (in_array($route->getActionMethod(), self::AVAILABLE_BULKS, true) && RequestHelper::isExt(request(), self::EXT_BULK) === false) {
            throw new HeadersException('There is no ' . self::EXT_BULK . ' value in ' . self::EXT . ' key of ' . self::CONTENT_TYPE_KEY . ' header');
        }
    }

    /**
     * Creates bulk of items in transaction mode
     *
     * @param Request $request
     * @throws \LogicException
     * @throws \rjapi\exceptions\AttributesException
     */
    public function createBulk(Request $request)
    {
        $meta              = [];
        $json              = Json::decode($request->getContent());
        $jsonApiAttributes = Json::getBulkAttributes($json);

        try {
            $collection = new Collection();
            DB::beginTransaction();
            foreach ($jsonApiAttributes as $jsonObject) {
                // FSM initial state check
                if ($this->configOptions->isStateMachine() === true) {
                    $this->checkFsmCreate($jsonObject);
                }
                // spell check
                if ($this->configOptions->isSpellCheck() === true) {
                    $meta[] = $this->spellCheck($jsonObject);
                }
                // fill in model
                foreach ($this->props as $k => $v) {
                    // request fields should match Middleware fields
                    if (isset($jsonObject[$k])) {
                        $this->model->$k = $jsonObject[$k];
                    }
                }
                // set bit mask
                if (true === $this->configOptions->isBitMask()) {
                    $this->setMaskCreate($jsonObject);
                }

                $collection->setData($this->model);
                $this->model->save();
                // jwt
                if ($this->configOptions->getIsJwtAction() === true) {
                    $this->createJwtUser(); // !!! model is overridden
                }
                // set bit mask from model -> response
                if (true === $this->configOptions->isBitMask()) {
                    $this->model = $this->setFlagsCreate();
                }
            }
            DB::commit();
        } catch (\PDOException $e) {
            echo $e->getTraceAsString();
            DB::rollBack();
        }

        $this->setRelationships($json, $this->model->id);
        $resource = Json::getResource($this->middleWare, $collection, $this->entity, false, $meta);
        Json::outputSerializedData($resource, JSONApiInterface::HTTP_RESPONSE_CODE_CREATED);
    }

    /**
     * @param Request $request
     * @throws HeadersException
     * @throws \rjapi\exceptions\AttributesException
     */
    public function updateBulk(Request $request)
    {
<commit_msg>#develop. Add illuminat collection object to pass to output<commit_after><?php

namespace rjapi\extension;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\DB;
use rjapi\exceptions\HeadersException;
use rjapi\helpers\Json;
use rjapi\helpers\Request as RequestHelper;

class BaseController extends ApiController
{

    /**
     * BaseController constructor.
     * @param Route $route
     * @throws HeadersException
     */
    public function __construct(Route $route)
    {
        parent::__construct($route);
        if (in_array($route->getActionMethod(), self::AVAILABLE_BULKS, true) && RequestHelper::isExt(request(), self::EXT_BULK) === false) {
            throw new HeadersException('There is no ' . self::EXT_BULK . ' value in ' . self::EXT . ' key of ' . self::CONTENT_TYPE_KEY . ' header');
        }
    }

    /**
     * Creates bulk of items in transaction mode
     *
     * @param Request $request
     * @throws \LogicException
     * @throws \rjapi\exceptions\AttributesException
     */
    public function createBulk(Request $request)
    {
        $meta              = [];
        $json              = Json::decode($request->getContent());
        $jsonApiAttributes = Json::getBulkAttributes($json);
        $collection = new Collection();

        try {
            DB::beginTransaction();
            foreach ($jsonApiAttributes as $jsonObject) {
                // FSM initial state check
                if ($this->configOptions->isStateMachine() === true) {
                    $this->checkFsmCreate($jsonObject);
                }

                // spell check
                if ($this->configOptions->isSpellCheck() === true) {
                    $meta[] = $this->spellCheck($jsonObject);
                }

                // fill in model
                foreach ($this->props as $k => $v) {
                    // request fields should match Middleware fields
                    if (isset($jsonObject[$k])) {
                        $this->model->$k = $jsonObject[$k];
                    }
                }

                // set bit mask
                if (true === $this->configOptions->isBitMask()) {
                    $this->setMaskCreate($jsonObject);
                }

                $collection->push($this->model);
                $this->model->save();

                // jwt
                if ($this->configOptions->getIsJwtAction() === true) {
                    $this->createJwtUser(); // !!! model is overridden
                }

                // set bit mask from model -> response
                if (true === $this->configOptions->isBitMask()) {
                    $this->model = $this->setFlagsCreate();
                }
            }
            DB::commit();
        } catch (\PDOException $e) {
            echo $e->getTraceAsString();
            DB::rollBack();
        }

        $resource = Json::getResource($this->middleWare, $collection, $this->entity, true, $meta);
        Json::outputSerializedData($resource, JSONApiInterface::HTTP_RESPONSE_CODE_CREATED);
    }

    /**
     * @param Request $request
     * @throws HeadersException
     * @throws \rjapi\exceptions\AttributesException
     */
    public function updateBulk(Request $request)
    {

{'fraction_non_alphanumeric': 0.12939585211902616, 'fraction_numerical': 0.0, 'mean_word_length': 1.796218487394958, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 66, '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': '105052', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1733, 'n_words': 358}