a linq for php

:::brush:php;toolbar:false
/*
今天研究了下一个以linq方式操作数组的php类Plinq http://plinq.codeplex.com/,然后在不继承php标准库SPL RecursiveArrayIterator的情况下重新实现了下。
项目地址:https://github.com/wosiwo/alinq
这里贴下测试代码
*/
date_default_timezone_set("Asia/Shanghai");

include('alinq.php');


$testArray = array('key_0' => array('int' => 0, 'date' => new \DateTime(date('Y-m-d H:i:s', time()+(0))), 'string' => 'val_0', 'bool' => true), 'key_1' => array('int' => 1, 'date' => new \DateTime(date('Y-m-d H:i:s', time()+(86400))), 'string' => 'val_1', 'bool' => false), 'key_2' => array('int' => 2, 'date' => new \DateTime(date('Y-m-d H:i:s', time()+(172800))), 'string' => 'val_2', 'bool' => true), 'key_3' => array('int' => 3, 'date' => new \DateTime(date('Y-m-d H:i:s', time()+(259200))), 'string' => 'val_3', 'bool' => false),);

    TestWhere($testArray);
    TestSkip($testArray);
    TestTake($testArray);
    TestAll($testArray);
    TestAny($testArray);
    TestAverage($testArray);
    TestOrderBy($testArray);
    TestMax($testArray);
    TestMin($testArray);
    TestSelect($testArray);
    TestSelectMany($testArray);
    TestConcat($testArray);
    TestGroupBy($testArray);
    TestSingle($testArray);
    testTime($testArray);


function arrayTest(&$testArray){
    $p = new Plinq($testArray);
    $result = $p->ElementAt(111);
}


function TestSingle(&$testArray)
{    
    $p = new Plinq($testArray);
    $result = $p->Single(function($k, $v){ return $v['int'] > 10; });

    assert('$result["int"] == 101');       
}

function TestGroupBy(&$testArray)
{    
    $p = new Plinq($testArray);
    $result = $p->GroupBy(function($k, $v){ 
        return (date('Y-m',$v['date']->getTimeStamp()))->ToArray(); 
    });

    assert('array_key_exists("2012-06", $result)');
    assert('$result["2012-06"]["key_58"]["int"] == 58');     
}

function TestConcat(&$testArray)
{    
    $p = new Plinq($testArray);
    $result = $p->Concat(array('key_3' => 'concat', 'key_1000' => 'concat'))->ToArray();
      
    assert('array_key_exists("key_1000", $result)');
    assert('$result["key_1000"] == "concat"');
    assert('$result["key_3"] == "concat"');    
    assert('count($result)-1 == count($testArray)');    
}

function TestSelectMany(&$testArray)
{    
    $p = new Plinq($testArray);
    $result = $p->SelectMany(function($k, $v){ return $v; })->ToArray();
    
    assert('array_key_exists(22, $result)');
    assert('$result[22] == "val_5"');
}

function TestSelect(&$testArray)
{    
    $p = new Plinq($testArray);
    $result = $p->Select(function($k, $v){ return $v['int']; })->ToArray();    
    
    assert('array_key_exists("key_126", $result)');
    assert('$result["key_126"] == 126');    
}

function TestMin(&$testArray)
{    
    $p = new Plinq($testArray);
    $result = $p->Min(function($k, $v){ return $v['int']; });
    
    assert('$result == 0');
}

function TestMax(&$testArray)
{    
    $p = new Plinq($testArray);
    $result = $p->Max(function($k, $v){ return $v['int']; });    
    assert('$result == 999');
}

function TestOrderBy(&$testArray)
{    
    $p = new Plinq($testArray);
    $result = $p->OrderByDescending(function($k, $v){ return $v['int']; })->ToArray();    
    
    assert('key($result) == "key_999"');
    
    $p = new Plinq($testArray);
    $result = $p->OrderByDescending(function($k, $v){ return $v['date']; })->ToArray();    
  
    assert('key($result) == "key_999"');    
    
    $p = new Plinq($testArray);
    $result = $p->OrderBy(function($k, $v){ return $v['string']; })->ToArray();    
  
    assert('key($result) == "key_0"');       
}

function TestAverage(&$testArray)
{    
    $p = new Plinq($testArray);
    $result = $p->Average(function($k, $v){ return $v['int']; });    

    assert('$result == 499.5');
}

function TestWhere(&$testArray)
{    
    $p = new Plinq($testArray);
    $result = $p->Where(function($k, $v){ return ($v['int'] > 998); })->ToArray();
  
    assert('count($result) == 1');
    assert('$result["key_999"]["int"] == 999');
    assert('!array_key_exists("key_998", $result)');      
}

function TestSkip(&$testArray)
{
    $p = new Plinq($testArray);
    $result = $p->Where(function($k, $v){ return ($v['int'] > 997); })
                ->Skip(1)->ToArray();
              
    assert('count($result) == 1');    
    assert('$result["key_999"]["int"] == 999');
    assert('!array_key_exists("key_998", $result)');    
}

function TestTake(&$testArray)
{
    $p = new Plinq($testArray);
    $result = $p->Where(function($k, $v){ return ($v['int'] > 996); })
                ->Take(1)->ToArray();
              

}

function TestAll(&$testArray)
{
    $p = new Plinq($testArray);
    $result = $p->All(function($k, $v){ return ($v['int'] < 1000); });
  
    assert('$result == true');    
}

function TestAny(&$testArray)
{
    $p = new Plinq($testArray);
    $result = $p->Any(function($k, $v){ return ($v['int'] > 1000); });   
   
}
    

This article is my 7th oldest. It is 418 words long