<commit_before><?php
/**
 * Spiral Framework.
 *
 * @license   MIT
 * @author    <NAME> (Wolfy-J)
 */
namespace Controllers;

use Database\Sample;
use Database\Sources\SampleSource;
use Requests\SampleRequest;
use Spiral\Core\Controller;
use Spiral\Http\Exceptions\ClientExceptions\NotFoundException;
use Spiral\Translator\Traits\TranslatorTrait;

class SampleController extends Controller
{
    use TranslatorTrait;

    /**
     * @see SeedCommand
     * @return string
     */
    public function indexAction(SampleSource $source)
    {
        /*
         * $source == Sample::source() (automatically resolved on orm schema update)
         */
        $selector = $source->findAll()->orderBy('sample.id', 'DESC')->paginate(25);

        return $this->views->render('sample/list', [
            'selector' => $selector
        ]);
    }

    /**
     * @param SampleSource $source This is alternative way to receive record source without using
     *                             static methods.
     * @return string
     */
    public function addAction(SampleSource $source)
    {
        return $this->views->render('sample/add', [
            'entity' => $source->create() //Identical to new Sample()
        ]);
    }

    /**
     * @param string       $id
     * @param SampleSource $source
     * @return string
     */
    public function editAction($id, SampleSource $source)
    {
        //Same as Sample::findByPK($id, ['child']);
        if (empty($entity = $source->findByPK($id))) {
            throw new NotFoundException('Unable to find Sample entity');
        }

        return $this->views->render('sample/edit', [
            'entity' => $entity,
            'source' => $source
        ]);
    }

    /**
     * @param SampleSource  $source
     * @param SampleRequest $request It's not really needed in most of your CRUD controllers.
     * @return array
     */
    public function createAction(SampleSource $source, SampleRequest $request)
    {
        /**
         * @var Sample $entity
         */
        $entity = $source->create();

        //Alternatively you can ask entity to validate itself and skip request wrapper
        //or use it without populate method
        if (!$request->isValid() || !$request->populate($entity)) {
            return [
                'status' => 400,
                'errors' => $request->getErrors()
            ];
        }

        /*
         * You can move entity validations into Sample model (almost same code) and use it this way:
         *
         * $entity->setFields($this->input->data); //Can automatically fill child->value property
         */

        //Alternatives: $entity->save()
        if (!$source->save($entity, $errors)) {
            return [
                'status' => 500,
                'errors' => $errors
            ];
        }

        return [
            'status'  => 201,
            'message' => $this->say("Sample entity has been successfully created"),
            'action'  => [
                //Wait for 2 seconds
                'delay'    => 2000,
                'redirect' => $this->router->uri('sample::edit', $entity)
            ]
        ];
    }
<commit_msg>Update SampleController.php<commit_after><?php
/**
 * Spiral Framework.
 *
 * @license   MIT
 * @author    <NAME> (Wolfy-J)
 */
namespace Controllers;

use Database\Sample;
use Database\Sources\SampleSource;
use Requests\SampleRequest;
use Spiral\Core\Controller;
use Spiral\Http\Exceptions\ClientExceptions\NotFoundException;
use Spiral\Translator\Traits\TranslatorTrait;

class SampleController extends Controller
{
    use TranslatorTrait;

    /**
     * @see SeedCommand
     * @return string
     */
    public function indexAction(SampleSource $source)
    {
        /*
         * $source == Sample::source() (automatically resolved on orm schema update)
         */
        $selector = $source->findAll()->orderBy('sample.id', 'DESC')->paginate(25);

        return $this->views->render('sample/list', [
            'selector' => $selector
        ]);
    }

    /**
     * @param SampleSource $source This is alternative way to receive record source without using
     *                             static methods.
     * @return string
     */
    public function addAction(SampleSource $source)
    {
        return $this->views->render('sample/add', [
            'entity' => $source->create() //Identical to new Sample()
        ]);
    }

    /**
     * @param string       $id
     * @param SampleSource $source
     * @return string
     */
    public function editAction($id, SampleSource $source)
    {
        //Same as Sample::findByPK($id, ['child']);
        if (empty($entity = $source->findByPK($id))) {
            throw new NotFoundException('Unable to find Sample entity');
        }

        return $this->views->render('sample/edit', [
            'entity' => $entity,
            'source' => $source
        ]);
    }

    /**
     * @param SampleSource  $source
     * @param SampleRequest $request It's not really needed in most of your CRUD controllers.
     * @return array
     */
    public function createAction(SampleSource $source, SampleRequest $request)
    {
        /**
         * @var Sample $entity
         */
        $entity = $source->create();

        //Alternatively you can ask entity to validate itself and skip request wrapper
        //or use it without populate method
        if (!$request->isValid() || !$request->populate($entity)) {
            return [
                'status' => 400,
                'errors' => $request->getErrors()
            ];
        }

        /*
         * You can move entity validations into Sample model (almost same code) and use it this way:
         *
         * $entity->setFields($this->input->data); //Can automatically fill child->value property
         */

        //Alternatives: $entity->save()
        if (!$source->save($entity, $errors)) {
            return [
                'status' => 500,
                'errors' => $errors
            ];
        }

        return [
            'status'  => 201,
            'message' => $this->say("Sample entity has been successfully created"),
            'action'  => [
                //Wait for 2 seconds (FIY, there is flash notifications in vault panel)
                'delay'    => 2000,
                'redirect' => $this->router->uri('sample::edit', $entity)
            ]
        ];
    }

{'fraction_non_alphanumeric': 0.13155030321878403, 'fraction_numerical': 0.004975897994091121, 'mean_word_length': 1.8907865168539326, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5440923', 'n_tokens_mistral': 1758, 'n_tokens_neox': 1690, 'n_words': 449}