Skip to content

Commit

Permalink
Clear directives, and set on construct
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Feb 10, 2020
1 parent 9c75e72 commit 4441c13
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/Headers/ContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ContentSecurityPolicy implements Header
const REFERRER = 'referrer';
const UPGRADE_INSECURE_REQUESTS = 'upgrade-insecure-requests';

protected $_directives = ['default-src' => ["'self'"]];
protected $_directives;

public function getKey(): string
{
Expand All @@ -63,6 +63,36 @@ public function getValue()
return implode('; ', $directives);
}

public function __construct(array $directives = [self::DEFAULT_SRC => ["'self'"]])
{
$this->_directives = $directives;
}

public static function blank()
{
return new static([]);
}

/**
* Clear all directives, or a single type
*
* @param null|string $directive
*
* @return $this
*/
public function clearDirective($directive = null)
{
if($directive === null)
{
$this->_directives = [];
}
else
{
unset($this->_directives[$directive]);
}
return $this;
}

public function setDirective($directive, ...$src)
{
$this->_directives[$directive] = $src;
Expand Down

0 comments on commit 4441c13

Please sign in to comment.