diff --git a/composer.json b/composer.json index fd45950..d0fff14 100644 --- a/composer.json +++ b/composer.json @@ -4,6 +4,9 @@ "keywords": [ "content-security-policy", "csp", + "http", + "headers", + "security", "xss" ], "license": "MIT", @@ -31,6 +34,7 @@ "paragonie/constant_time_encoding": "^2.0" }, "require-dev": { + "psr/http-message": "^1", "phpunit/phpunit": "4.*|5.*" }, "suggest": { diff --git a/src/CSPBuilder.php b/src/CSPBuilder.php index 358f610..15f92b7 100644 --- a/src/CSPBuilder.php +++ b/src/CSPBuilder.php @@ -219,12 +219,12 @@ public function enableOldBrowserSupport(): self */ public static function fromFile(string $filename = ''): self { - if (!file_exists($filename)) { + if (!\file_exists($filename)) { throw new \Exception($filename.' does not exist'); } - $json = \file_get_contents($filename); - $array = \json_decode($json, true); - return new CSPBuilder($array); + return self::fromData( + \file_get_contents($filename) + ); } /** @@ -232,12 +232,13 @@ public static function fromFile(string $filename = ''): self * * @param string $data * @return CSPBuilder + * @throws \Exception */ public static function fromData($data = ''): self { $array = \json_decode($data, true); - if(!is_array($array)) { + if(!\is_array($array)) { throw new \Exception('Is not array valid'); } @@ -348,7 +349,7 @@ public function nonce(string $directive = 'script-src', string $nonce = ''): str { $ruleKeys = \array_keys($this->policies); if (!\in_array($directive, $ruleKeys)) { - return null; + return ''; } if (empty($nonce)) {