PHP code example of hotrush / laravel-signer

1. Go to this page and download the library: Download hotrush/laravel-signer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hotrush / laravel-signer example snippets


    use Hotrush\Signer\Contracts\Signable;
    
    class Post extends Model implements Signable
    {
    
    }
    

    use Hotrush\Signer\Contracts\Signable;
    use Hotrush\Signer\Contracts\Traits\CanBeSigned;
        
    class Post extends Model implements Signable
    {
        use CanBeSigned;
        
        /**
         * Return null if never expires.
         *
         * @return Carbon|null
         */
        public function getSignExpiration(): ?Carbon
        {
            return null;
        }
    
        /**
         * Payload used for making signature hash.
         *
         * @return array
         */
        public function getSignPayload(): array
        {
            return [
                $this->getKeyName() => $this->getKey(),
                'field' => $this->field,
            ];
        }
    
        /**
         * Payload put into encoded code. Will be publicly accessible.
         *
         * @return array
         */
        public function getPublicSignPayload(): array
        {
            return [
                $this->getKeyName() => $this->getKey(),
            ];
        }
        
        /**
         * Define where clause for getting signable model instance by signature.
         * Only values from public payload can be used.
         */
        public static function signableClauses(Signature $signature): \Closure
        {
            return function (Builder $query) use ($signature) {
                $query->where('id', '=', $signature->payload['id']);
            };
        }
    }
    

   use Hotrush\Signer\Facades\Signer;
   
   $signable = Post::find(1);
   $signature = Signer::generate($signable);
   
   echo (string) $signature;
   

   use Hotrush\Signer\Facades\Signer;
   use Hotrush\Signer\Signature;
   
   // decode signature
   $signature = Signature::decode('signature-string-value');
   
   // get signable
   $signable = Post::findSignable($signature);
   
   // verify
   $valid = Signer::validate($signable, $signature);