diff --git a/README-ES.md b/README-ES.md index 9a7e461..7c1280c 100644 --- a/README-ES.md +++ b/README-ES.md @@ -102,10 +102,22 @@ Cookie::destroy($key); **# Return** (boolean) +### - Establecer prefijo para cookies: + +```php +Cookie::setPrefix($prefix); +``` + +| Atributo | Descripción | Tipo | Requerido | Predeterminado +| --- | --- | --- | --- | --- | +| $prefix | Prefijo para las cookies. | string | Sí | | + +**# Return** (boolean) + ### - Obtener prefijo de cookies: ```php -Cookie::getCookiePrefix(); +Cookie::getPrefix(); ``` **# Return** (string) → prefijo de cookies @@ -168,10 +180,16 @@ Cookie::destroy('cookie_name'); Cookie::destroy(); ``` +### - Establecer prefijo para cookies: + +```php +Cookie::setPrefix('prefix_'); +``` + ### - Obtener prefijo de cookies: ```php -Cookie::getCookiePrefix(); +Cookie::getPrefix(); ``` ## Tests diff --git a/README.md b/README.md index 8a25ebf..ecb55c5 100644 --- a/README.md +++ b/README.md @@ -102,10 +102,22 @@ Cookie::destroy($key); **# Return** (boolean) +### - Set cookie prefix: + +```php +Cookie::setPrefix($prefix); +``` + +| Attribute | Description | Type | Required | Default +| --- | --- | --- | --- | --- | +| $prefix | Cookie prefix. | string | Yes | | + +**# Return** (boolean) + ### - Get cookie prefix: ```php -Cookie::getCookiePrefix(); +Cookie::getPrefix(); ``` **# Return** (string) → cookie prefix @@ -168,10 +180,16 @@ Cookie::destroy('cookie_name'); Cookie::destroy(); ``` +### - Set cookie prefix: + +```php +Cookie::setPrefix('prefix_'); +``` + ### - Get cookie prefix: ```php -Cookie::getCookiePrefix(); +Cookie::getPrefix(); ``` ## Tests diff --git a/src/Cookie.php b/src/Cookie.php index 815d69e..0169524 100644 --- a/src/Cookie.php +++ b/src/Cookie.php @@ -31,7 +31,7 @@ class Cookie * @param string $value → the data to save * @param string $time → expiration time in days * - * @return bool + * @return boolean */ public static function set($key, $value, $time = 365) { @@ -79,7 +79,7 @@ public static function pull($key) * * @param string $key → cookie name to destroy. Not set to delete all * - * @return bool + * @return boolean */ public static function destroy($key = '') { @@ -100,6 +100,25 @@ public static function destroy($key = '') return false; } + /** + * Set cookie prefix. + * + * @since 1.1.6 + * + * @param string $prefix → cookie prefix + * + * @return boolean + */ + public static function setPrefix($prefix) + { + if (!empty($prefix) && is_string($prefix)) { + self::$prefix = $prefix; + return true; + } + + return false; + } + /** * Get cookie prefix. * @@ -107,7 +126,7 @@ public static function destroy($key = '') * * @return string */ - public static function getCookiePrefix() + public static function getPrefix() { return self::$prefix; } diff --git a/tests/CookieTest.php b/tests/CookieTest.php index b0227ed..f063cbe 100644 --- a/tests/CookieTest.php +++ b/tests/CookieTest.php @@ -50,7 +50,7 @@ public function setUp() $cookie = $this->Cookie; - $this->cookiePrefix = $cookie::getCookiePrefix(); + $this->cookiePrefix = $cookie::getPrefix(); } /** @@ -197,4 +197,48 @@ public function testDestroyAllCookiesNonExistents() $this->assertFalse($cookie::destroy()); } + + /** + * Get cookie prefix. + * + * @runInSeparateProcess + * + * @since 1.1.6 + */ + public function testGetCookiePrefix() + { + $cookie = $this->Cookie; + + $this->assertContains($cookie::getPrefix(), 'jst_'); + } + + /** + * Set cookie prefix. + * + * @runInSeparateProcess + * + * @since 1.1.6 + */ + public function testSetCookiePrefix() + { + $cookie = $this->Cookie; + + $this->assertTrue($cookie::setPrefix('prefix_')); + } + + /** + * Set cookie prefix incorrectly. + * + * @runInSeparateProcess + * + * @since 1.1.6 + */ + public function testSetCookieIncorrectly() + { + $cookie = $this->Cookie; + + $this->assertFalse($cookie::setPrefix('')); + $this->assertFalse($cookie::setPrefix(5)); + $this->assertFalse($cookie::setPrefix(true)); + } }