<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 ArrayAccess;
use JsonSerializable;
use Countable;
use Tymon\JWTAuth\Claims\Claim;
use Illuminate\Support\Collection;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;
use Tymon\JWTAuth\Exceptions\PayloadException;
use Tymon\JWTAuth\Validators\PayloadValidator;

class Payload implements ArrayAccess, Arrayable, JsonSerializable, Jsonable, Countable
{
    /**
     * The collection of claims
     *
     * @var \Illuminate\Support\Collection
     */
    private $claims;

    /**
     * Build the Payload
     *
     * @param \Illuminate\Support\Collection              $claims
     * @param \Tymon\JWTAuth\Validators\PayloadValidator  $validator
     * @param boolean                                     $refreshFlow
     */
    public function __construct(Collection $claims, PayloadValidator $validator, $refreshFlow = false)
    {
        $this->claims = $claims;

        $validator->setRefreshFlow($refreshFlow)->check($this->toArray());
    }

    /**
     * Get the array of claim instances
     *
     * @return \Illuminate\Support\Collection
     */
    public function getClaims()
    {
        return $this->claims;
    }

    /**
     * Get the payload
     *
     * @param  mixed  $claim
     *
     * @return mixed
     */
    public function get($claim = null)
    {
        if (! is_null($claim)) {
            if (is_array($claim)) {
                return array_map([$this, 'get'], $claim);
            }

            return array_get($this->toArray(), $claim, false);
        }

        return $this->toArray();
    }

    /**
     * Determine whether the payload has the claim
     *
     * @param  \Tymon\JWTAuth\Claims\Claim  $claim
     *
     * @return boolean
     */
    public function has(Claim $claim)
    {
        return in_array($claim, $this->claims->toArray());
    }

    /**
     * Get the array of claims
     *
     * @return array
     */
    public function toArray()
    {
        $collection = $this->claims->map(function (Claim $claim) {
            return $claim->getValue();
        });

        return $collection->toArray();
    }

    /**
     * Convert the object into something JSON serializable.
     *
     * @return array
     */
    public function jsonSerialize()
    {
        return $this->toArray();
    }

    /**
     * Get the payload as JSON.
     *
     * @param  int  $options
     * @return string
     */
    public function toJson($options = 0)
    {
        return json_encode($this->toArray(), $options);
    }

    /**
     * Get the payload as a string
     *
     * @return string
     */
    public function __toString()
    {
        return $this->toJson(JSON_UNESCAPED_SLASHES);
    }

    /**
     * Determine if an item exists at an offset.
     *
     * @param  mixed  $key
     *
     * @return bool
     */
    public function offsetExists($key)
    {
        return array_key_exists($key, $this->toArray());
    }

    /**
     * Get an item at a given offset.
     *
     * @param  mixed  $key
     *
     * @return mixed
     */
    public function offsetGet($key)
    {
        return array_get($this->toArray(), $key, []);
    }

    /**
     * Don't allow changing the payload as it should be immutable
     *
     * @param  mixed  $key
     * @param  mixed  $value
     *
     * @throws Exceptions\PayloadException
     */
    public function offsetSet($key, $value)
    {
        throw new PayloadException('The payload is immutable');
    }

    /**
     * Don't allow changing the payload as it should be immutable
     *
     * @param  string  $key<commit_msg>using class rather than function<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 Countable;
use ArrayAccess;
use JsonSerializable;
use Illuminate\Support\Arr;
use Tymon\JWTAuth\Claims\Claim;
use Illuminate\Support\Collection;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;
use Tymon\JWTAuth\Exceptions\PayloadException;
use Tymon\JWTAuth\Validators\PayloadValidator;

class Payload implements ArrayAccess, Arrayable, JsonSerializable, Jsonable, Countable
{
    /**
     * The collection of claims
     *
     * @var \Illuminate\Support\Collection
     */
    private $claims;

    /**
     * Build the Payload
     *
     * @param \Illuminate\Support\Collection              $claims
     * @param \Tymon\JWTAuth\Validators\PayloadValidator  $validator
     * @param boolean                                     $refreshFlow
     */
    public function __construct(Collection $claims, PayloadValidator $validator, $refreshFlow = false)
    {
        $this->claims = $claims;

        $validator->setRefreshFlow($refreshFlow)->check($this->toArray());
    }

    /**
     * Get the array of claim instances
     *
     * @return \Illuminate\Support\Collection
     */
    public function getClaims()
    {
        return $this->claims;
    }

    /**
     * Get the payload
     *
     * @param  mixed  $claim
     *
     * @return mixed
     */
    public function get($claim = null)
    {
        if (! is_null($claim)) {
            if (is_array($claim)) {
                return array_map([$this, 'get'], $claim);
            }

            return Arr::get($this->toArray(), $claim, false);
        }

        return $this->toArray();
    }

    /**
     * Determine whether the payload has the claim
     *
     * @param  \Tymon\JWTAuth\Claims\Claim  $claim
     *
     * @return boolean
     */
    public function has(Claim $claim)
    {
        return in_array($claim, $this->claims->toArray());
    }

    /**
     * Get the array of claims
     *
     * @return array
     */
    public function toArray()
    {
        $collection = $this->claims->map(function (Claim $claim) {
            return $claim->getValue();
        });

        return $collection->toArray();
    }

    /**
     * Convert the object into something JSON serializable.
     *
     * @return array
     */
    public function jsonSerialize()
    {
        return $this->toArray();
    }

    /**
     * Get the payload as JSON.
     *
     * @param  int  $options
     * @return string
     */
    public function toJson($options = 0)
    {
        return json_encode($this->toArray(), $options);
    }

    /**
     * Get the payload as a string
     *
     * @return string
     */
    public function __toString()
    {
        return $this->toJson(JSON_UNESCAPED_SLASHES);
    }

    /**
     * Determine if an item exists at an offset.
     *
     * @param  mixed  $key
     *
     * @return bool
     */
    public function offsetExists($key)
    {
        return Arr::has($this->toArray(), $key);
    }

    /**
     * Get an item at a given offset.
     *
     * @param  mixed  $key
     *
     * @return mixed
     */
    public function offsetGet($key)
    {
        return Arr::get($this->toArray(), $key, []);
    }

    /**
     * Don't allow changing the payload as it should be immutable
     *
     * @param  mixed  $key
     * @param  mixed  $value
     *
     * @throws Exceptions\PayloadException
     */
    public function offsetSet($key, $value)
    {
        throw new PayloadException('The payload is immutable');
    }

    /**
     * Don't allow changing the payload as it should be immutable
     *
     * @param  string  $key
{'fraction_non_alphanumeric': 0.11837158044859328, 'fraction_numerical': 0.000259302476338649, 'mean_word_length': 1.9749325106054763, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 45, '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': '6368243', 'n_tokens_mistral': 2317, 'n_tokens_neox': 2289, 'n_words': 577}