src/CSPBuilder.php
public function nonce(string $directive = 'script-src', string $nonce = ''): string
    {
        $ruleKeys = \array_keys($this->policies);
        if (!\in_array($directive, $ruleKeys)) {
            return '';
        }

        if (empty($nonce)) {
            $nonce = Base64::encode(\random_bytes(18));
        }
        $this->policies[$directive]['nonces'] []= $nonce;
        return $nonce;
    }
public function nonce(string $directive = 'script-src', string $nonce = ''): string
    {
        $ruleKeys = \array_keys($this->policies);
        if (!\in_array($directive, $ruleKeys)) {
            return '';
        }

        if (empty($nonce)) {
            $nonce = Base64::encode(\random_bytes(18));
        }
        $this->policies[$directive]['nonces'] []= $nonce;
        return $nonce;
    }
public function nonce ( string $ directive = 'script-src' , string $ nonce = '' ) : string { $ ruleKeys = \ array_keys ( $ this -> policies ) ; if ( ! \ in_array ( $ directive , $ ruleKeys ) ) { return '' ; } if ( empty ( $ nonce ) ) { $ nonce = Base64 :: encode ( \ random_bytes ( 18 ) ) ; } $ this -> policies [ $ directive ] [ 'nonces' ] [ ] = $ nonce ; return $ nonce ; }
Add a new nonce to the existing CSP. Returns the nonce generated.

@param string $directive
@param string $nonce (if empty, it will be generated)
@return string
@throws \Exception
Add a new nonce to the existing CSP . Returns the nonce generated .