<commit_before><?php

/*
 * This file is part of jwt-auth
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Tymon\JWTAuth;

use Illuminate\Http\Request;
use Tymon\JWTAuth\Support\Utils;
use Tymon\JWTAuth\Claims\Factory as ClaimFactory;
use Illuminate\Support\Collection;
use Tymon\JWTAuth\Support\RefreshFlow;
use Tymon\JWTAuth\Support\CustomClaims;
use Tymon\JWTAuth\Validators\PayloadValidator;

class Factory
{
    use RefreshFlow, CustomClaims;

    /**
     * @var \Tymon\JWTAuth\Claims\Factory
     */
    protected $claimFactory;

    /**
     * @var \Illuminate\Http\Request
     */
    protected $request;

    /**
     * @var \Tymon\JWTAuth\Validators\PayloadValidator
     */
    protected $validator;

    /**
     * @var int
     */
    protected $ttl = 60;

    /**
     * @var array
     */
    protected $defaultClaims = ['iss', 'iat', 'exp', 'nbf', 'jti'];

    /**
     * @var \Illuminate\Support\Collection
     */
    protected $claims;

    /**
     * @param \Tymon\JWTAuth\Claims\Factory               $claimFactory
     * @param \Illuminate\Http\Request                    $request
     * @param \Tymon\JWTAuth\Validators\PayloadValidator  $validator
     */
    public function __construct(ClaimFactory $claimFactory, Request $request, PayloadValidator $validator)
    {
        $this->claimFactory = $claimFactory;
        $this->request = $request;
        $this->validator = $validator;

        $this->claims = new Collection;
    }

    /**
     * Create the Payload instance
     *
     * @return \Tymon\JWTAuth\Payload
     */
    public function make()
    {
        $claims = $this->buildClaims()->resolveClaims();

        return new Payload($claims, $this->validator, $this->refreshFlow);
    }

    /**
     * Add an array of claims to the Payload
     *
     * @param  array  $claims
     *
     * @return Factory
     */
    public function addClaims(array $claims)
    {
        foreach ($claims as $name => $value) {
            $this->addClaim($name, $value);
        }

        return $this;
    }

    /**
     * Add a claim to the Payload
     *
     * @param  string  $name
     * @param  mixed   $value
     *
     * @return Factory
     */
    public function addClaim($name, $value)
    {
        $this->claims->put($name, $value);

        return $this;
    }

    /**
     * Build the default claims
     *
     * @return Factory
     */
    protected function buildClaims()
    {
        // add the default claims
        foreach ($this->defaultClaims as $claim) {
            $this->addClaim($claim, $this->$claim());
        }

        // add custom claims on top, allowing them to overwrite defaults
        $this->addClaims($this->customClaims);

        return $this;
    }

    /**
     * Build out the Claim DTO's
     *
     * @return \Illuminate\Support\Collection
     */
    protected function resolveClaims()
    {
        return $this->claims->map(function ($value, $name) {
            return $this->claimFactory->get($name, $value);
        });
    }

    /**<commit_msg>using call_user_func<commit_after><?php

/*
 * This file is part of jwt-auth
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Tymon\JWTAuth;

use Illuminate\Http\Request;
use Tymon\JWTAuth\Support\Utils;
use Tymon\JWTAuth\Claims\Factory as ClaimFactory;
use Illuminate\Support\Collection;
use Tymon\JWTAuth\Support\RefreshFlow;
use Tymon\JWTAuth\Support\CustomClaims;
use Tymon\JWTAuth\Validators\PayloadValidator;

class Factory
{
    use RefreshFlow, CustomClaims;

    /**
     * @var \Tymon\JWTAuth\Claims\Factory
     */
    protected $claimFactory;

    /**
     * @var \Illuminate\Http\Request
     */
    protected $request;

    /**
     * @var \Tymon\JWTAuth\Validators\PayloadValidator
     */
    protected $validator;

    /**
     * @var int
     */
    protected $ttl = 60;

    /**
     * @var array
     */
    protected $defaultClaims = ['iss', 'iat', 'exp', 'nbf', 'jti'];

    /**
     * @var \Illuminate\Support\Collection
     */
    protected $claims;

    /**
     * @param \Tymon\JWTAuth\Claims\Factory               $claimFactory
     * @param \Illuminate\Http\Request                    $request
     * @param \Tymon\JWTAuth\Validators\PayloadValidator  $validator
     */
    public function __construct(ClaimFactory $claimFactory, Request $request, PayloadValidator $validator)
    {
        $this->claimFactory = $claimFactory;
        $this->request = $request;
        $this->validator = $validator;

        $this->claims = new Collection;
    }

    /**
     * Create the Payload instance
     *
     * @return \Tymon\JWTAuth\Payload
     */
    public function make()
    {
        $claims = $this->buildClaims()->resolveClaims();

        return new Payload($claims, $this->validator, $this->refreshFlow);
    }

    /**
     * Add an array of claims to the Payload
     *
     * @param  array  $claims
     *
     * @return Factory
     */
    public function addClaims(array $claims)
    {
        foreach ($claims as $name => $value) {
            $this->addClaim($name, $value);
        }

        return $this;
    }

    /**
     * Add a claim to the Payload
     *
     * @param  string  $name
     * @param  mixed   $value
     *
     * @return Factory
     */
    public function addClaim($name, $value)
    {
        $this->claims->put($name, $value);

        return $this;
    }

    /**
     * Build the default claims
     *
     * @return Factory
     */
    protected function buildClaims()
    {
        // add the default claims
        foreach ($this->defaultClaims as $claim) {
            $this->addClaim($claim, call_user_func([$this, $claim]));
        }

        // add custom claims on top, allowing them to overwrite defaults
        $this->addClaims($this->customClaims);

        return $this;
    }

    /**
     * Build out the Claim DTO's
     *
     * @return \Illuminate\Support\Collection
     */
    protected function resolveClaims()
    {
        return $this->claims->map(function ($value, $name) {
            return $this->claimFactory->get($name, $value);
        });
    }

    /**
{'fraction_non_alphanumeric': 0.12979025390316984, 'fraction_numerical': 0.0006308153288124901, 'mean_word_length': 2.072674418604651, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 50, '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': '3549379', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1961, 'n_words': 444}