Commit 40dc44418383944b9730ded68cb9595785499a5a

Authored by devel-srv
1 parent f4deac97a6
Exists in master

added url utilities library

app/lib/UrlUtils.php
... ... @@ -0,0 +1,88 @@
  1 +<?php
  2 +
  3 +/**
  4 +* Singleton class with a collection of utilities for compressing urls and web content
  5 +*/
  6 +
  7 +namespace Fccn\Lib;
  8 +
  9 +use Fccn\Lib\SiteConfig as SiteConfig;
  10 +use Fccn\Lib\FileLogger;
  11 +
  12 +class UrlUtils
  13 +{
  14 + private static $instance;
  15 +
  16 +
  17 + public static function getInstance()
  18 + {
  19 + if (!self::$instance instanceof self) {
  20 + self::$instance = new self();
  21 + }
  22 +
  23 + return self::$instance;
  24 + }
  25 +
  26 + /*
  27 + * Initializes the Compression utilities
  28 + */
  29 + public function __construct()
  30 + {
  31 + }
  32 +
  33 + /**
  34 + * compresses an array of parameters into a single token for page load requests
  35 + * @param Array $params an associative array of parameters in the form 'param_name' => 'param_value'
  36 + * @return String a token representation of the parameters
  37 + */
  38 + public function compressParams($params){
  39 + return $this->base64UrlEncode(gzcompress(json_encode($params,JSON_UNESCAPED_UNICODE),9));
  40 + }
  41 +
  42 +
  43 + /**
  44 + * decompresses a previously compressed array of parameters back into an array of parameters
  45 + * @param String $token the token to decompress
  46 + */
  47 + public function decompressParams($token){
  48 + return json_decode(gzuncompress($this->base64UrlDecode($token)),true);
  49 + }
  50 +
  51 +
  52 + /**
  53 + * Encodes a string in base64 URL safe
  54 + * @param String $string the string to encode
  55 + * @return String the base64 encoded string
  56 + */
  57 + public function base64UrlEncode($string) {
  58 + $data = base64_encode($string);
  59 + $data = str_replace(array('+','/','='),array('-','_',''),$data);
  60 + return $data;
  61 + }
  62 +
  63 + /**
  64 + * Decodes a base64 string
  65 + * @param string $string the base64 string to decode
  66 + * @return string the decoded string
  67 + */
  68 + public function base64UrlDecode($string) {
  69 + $data = str_replace(array('-','_'),array('+','/'),$string);
  70 + $mod4 = strlen($data) % 4;
  71 + if ($mod4) {
  72 + $data .= substr('====', $mod4);
  73 + }
  74 + return base64_decode($data);
  75 + }
  76 +
  77 + /**
  78 + * Encodes a string to UTF-8 if not already in UTF-8
  79 + * @param String $str the string to encode
  80 + */
  81 + public function toUtf8($str){
  82 + if(!mb_detect_encoding($str, 'UTF-8', true)){
  83 + $str = utf8_encode($str);
  84 + }
  85 + return $str;
  86 + }
  87 +
  88 + }
... ...
tests/unit/UrlUtilsTest.php
... ... @@ -0,0 +1,63 @@
  1 +<?php
  2 +
  3 +/**
  4 +* Tests the UrlUtils singleton
  5 +*/
  6 +
  7 +class UrlUtilsTest extends \Codeception\Test\Unit
  8 +{
  9 + /**
  10 + * @var \UnitTester
  11 + */
  12 + protected $tester;
  13 + private $config;
  14 + private $params;
  15 + private $url;
  16 +
  17 + protected function _before()
  18 + {
  19 + $this->config = \Fccn\Lib\SiteConfig::getInstance();
  20 + $this->params = array("param1" => true, "param2" => 'textparam', "param3" => 10 );
  21 + $this->url = "http://my.test.url/some/path";
  22 + }
  23 +
  24 + protected function _after()
  25 + {
  26 + }
  27 +
  28 + // tests
  29 + public function testGetInstance()
  30 + {
  31 + $instance = \Fccn\Lib\UrlUtils::getInstance();
  32 + $this->assertNotNull($instance);
  33 + }
  34 +
  35 + public function testCompressParams()
  36 + {
  37 + $instance = \Fccn\Lib\UrlUtils::getInstance();
  38 + $this->assertNotNull($instance);
  39 + $compressed = $instance->compressParams($this->params);
  40 + $this->assertNotNull($compressed);
  41 + $this->assertTrue(is_string($compressed));
  42 + $uncompressed = $instance->decompressParams($compressed);
  43 + $this->assertNotNull($uncompressed);
  44 + $this->assertTrue(is_array($uncompressed));
  45 + $this->assertTrue(sizeof($uncompressed) == sizeof($this->params));
  46 + $this->assertTrue(isset($uncompressed['param1']));
  47 + $this->assertTrue($uncompressed['param1'] == $this->params['param1']);
  48 + }
  49 +
  50 + public function testBase64Encode()
  51 + {
  52 + $instance = \Fccn\Lib\UrlUtils::getInstance();
  53 + $this->assertNotNull($instance);
  54 + $encoded = $instance->base64UrlEncode($this->url);
  55 + $this->assertNotNull($encoded);
  56 + $this->assertTrue(is_string($encoded));
  57 + $decoded = $instance->base64UrlDecode($encoded);
  58 + $this->assertNotNull($decoded);
  59 + $this->assertTrue(is_string($decoded));
  60 + $this->assertTrue($decoded == $this->url);
  61 + }
  62 +
  63 +}
... ...