<commit_before><?php
/**
 * //TODO PHPDoc
 * @author <NAME>
 */

namespace DataDo;


use DataDo\Data\Repository;
use Psr\Http\Message\StreamInterface;
use ReflectionClass;
use ReflectionException;
use Slim\App;
use Slim\Http\Request;
use Slim\Http\Response;

class Slim
{

    private $showCheckPage = false;

    /**
     * Create a new slim app that hosts the provided repositories
     * @return App
     */
    public static function create()
    {
        $app = new App();
        $slimBS = new Slim();

        // Find options
        foreach (func_get_args() as $option) {
            if (is_int($option)) {
                $slimBS->flag($option);
            }
        }

        foreach (func_get_args() as $repo) {
            if ($repo instanceof Repository) {
                $slimBS->bootStrap($app, $repo);
            }
        }

        return $app;

    }

    public static function attach(App $slim)
    {
        $slimBS = new Slim();

        // Find options
        foreach (func_get_args() as $option) {
            if (is_int($option)) {
                $slimBS->flag($option);
            }
        }

        foreach (func_get_args() as $arg) {
            if (!($arg instanceof App)) {
                $slimBS->bootStrap($slim, $arg);
            }
        }
    }

    public function flag($option)
    {
        switch ($option) {
            case Slim::CHECK_PAGE:
                $this->showCheckPage = true;
                break;
        }
    }

    /**
     * Add a repository to your Slim App
     * @param App $app
     * @param Repository $repository
     * @return App the given app for chaining
     */
    public function bootStrap(App $app, Repository $repository)
    {
        $baseUrl = '/' . self::parseRepoName($repository->getEntityClass()->getShortName());

        /**
         * Get the whole collection.
         */
        $app->get($baseUrl, function (Request $request, Response $response) use ($repository) {
            return $response
                ->write(self::output($repository->findAll()))
                ->withHeader('Content-Type', 'application/json');
        });

        /**
         * Delete the whole collection.
         */
        $app->delete($baseUrl, function (Request $request, Response $response) use ($repository) {
            $repository->deleteAll();
        });

        /**
         * Add a new entity to the collection.
         */
        $app->post($baseUrl, function (Request $request, Response $response) use ($repository) {

            $body = self::getBody($request->getBody(), $repository->getEntityClass(), $response);

            if ($body instanceof Response) {
                return $body;
            } else {
                // Store the entity
                $repository->insert($body);

                return $response
                    ->withStatus(Status::CREATED)
                    ->withHeader('Content-Type', 'application/json')
                    ->write(self::output($body));
            }
        });


        if ($this->showCheckPage) {
            /**
             * Display the repository check page.
             */
            $app->get($baseUrl . '/check', function (Request $request, Response $response) use ($repository) {
                $repository->checkDatabase();
            });
        }


        $entityUrl = $baseUrl . '/{id}';

        /**
         * Get a single entity.
         */
        $app->get($entityUrl, function (Request $request, Response $response, $args) use ($repository) {
            $entity = $repository->get($args['id']);

            if ($entity) {
                return $response
                    ->write(self::output($entity))
                    ->withHeader('Content-Type', 'application/json');
            }

            return $response->withStatus(Status::NOT_FOUND);
        });

        /**
         * Delete a single entity
         */
        $app->delete($entityUrl, function (Request $request, Response $response, $args) use ($repository) {
            $repository->delete($args['id']);
        });

        /**
         * Replace a single entity
         */
        $app->put($entityUrl, function (Request $request, Response $response, $args) use ($repository) {

            $body = self::getBody($request->getBody(), $repository->getEntityClass(), $response);

            if ($body instanceof Response) {
                return $body;
            } else {
                // Store the entity
                $repository->getIdProperty()->setValue($body, $args['id']);
                $repository->update($body);

                return $response
                    ->withHeader('Content-Type', 'application/json')
                    ->write(self::output($body));
            }
        });

        return $app;
    }

    private static function output($input)
    {
        return json_encode($input, JSON_PRETTY_PRINT);
    }

    private static function parseRepoName($className)
    {
        $repoName = lcfirst($className);

        if (substr($repoName, strlen($repoName) - 1) !== 's') {
            $repoName .= 's';
        }

        return $repoName;
    }

    private static function getBody(StreamInterface $input, ReflectionClass $class, Response $response)
    {
        $body = self::parseBody($input);
        $newInstance = $class->newInstanceWithoutConstructor();

        foreach ($body as $key => $value) {
            try {
                $property = $class->getProperty($key);
                $property->setAccessible(true);
                $property->setValue($newInstance, $value);
            } catch (ReflectionException $e) {
                return $response
                    ->withStatus(Status::BAD_REQUEST)
                    ->withHeader('Content-Type', 'application/json')
                    ->write(self::output(['error' => $e->getMessage()]));
            }
        }

        if ($class->getConstructor() !== null) {
            $class->getConstructor()->setAccessible(true);
            $class->getConstructor()->invoke($newInstance);
        }

        return $newInstance;
    }

    private static function parseBody(StreamInterface $body)
    {
        $result = json_decode($body->getContents());

        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new \InvalidArgumentException('Invalid json');
        }

        return $result;
    }

    const CHECK_PAGE = 0;
}<commit_msg>Bug fix on the check pages.<commit_after><?php
/**
 * //TODO PHPDoc
 * @author <NAME>
 */

namespace DataDo;


use DataDo\Data\Repository;
use Psr\Http\Message\StreamInterface;
use ReflectionClass;
use ReflectionException;
use Slim\App;
use Slim\Http\Request;
use Slim\Http\Response;

class Slim
{

    private $showCheckPage = false;

    /**
     * Create a new slim app that hosts the provided repositories
     * @return App
     */
    public static function create()
    {
        $app = new App();
        $slimBS = new Slim();

        // Find options
        foreach (func_get_args() as $option) {
            if (is_int($option)) {
                $slimBS->flag($option);
            }
        }

        foreach (func_get_args() as $repo) {
            if ($repo instanceof Repository) {
                $slimBS->bootStrap($app, $repo);
            }
        }

        return $app;

    }

    public static function attach(App $slim)
    {
        $slimBS = new Slim();

        // Find options
        foreach (func_get_args() as $option) {
            if (is_int($option)) {
                $slimBS->flag($option);
            }
        }

        foreach (func_get_args() as $arg) {
            if ($arg instanceof Repository) {
                $slimBS->bootStrap($slim, $arg);
            }
        }
    }

    public function flag($option)
    {
        switch ($option) {
            case Slim::CHECK_PAGE:
                $this->showCheckPage = true;
                break;
        }
    }

    /**
     * Add a repository to your Slim App
     * @param App $app
     * @param Repository $repository
     * @return App the given app for chaining
     */
    public function bootStrap(App $app, Repository $repository)
    {
        $baseUrl = '/' . self::parseRepoName($repository->getEntityClass()->getShortName());

        /**
         * Get the whole collection.
         */
        $app->get($baseUrl, function (Request $request, Response $response) use ($repository) {
            return $response
                ->write(self::output($repository->findAll()))
                ->withHeader('Content-Type', 'application/json');
        });

        /**
         * Delete the whole collection.
         */
        $app->delete($baseUrl, function (Request $request, Response $response) use ($repository) {
            $repository->deleteAll();
        });

        /**
         * Add a new entity to the collection.
         */
        $app->post($baseUrl, function (Request $request, Response $response) use ($repository) {

            $body = self::getBody($request->getBody(), $repository->getEntityClass(), $response);

            if ($body instanceof Response) {
                return $body;
            } else {
                // Store the entity
                $repository->insert($body);

                return $response
                    ->withStatus(Status::CREATED)
                    ->withHeader('Content-Type', 'application/json')
                    ->write(self::output($body));
            }
        });


        if ($this->showCheckPage) {
            /**
             * Display the repository check page.
             */
            $app->get($baseUrl . '/check', function (Request $request, Response $response) use ($repository) {
                $repository->checkDatabase();
            });
        }


        $entityUrl = $baseUrl . '/{id}';

        /**
         * Get a single entity.
         */
        $app->get($entityUrl, function (Request $request, Response $response, $args) use ($repository) {
            $entity = $repository->get($args['id']);

            if ($entity) {
                return $response
                    ->write(self::output($entity))
                    ->withHeader('Content-Type', 'application/json');
            }

            return $response->withStatus(Status::NOT_FOUND);
        });

        /**
         * Delete a single entity
         */
        $app->delete($entityUrl, function (Request $request, Response $response, $args) use ($repository) {
            $repository->delete($args['id']);
        });

        /**
         * Replace a single entity
         */
        $app->put($entityUrl, function (Request $request, Response $response, $args) use ($repository) {

            $body = self::getBody($request->getBody(), $repository->getEntityClass(), $response);

            if ($body instanceof Response) {
                return $body;
            } else {
                // Store the entity
                $repository->getIdProperty()->setValue($body, $args['id']);
                $repository->update($body);

                return $response
                    ->withHeader('Content-Type', 'application/json')
                    ->write(self::output($body));
            }
        });

        return $app;
    }

    private static function output($input)
    {
        return json_encode($input, JSON_PRETTY_PRINT);
    }

    private static function parseRepoName($className)
    {
        $repoName = lcfirst($className);

        if (substr($repoName, strlen($repoName) - 1) !== 's') {
            $repoName .= 's';
        }

        return $repoName;
    }

    private static function getBody(StreamInterface $input, ReflectionClass $class, Response $response)
    {
        $body = self::parseBody($input);
        $newInstance = $class->newInstanceWithoutConstructor();

        foreach ($body as $key => $value) {
            try {
                $property = $class->getProperty($key);
                $property->setAccessible(true);
                $property->setValue($newInstance, $value);
            } catch (ReflectionException $e) {
                return $response
                    ->withStatus(Status::BAD_REQUEST)
                    ->withHeader('Content-Type', 'application/json')
                    ->write(self::output(['error' => $e->getMessage()]));
            }
        }

        if ($class->getConstructor() !== null) {
            $class->getConstructor()->setAccessible(true);
            $class->getConstructor()->invoke($newInstance);
        }

        return $newInstance;
    }

    private static function parseBody(StreamInterface $body)
    {
        $result = json_decode($body->getContents());

        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new \InvalidArgumentException('Invalid json');
        }

        return $result;
    }

    const CHECK_PAGE = 0;
}
{'fraction_non_alphanumeric': 0.13724576929048285, 'fraction_numerical': 0.0003105107902499612, 'mean_word_length': 1.6991410014665829, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 113, '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': '5974880', 'n_tokens_mistral': 3443, 'n_tokens_neox': 3292, 'n_words': 759}