Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

php comment

  • Loading branch information...
commit 050178c5f37db9e2432e9d09abacf4c3f93e3e54 1 parent 17e547a
Andrea Giammarchi authored
2  build.php
View
@@ -21,7 +21,7 @@
$output = $info['file'];
// -- simple build
$file = 'build/'.$name.'.js';
-$filemin = str_replace('.js', '.min.js', $file);
+$filemin = preg_replace('/\.js$/', '.min.js', $file);
if(!function_exists('file_put_contents')){
function file_put_contents($file, $content){
$fp = fopen($file, 'wb');
13 build/json.hpack.js
View
@@ -1,9 +1,10 @@
/** json.hpack
- * @version 1.0.1
- * @author Andrea Giammarchi
- * @license Mit Style License
- * @project http://github.com/WebReflection/json.hpack/tree/master
- * @blog http://webreflection.blogspot.com/
+ * @description JSON Homogeneous Collection Packer
+ * @version 1.0.1
+ * @author Andrea Giammarchi
+ * @license Mit Style License
+ * @project http://github.com/WebReflection/json.hpack/tree/master
+ * @blog http://webreflection.blogspot.com/
*/
-/** JSON homogeneous collection packer
+// @author Andrea Giammarchi
13 build/json.hpack.min.js
View
@@ -1,7 +1,8 @@
/** json.hpack
- * @version 1.0.1
- * @author Andrea Giammarchi
- * @license Mit Style License
- * @project http://github.com/WebReflection/json.hpack/tree/master
- * @blog http://webreflection.blogspot.com/
- */(function(cache){(this.JSON||(JSON={})).hpack=function(collection,compression){if(3<compression){var i=JSON.hbest(collection),result=cache[i];cache=[]}else{var indexOf=Array.prototype.indexOf||function(v){for(var l=this.length,i=0;i<l;++i){if(this[i]===v){return i}}return -1},header=[],result=[header],first=collection[0],index=0,k=0,len;for(var key in first){header[index++]=key}len=index;index=0;for(var length=collection.length,i=0;i<length;++i){for(var item=collection[i],row=[],j=0;j<len;++j){row[j]=item[header[j]]}result[++index]=row}++index;if(0<compression){for(row=result[1],j=0;j<len;++j){if(typeof row[j]!="number"){header.splice(j,1,first=[header[j],[]]);first=first[1];first.indexOf=indexOf;for(i=1;i<index;++i){var value=result[i][j],l=first.indexOf(value);result[i][j]=l<0?first.push(value)-1:l}}}}if(1<compression){length-=Math.floor(length/2);for(j=0;j<len;++j){if(header[j] instanceof Array){if(length<(first=header[j][1]).length){for(i=1;i<index;++i){var value=result[i][j];result[i][j]=first[value]}header[j]=header[j][0]}}}}if(2<compression){length=result.length;for(j=0;j<len;++j){if(header[j] instanceof Array){for(row=header[j][1],value=[],first=[],k=0,i=1;i<length;++i){value[k]=row[first[k]=result[i][j]];++k}if(JSON.stringify(value).length<JSON.stringify(first.concat(row)).length){for(k=0,i=1;i<length;++i){result[i][j]=value[k];++k}header[j]=header[j][0]}}}}if(0<compression){for(j=0;j<len;++j){if(header[j] instanceof Array){header.splice(j,1,header[j][0],header[j][1]);++len;++j}}}}return result};JSON.hunpack=function(collection){for(var result=[],keys=[],header=collection[0],len=header.length,length=collection.length,index=-1,k=-1,i=0,l=0,j,row;i<len;++i){keys[++k]=header[i];if(typeof header[i+1]=="object"){++i;for(j=1;j<length;++j){row=collection[j];row[l]=header[i][row[l]]}}++l}row="\\x"+'"'.charCodeAt(0).toString(16);for(i=0,len=keys.length;i<len;++i){keys[i]='o["'.concat(keys[i].replace('"',row),'"]=a[',i,"];")}var anonymous=Function("o,a",keys.join("")+"return o;");for(j=1;j<length;++j){result[++index]=anonymous({},collection[j])}return result};JSON.hclone=function(collection){for(var clone=[],i=0,length=collection.length;i<length;++i){clone[i]=collection[i].slice(0)}return clone};JSON.hbest=function(collection){for(var i=0,j=0,len=0,length=0;i<4;++i){cache[i]=JSON.hpack(collection,i);len=JSON.stringify(cache[i]).length;if(length===0){length=len}else{if(len<length){length=len;j=i}}}return j}})([]);
+ * @description JSON Homogeneous Collection Packer
+ * @version 1.0.1
+ * @author Andrea Giammarchi
+ * @license Mit Style License
+ * @project http://github.com/WebReflection/json.hpack/tree/master
+ * @blog http://webreflection.blogspot.com/
+ */(function(cache){(this.JSON||(JSON={})).hpack=function(collection,compression){if(3<compression){var i=JSON.hbest(collection),result=cache[i];cache=[]}else{var indexOf=Array.prototype.indexOf||function(v){for(var l=this.length,i=0;i<l;++i){if(this[i]===v){return i}}return -1},header=[],result=[header],first=collection[0],index=0,k=0,len;for(var key in first){header[index++]=key}len=index;index=0;for(var length=collection.length,i=0;i<length;++i){for(var item=collection[i],row=[],j=0;j<len;++j){row[j]=item[header[j]]}result[++index]=row}++index;if(0<compression){for(row=result[1],j=0;j<len;++j){if(typeof row[j]!="number"){header[j]=[header[j],first=[]];first.indexOf=indexOf;for(i=1;i<index;++i){var value=result[i][j],l=first.indexOf(value);result[i][j]=l<0?first.push(value)-1:l}}}}if(1<compression){length-=Math.floor(length/2);for(j=0;j<len;++j){if(header[j] instanceof Array){if(length<(first=header[j][1]).length){for(i=1;i<index;++i){var value=result[i][j];result[i][j]=first[value]}header[j]=header[j][0]}}}}if(2<compression){length=result.length;for(j=0;j<len;++j){if(header[j] instanceof Array){for(row=header[j][1],value=[],first=[],k=0,i=1;i<length;++i){value[k]=row[first[k]=result[i][j]];++k}if(JSON.stringify(value).length<JSON.stringify(first.concat(row)).length){for(k=0,i=1;i<length;++i){result[i][j]=value[k];++k}header[j]=header[j][0]}}}}if(0<compression){for(j=0;j<len;++j){if(header[j] instanceof Array){header.splice(j,1,header[j][0],header[j][1]);++len;++j}}}}return result};JSON.hunpack=function(collection){for(var result=[],keys=[],header=collection[0],len=header.length,length=collection.length,index=-1,k=-1,i=0,l=0,j,row;i<len;++i){keys[++k]=header[i];if(typeof header[i+1]=="object"){++i;for(j=1;j<length;++j){row=collection[j];row[l]=header[i][row[l]]}}++l}for(i=0,len=keys.length;i<len;++i){keys[i]='o["'.concat(keys[i].replace('"',"\\x22"),'"]=a[',i,"];")}var anonymous=Function("o,a",keys.join("")+"return o;");for(j=1;j<length;++j){result[++index]=anonymous({},collection[j])}return result};JSON.hclone=function(collection){for(var clone=[],i=0,length=collection.length;i<length;++i){clone[i]=collection[i].slice(0)}return clone};JSON.hbest=function(collection){for(var i=0,j=0,len=0,length=0;i<4;++i){cache[i]=JSON.hpack(collection,i);len=JSON.stringify(cache[i]).length;if(length===0){length=len}else{if(len<length){length=len;j=i}}}return j}})([]);
3  config.ini
View
@@ -1,5 +1,6 @@
[info]
-name=JSON Homogeneous Collection Packer
+name=json.hpack
+description=JSON Homogeneous Collection Packer
version=1.0.1
author=Andrea Giammarchi
license=Mit Style License
2  src/javascript/json.hpack.js
View
@@ -1 +1 @@
-// @author Andrea Giammarchi
+// @author Andrea Giammarchi
174 src/php/json.hpack.php
View
@@ -0,0 +1,174 @@
+<?php
+
+/** json.hpack for PHP
+ * @description JSON Homogeneous Collection Packer
+ * @version 1.0.1
+ * @author Andrea Giammarchi
+ * @license Mit Style License
+ * @project http://github.com/WebReflection/json.hpack/tree/master
+ * @blog http://webreflection.blogspot.com/
+ */
+
+/** json_hpack(homogeneousCollection:Array[, compression:Number]):Array
+ * @param Array mono dimensional homogeneous collection of objects to pack
+ * @param [Number] optional compression level from 0 to 4 - default 0
+ * @return Array optimized collection
+ */
+function json_hpack($collection, $compression = 0){
+ if(3 < $compression){
+ $i = json_hbest($collection);
+ $result = json_hbest($i);
+ } else {
+ $header = array();
+ $result = array(&$header);
+ $first = $collection[0];
+ $k = 0;
+ foreach($first as $key => $value)
+ $header[] = $key;
+ $len = count($header);
+ for($length = count($collection), $i = 0; $i < $length; ++$i){
+ for(
+ $item = $collection[$i],
+ $row = array(),
+ $j = 0;
+ $j < $len; ++$j
+ )
+ $row[$j] = $item->$header[$j];
+ $result[] = $row;
+ }
+ $index = count($result);
+ if(0 < $compression){
+ for($row = $result[1], $j = 0; $j < $len; ++$j){
+ if(!is_float($row[$j]) && !is_int($row[$j])){
+ $first = array();
+ $header[$j] = array($header[$j], &$first);
+ for($i = 1; $i < $index; ++$i){
+ $value = $result[$i][$j];
+ $l = array_search($value, $first, true);
+ $result[$i][$j] = $l === false ? array_push($first, $value) - 1 : $l;
+ }
+ unset($first);
+ }
+ }
+ }
+ if(1 < $compression){
+ $length -= floor($length / 2);
+ for($j = 0; $j < $len; ++$j){
+ if(is_array($header[$j])){
+ if($length < count($first = $header[$j][1])){
+ for($i = 1; $i < $index; ++$i){
+ $value = $result[$i][$j];
+ $result[$i][$j] = $first[$value];
+ }
+ $header[$j] = $header[$j][0];
+ }
+ }
+ }
+ }
+ if(2 < $compression){
+ $length = count($result);
+ for($j = 0; $j < $len; ++$j){
+ if(is_array($header[$j])){
+ for($row = $header[$j][1], $value = array(), $first = array(), $k = 0, $i = 1; $i < $length; ++$i){
+ $value[$k] = $row[$first[$k] = $result[$i][$j]];
+ ++$k;
+ }
+ if(strlen(json_encode($value)) < strlen(json_encode(array_merge($first, $row)))){
+ for($k = 0, $i = 1; $i < $length; ++$i){
+ $result[$i][$j] = $value[$k];
+ ++$k;
+ }
+ $header[$j] = $header[$j][0];
+ }
+ }
+ }
+ }
+ if(0 < $compression){
+ for($j = 0; $j < $len; ++$j){
+ if(is_array($header[$j])){
+ $enum = $header[$j][1];
+ $header[$j] = $header[$j][0];
+ array_splice($header, $j + 1, 0, array($enum));
+ ++$len;
+ ++$j;
+ }
+ }
+ }
+ }
+ return $result;
+}
+
+/** json_hunpack(packedCollection:Array):Array
+ * @param Array optimized collection to unpack
+ * @return Array original mono dimensional homogeneous collection of objects
+ */
+function json_hunpack($collection){
+ for(
+ $result = array(),
+ $keys = array(),
+ $header = $collection[0],
+ $len = count($header),
+ $length = count($collection),
+ $i = 0,
+ $l = 0;
+ $i < $len; ++$i
+ ){
+ $keys[] = $header[$i];
+ if(is_array($header[$i + 1])){
+ ++$i;
+ for($j = 1; $j < $length; ++$j){
+ $row = &$collection[$j];
+ $row[$l] = $header[$i][$row[$l]];
+ };
+ };
+ ++$l;
+ };
+ for($j = 1; $j < $length; ++$j)
+ $result[] = json_hunpack_createRow($keys, $collection[$j]);
+ return $result;
+}
+
+/** json_hunpack_createRow(objectKeys:Array, values:Array):stdClass
+ * @param Array a list of keys to assign
+ * @param Array a list of values to assign
+ * @return stdClass object representing the row
+ */
+function json_hunpack_createRow($keys, $array){
+ $o = new StdClass;
+ for($i = 0, $len = count($keys); $i < $len; ++$i)
+ $o->$keys[$i] = $array[$i];
+ return $o;
+}
+
+/** json_hbest(packedCollection:Array):Number
+ * @param Array optimized collection to clone
+ * @return Number best compression option
+ */
+function json_hbest($collection){
+ static $_cache = array();
+ if(is_array($collection)){
+ for($i = 0,
+ $j = 0,
+ $len = 0,
+ $length = 0;
+ $i < 4;
+ ++$i
+ ){
+ $_cache[$i] = json_hpack($collection, $i);
+ $len = strlen(json_encode($_cache[$i]));
+ if($length === 0)
+ $length = $len;
+ elseif($len < $length){
+ $length = $len;
+ $j = $i;
+ }
+ }
+ return $j;
+ } else {
+ $result = $_cache[$collection];
+ $_cache = array();
+ return $result;
+ }
+}
+
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.