Skip to content

Commit

Permalink
Add the _encode and _decode functions to the cookie class.
Browse files Browse the repository at this point in the history
  • Loading branch information
7r1n17y committed Jul 9, 2017
1 parent 28b1a88 commit 770f981
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion src/Manage/Cookie.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace r7r1n17y\Myriad\Manage;
use r7r1n17y\Myriad\Interfaces;
class Cookie implements Handler {
class Cookie extends Extender implements Handler {
private $params = array();
public function __construct(array $options = array()) {
$this->params = session_get_cookie_params();
Expand Down Expand Up @@ -48,5 +48,58 @@ public function delete(string $name = null) {
setcookie($name, '', time() - 42000, $this->params['path'], $this->params['domain'], $this->params['secure'], $this->params['httponly']);
}
}
private function _encode(array $options = array('val' => null)) {
$val = isset($options['val']) ? $options['val'] : null;
if (is_array($val)) {
$res = 'a|' . json_encode($val);
} elseif (is_bool($val)) {
if ($val) {
$res = 'b|t';
} else {
$res = 'b|f';
}
} elseif (is_int($val)) {
$res = 'i|' . strval($val);
} elseif (is_float($val)) {
$res = 'f|' . strval($val);
} elseif (is_string($val)) {
$res = "s|$val";
} else {
$res = 'n|n';
}
return $res;
}
private function _decode(string $encoded = null) {
if (!is_string($encoded) || is_null($encoded)) {
throw new \InvalidArgumentException(sprintf('$encoded only accepts strings. Input was: "%s"', gettype($encoded));
}
if (empty($encoded)) {
throw new \InvalidArgumentException('$encoded can not be empty.');
}
$fc = mb_substr($encoded, 0, 1);
if ($this->equals($fc, 'a')) {
$encoded = ltrim($encoded, 'a|');
return json_decode($encoded);
} elseif ($this->equals($fc, 'b')) {
$encoded = ltrim($encoded, 'b|');
if ($this->equals($encoded, 't')) {
return true;
} else {
return false;
}
} elseif ($this->equals($fc, 'i')) {
$encoded = ltrim($encoded, 'i|');
return (int) $encoded;
} elseif ($this->equals($fc, 'f')) {
$encoded = ltrim($encoded, 'f|');
return (float) $encoded;
}
if ($this->equals($fc, 's')) {
$encoded = ltrim($encoded, 's|');
return $encoded;
} else {
return null;
}
}
}
?>

0 comments on commit 770f981

Please sign in to comment.