<commit_before><?php

namespace PeskyCMF;

use Illuminate\Validation\ValidationServiceProvider;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Validator;

class PeskyValidationServiceProvider extends ValidationServiceProvider {

    /**
     * Bootstrap any application services.
     *
     * @return void
     * @throws \Symfony\Component\HttpFoundation\File\Exception\FileException
     * @throws \Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException
     * @throws \InvalidArgumentException
     */
    public function boot() {
        $this->addImgValidator();
        $this->addFileValidator();
        $this->addFileExtensionValidator();
        $this->getFileSizeValidator();
        $this->addAlternativeExistsValidator();
    }

    protected function registerPresenceVerifier() {
        $this->app->singleton('validation.presence', function ($app) {
            return new \PeskyCMF\Db\DatabasePresenceVerifier();
        });
    }

    /**
     * Image validation: 'img', 'img:jpeg,png'
     */
    protected function addImgValidator() {
        Validator::extend('img', function ($attribute, $value, $parameters) {
            $file = new UploadedFile($value['tmp_name'], $value['name'], $value['type'], $value['size'], $value['error']);
            $validators = 'required|image';
            if (!empty($parameters)) {
                $validators .= '|mimes:' . (is_array($parameters) ? implode(',', $parameters) : $parameters);
            }
            $isValid = Validator::make(
                [$attribute => $file],
                [$attribute => $validators]
            )->passes();
            return $isValid;
        });
        Validator::replacer('img', $this->getDefaultReplacer());
    }

    /**
     * File validation: 'filetype:jpeg,png'
     */
    protected function addFileValidator() {
        Validator::extend('filetype', function ($attribute, $value, $parameters) {
            if (count($parameters) < 1) {
                throw new \InvalidArgumentException('Validation rule file requires at least 1 parameter.');
            }
            $file = new UploadedFile($value['tmp_name'], $value['name'], $value['type'], $value['size'], $value['error']);
            $isValid = Validator::make(
                [$attribute => $file],
                [$attribute => 'required|mimes:' . (is_array($parameters) ? implode(',', $parameters) : $parameters)]
            )->passes();
            return $isValid;
        });
        Validator::replacer('filetype', $this->getDefaultReplacer());
    }

    /**
     * File extension validation: 'ext', 'ext:min', 'ext:min,max'
     */
    protected function addFileExtensionValidator() {
        Validator::extend('ext', function ($attribute, $value, $parameters) {
            if (isset($parameters[0]) && (!is_numeric($parameters[0]) || (int)$parameters < 1)) {
                throw new \InvalidArgumentException('Validation rule ext requires that 1st parameter (min) must be a positive number > 0.');
            }
            $min = isset($parameters[0]) && is_numeric($parameters[0]) ? (int)$parameters[0] : 1;
            $max = isset($parameters[1]) && is_numeric($parameters[1]) ? (int)$parameters[1] : '';
            return (
                is_array($value)
                && !empty($value['name'])
                && preg_match('%\.[a-zA-Z0-9]{' . $min . ',' . $max . '}$%i', $value['name'])
            );
        });
        Validator::replacer('ext', function ($message, $attribute, $rule, $parameters) {
            return str_replace(
                [':min', ':max'],
                [
                    isset($parameters[0]) && is_numeric($parameters[0]) ? (int)$parameters[0] : 1,
                    isset($parameters[1]) && is_numeric($parameters[1]) ? (int)$parameters[1] : ''
                ],
                $message
            );
        });
    }

    /**
     * File size validation: 'filesize:min' 'filesize:min,max', max = 0 means no limitation
     */
    protected function getFileSizeValidator() {
        Validator::extend('filesize', function ($attribute, $value, $parameters) {
            $this->requireParameterCount(1, $parameters, 'filesize');
            if (!is_numeric($parameters[0]) || (int)$parameters < 0) {
                throw new \InvalidArgumentException('Validation rule filesize requires that 1st parameter (min) must be a positive number or 0.');
            }
            if (!empty($parameters[1]) && (!is_numeric($parameters[1]) || (int)$parameters < 0)) {
                throw new \InvalidArgumentException('Validation rule filesize requires that 2nd parameter (max) must be a positive number or 0.');
            }
            $min = (int)$parameters[0];
            $max = empty($parameters[1]) ? 0 : (int)$parameters[1];
            if ($max > 0 && $min > $max) {
                throw new \InvalidArgumentException('Validation rule filesize requires that 2nd parameter (max) must be greater then 1st parameter (min) or 0.');
            }
            if (!is_array($value) || empty($value['size'])) {
                return false;
            }
            return (
                $value['size'] / 1024 >= $min
                && ($max === 0 || $value['size'] / 1024 <= $max)
            );
        });
        Validator::replacer('filesize', function ($message, $attribute, $rule, $parameters) {
            $min = (int)$parameters[0];<commit_msg>PeskyValidationServiceProvider - updated validator to also accept UploadedFile objects<commit_after><?php

namespace PeskyCMF;

use Illuminate\Validation\ValidationServiceProvider;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Validator;

class PeskyValidationServiceProvider extends ValidationServiceProvider {

    /**
     * Bootstrap any application services.
     *
     * @return void
     * @throws \Symfony\Component\HttpFoundation\File\Exception\FileException
     * @throws \Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException
     * @throws \InvalidArgumentException
     */
    public function boot() {
        $this->addImgValidator();
        $this->addFileValidator();
        $this->addFileExtensionValidator();
        $this->getFileSizeValidator();
        $this->addAlternativeExistsValidator();
    }

    protected function registerPresenceVerifier() {
        $this->app->singleton('validation.presence', function ($app) {
            return new \PeskyCMF\Db\DatabasePresenceVerifier();
        });
    }

    /**
     * Image validation: 'img', 'img:jpeg,png'
     */
    protected function addImgValidator() {
        Validator::extend('img', function ($attribute, $value, $parameters) {
            if (!($value instanceof UploadedFile)) {
                $value = new UploadedFile($value['tmp_name'], $value['name'], $value['type'], $value['size'], $value['error']);
            }
            $validators = 'required|image';
            if (!empty($parameters)) {
                $validators .= '|mimes:' . (is_array($parameters) ? implode(',', $parameters) : $parameters);
            }
            $isValid = Validator::make(
                [$attribute => $value],
                [$attribute => $validators]
            )->passes();
            return $isValid;
        });
        Validator::replacer('img', $this->getDefaultReplacer());
    }

    /**
     * File validation: 'filetype:jpeg,png'
     */
    protected function addFileValidator() {
        Validator::extend('filetype', function ($attribute, $value, $parameters) {
            if (count($parameters) < 1) {
                throw new \InvalidArgumentException('Validation rule file requires at least 1 parameter.');
            }
            if (!($value instanceof UploadedFile)) {
                $value = new UploadedFile($value['tmp_name'], $value['name'], $value['type'], $value['size'], $value['error']);
            }
            $isValid = Validator::make(
                [$attribute => $value],
                [$attribute => 'required|mimes:' . (is_array($parameters) ? implode(',', $parameters) : $parameters)]
            )->passes();
            return $isValid;
        });
        Validator::replacer('filetype', $this->getDefaultReplacer());
    }

    /**
     * File extension validation: 'ext', 'ext:min', 'ext:min,max'
     */
    protected function addFileExtensionValidator() {
        Validator::extend('ext', function ($attribute, $value, $parameters) {
            if (isset($parameters[0]) && (!is_numeric($parameters[0]) || (int)$parameters < 1)) {
                throw new \InvalidArgumentException('Validation rule ext requires that 1st parameter (min) must be a positive number > 0.');
            }
            $min = isset($parameters[0]) && is_numeric($parameters[0]) ? (int)$parameters[0] : 1;
            $max = isset($parameters[1]) && is_numeric($parameters[1]) ? (int)$parameters[1] : '';
            return (
                is_array($value)
                && !empty($value['name'])
                && preg_match('%\.[a-zA-Z0-9]{' . $min . ',' . $max . '}$%i', $value['name'])
            );
        });
        Validator::replacer('ext', function ($message, $attribute, $rule, $parameters) {
            return str_replace(
                [':min', ':max'],
                [
                    isset($parameters[0]) && is_numeric($parameters[0]) ? (int)$parameters[0] : 1,
                    isset($parameters[1]) && is_numeric($parameters[1]) ? (int)$parameters[1] : ''
                ],
                $message
            );
        });
    }

    /**
     * File size validation: 'filesize:min' 'filesize:min,max', max = 0 means no limitation
     */
    protected function getFileSizeValidator() {
        Validator::extend('filesize', function ($attribute, $value, $parameters) {
            $this->requireParameterCount(1, $parameters, 'filesize');
            if (!is_numeric($parameters[0]) || (int)$parameters < 0) {
                throw new \InvalidArgumentException('Validation rule filesize requires that 1st parameter (min) must be a positive number or 0.');
            }
            if (!empty($parameters[1]) && (!is_numeric($parameters[1]) || (int)$parameters < 0)) {
                throw new \InvalidArgumentException('Validation rule filesize requires that 2nd parameter (max) must be a positive number or 0.');
            }
            $min = (int)$parameters[0];
            $max = empty($parameters[1]) ? 0 : (int)$parameters[1];
            if ($max > 0 && $min > $max) {
                throw new \InvalidArgumentException('Validation rule filesize requires that 2nd parameter (max) must be greater then 1st parameter (min) or 0.');
            }
            if (!is_array($value)) {
                return false;
            }
            if (!($value instanceof UploadedFile)) {
                $value = new UploadedFile($value['tmp_name'], $value['name'], $value['type'], $value['size'], $value['error']);
            }
            if (!$value->getSize()) {
                return false;
            }
            return (
                $value->getSize() / 1024 >= $min
                && ($max === 0 || $value->getSize() / 1024 <= $max)
            );
        });
        Validator::replacer('filesize', function ($message, $attribute, $rule, $parameters) {
            $min = (int)$parameters[0];
{'fraction_non_alphanumeric': 0.15777405414939588, 'fraction_numerical': 0.009171884645912337, 'mean_word_length': 2.326488706365503, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 46, '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': '5518887', 'n_tokens_mistral': 3088, 'n_tokens_neox': 2944, 'n_words': 656}