Example #1
0
 public function Query($query, $params = array())
 {
     $pool = $this->pool->getPool();
     try {
         $result = Flow::of($pool->query($query, $params))->map(function (SqlResult $result) {
             if (count($result) > 0) {
                 return $result->toArray();
             } else {
                 return array();
             }
         })->toArray();
     } catch (Exception $e) {
         $result = array();
     }
     return $result;
 }
Example #2
0
--TEST--
Basic cursor test with stream
--FILE--
<?php 
use php\io\MemoryStream;
use php\util\Flow;
$buf = new MemoryStream();
$buf->write('foobar');
$buf->seek(0);
$cur = Flow::ofStream($buf);
var_dump($cur->toString(','));
$buf->seek(0);
$cur = Flow::ofStream($buf, 3);
var_dump($cur->toString(','));
$buf->seek(2);
$cur = Flow::ofStream($buf, 2);
var_dump($cur->toString(','));
?>
--EXPECT--
string(11) "f,o,o,b,a,r"
string(7) "foo,bar"
string(5) "ob,ar"
Example #3
0
--TEST--
Basic cursor test toString
--FILE--
<?php 
use php\util\Flow;
var_dump(Flow::of(['foo', 'bar'])->toString(','));
var_dump(Flow::of(['fail', 'fail', 'foo', 'bar', 'fail'])->skip(2)->limit(2)->toString(','));
?>
--EXPECT--
string(7) "foo,bar"
string(7) "foo,bar"
Example #4
0
--TEST--
SqlDriverManager test insert into table #2
--FILE--
<?php 
use php\sql\SqlDriverManager;
use php\sql\SqlResult;
use php\util\Flow;
SqlDriverManager::install('sqlite');
$conn = SqlDriverManager::getConnection('sqlite::memory:');
$conn->query('create table person (id integer, name string)')->update();
$result = $conn->query("insert into person values(?, ?)", [1, 'leo'])->update();
$result += $conn->query("insert into person values(?, ?)", [2, 'yui'])->update();
var_dump($result);
$array = Flow::of($conn->query('select * from person'))->map(function (SqlResult $result) {
    return $result->toArray();
})->toArray();
var_dump($array);
?>
--EXPECTF--
int(2)
array(2) {
  [0]=>
  array(2) {
    ["id"]=>
    int(1)
    ["name"]=>
    string(3) "leo"
  }
  [1]=>
  array(2) {
    ["id"]=>
Example #5
0
--TEST--
Basic cursor test repetandly
--FILE--
<?php 
use php\util\Flow;
$arr = [1, 2];
$cur = Flow::of($arr);
var_dump($cur->count());
try {
    var_dump($cur->count());
} catch (Exception $e) {
    var_dump($e->getMessage());
}
?>
--EXPECT--
int(2)
string(37) "Unable to iterate the flow repeatedly"
Example #6
0
use php\util\SharedMap;
use php\util\SharedStack;
$stack = new SharedStack([1, -2]);
var_dump($stack->isEmpty());
var_dump($stack->count());
var_dump($stack->push(3));
var_dump($stack->pop());
var_dump($stack->peek());
var_dump($stack->pop());
var_dump($stack->pop());
var_dump($stack->pop());
var_dump($stack->isEmpty());
var_dump($stack->count());
$stack->push('foo');
$stack->push('bar');
var_dump(Flow::of($stack)->toArray());
$stack->clear();
var_dump($stack->isEmpty());
var_dump($stack->count());
?>
--EXPECT--
bool(false)
int(2)
int(3)
int(3)
int(-2)
int(-2)
int(1)
NULL
bool(true)
int(0)
Example #7
0
--TEST--
SharedQueue basic test
--FILE--
<?php 
use php\util\Flow;
use php\util\SharedMap;
use php\util\SharedQueue;
use php\util\SharedStack;
$queue = new SharedQueue([1, 2, 3]);
var_dump($queue->count());
var_dump($queue->isEmpty());
var_dump($queue->poll());
var_dump($queue->peek());
var_dump(Flow::of($queue)->toArray());
$queue->add(4);
var_dump($queue->peek());
var_dump($queue->poll());
var_dump($queue->poll());
var_dump($queue->count());
var_dump($queue->isEmpty());
var_dump($queue->poll());
var_dump($queue->poll());
$queue->clear();
var_dump($queue->count());
var_dump($queue->isEmpty());
?>
--EXPECT--
int(3)
bool(false)
int(1)
int(2)
Example #8
0
--TEST--
Flow toArray
--FILE--
<?php 
use php\util\Flow;
$arr1 = Flow::of(['x' => 1, 2, 3])->toArray();
$arr2 = Flow::of(['x' => 100, 'y' => 500])->withKeys()->toArray();
var_dump($arr1, $arr2);
?>
--EXPECTF--
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}
array(2) {
  ["x"]=>
  int(100)
  ["y"]=>
  int(500)
}
Example #9
0
--TEST--
IteratorIterator with iterator
--FILE--
<?php 
use php\util\Flow;
$it = new IteratorIterator(Flow::ofRange(1, 3));
foreach ($it as $key => $el) {
    var_dump($el);
    var_dump($key);
    echo "\n";
}
foreach ($it as $el) {
    echo $el;
}
?>
--EXPECT--
int(1)
int(0)

int(2)
int(1)

int(3)
int(2)
Example #10
0
        $this->i++;
    }
    public function key()
    {
        return $this->i;
    }
    public function valid()
    {
        return $this->i < 5;
    }
    public function rewind()
    {
        $this->i = 0;
    }
}
$cursor = Flow::of(new MyIter());
foreach ($cursor as $el) {
    var_dump($el);
}
echo "--\n";
foreach (new MyIter() as $el) {
    var_dump($el);
}
?>
--EXPECT--
int(0)
int(1)
int(2)
int(3)
int(4)
--
Example #11
0
--TEST--
Basic cursor test find
--FILE--
<?php 
use php\util\Flow;
$arr = [1, 2, 3, 4];
$filter = function ($e) {
    return $e % 2 == 0;
};
foreach (Flow::of($arr)->find($filter) as $el) {
    var_dump($el);
}
echo "--with-skip\n";
foreach (Flow::of($arr)->skip(2)->find($filter) as $el) {
    var_dump($el);
}
?>
--EXPECT--
int(2)
int(4)
--with-skip
int(4)
Example #12
0
--TEST--
Basic cursor test append
--FILE--
<?php 
use php\util\Flow;
$arr = [1, 2];
$callback = function ($e) {
    var_dump($e);
};
echo "--simple\n";
Flow::of($arr)->append(['foo', 'bar'])->each($callback);
echo "--with-result\n";
Flow::of($arr)->skip(1)->append(['foo', 'bar'])->each($callback);
?>
--EXPECT--
--simple
int(1)
int(2)
string(3) "foo"
string(3) "bar"
--with-result
int(2)
string(3) "foo"
string(3) "bar"
Example #13
0
--TEST--
Basic cursor test sort by keys
--FILE--
<?php 
use php\util\Flow;
echo "--test-sort-by-keys\n";
$arr = [3 => 1, 1 => 2, 2 => 3];
var_dump(Flow::of($arr)->sortByKeys());
echo "--test-sort-by-keys-with-keys\n";
var_dump(Flow::of($arr)->withKeys()->sortByKeys());
echo "--test-sort-by-keys-with-comparator\n";
var_dump(Flow::of($arr)->withKeys()->sortByKeys(function ($key1, $key2) {
    if ($key1 == $key2) {
        return 0;
    }
    return $key1 > $key2 ? -1 : 1;
}));
?>
--EXPECT--
--test-sort-by-keys
array(3) {
  [0]=>
  int(2)
  [1]=>
  int(3)
  [2]=>
  int(1)
}
--test-sort-by-keys-with-keys
array(3) {
  [1]=>
Example #14
0
$cursor = Flow::ofRange(5, 7);
foreach ($cursor as $el) {
    var_dump($el);
}
echo "--test range with step\n";
$cursor = Flow::ofRange(5, 10, 2);
foreach ($cursor as $el) {
    var_dump($el);
}
echo "--test string\n";
$cursor = Flow::ofString('foo');
foreach ($cursor as $el) {
    var_dump($el);
}
echo "--test string with chunk size\n";
$cursor = Flow::ofString('foobar', 2);
foreach ($cursor as $el) {
    var_dump($el);
}
?>
--EXPECT--
--test array
int(1)
int(2)
int(3)
--test range
int(5)
int(6)
int(7)
--test range with step
int(5)
Example #15
0
--TEST--
Basic cursor test eachSlice
--FILE--
<?php 
use php\util\Flow;
$cursor = Flow::of(['a' => 10, 'b' => 20, 'c' => 30, 'd' => 40, 'e' => 50]);
$cursor->eachSlice(2, function ($array) {
    var_dump($array);
});
echo "--with keys\n";
$cursor = Flow::of(['a' => 10, 'b' => 20, 'c' => 30, 'd' => 40, 'e' => 50]);
$cursor->withKeys()->eachSlice(2, function ($array) {
    var_dump($array);
});
?>
--EXPECT--
array(2) {
  [0]=>
  int(10)
  [1]=>
  int(20)
}
array(2) {
  [0]=>
  int(30)
  [1]=>
  int(40)
}
array(1) {
  [0]=>
  int(50)
Example #16
0
--TEST--
Basic cursor test current and key
--FILE--
<?php 
use php\util\Flow;
$cursor = Flow::of(['x' => 1, 'y' => 2]);
var_dump($cursor->current());
var_dump($cursor->key());
$cursor->next();
var_dump($cursor->current());
var_dump($cursor->key());
?>
--EXPECT--
int(1)
string(1) "x"
int(2)
string(1) "y"
Example #17
0
--FILE--
<?php 
use php\util\Flow;
use php\util\SharedMap;
$map = new SharedMap([1, -2]);
var_dump($map->set(1, 2));
var_dump($map->set(2, 3));
var_dump($map->has(1));
var_dump(Flow::of($map)->toArray());
var_dump($map->remove(1));
var_dump($map->count());
var_dump($map->has(1));
var_dump($map->isEmpty());
$map->clear();
var_dump($map->isEmpty());
var_dump(Flow::of($map)->toArray());
?>
--EXPECT--
int(-2)
NULL
bool(true)
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}
int(2)
int(2)
Example #18
0
--TEST--
Basic cursor test sort
--FILE--
<?php 
use php\util\Flow;
echo "--test-sort\n";
$arr = [3, 1, 5, 2, 7, '0'];
var_dump(Flow::of($arr)->sort());
echo "--test-sort-with-keys\n";
$arr = ['x' => 3, 'y' => 1, 'z' => 2];
var_dump(Flow::of($arr)->withKeys()->sort());
echo "--test-sort-with-comparator\n";
$arr = [3, 1, 2];
var_dump(Flow::of($arr)->sort(function ($o1, $o2) {
    if ($o1 == $o2) {
        return 0;
    }
    return $o1 > $o2 ? -1 : 1;
}));
?>
--EXPECT--
--test-sort
array(6) {
  [0]=>
  string(1) "0"
  [1]=>
  int(1)
  [2]=>
  int(2)
  [3]=>
  int(3)
Example #19
0
--TEST--
Basic cursor test map + reduce
--FILE--
<?php 
use php\util\Flow;
$arr = [1, 2, 3, 4];
echo "--simple-map\n";
var_dump(Flow::of($arr)->map(function ($e) {
    return $e * 20;
})->toArray());
echo "--simple-reduce\n";
var_dump(Flow::of($arr)->reduce(function ($result, $e) {
    return $result + $e;
}));
echo "--simple-map+reduce\n";
var_dump(Flow::of($arr)->map(function ($e) {
    return $e * 20;
})->reduce(function ($result, $e) {
    return $result + $e;
}));
?>
--EXPECT--
--simple-map
array(4) {
  [0]=>
  int(20)
  [1]=>
  int(40)
  [2]=>
  int(60)
  [3]=>
Example #20
0
--TEST--
Basic cursor test limit and offset + count;
--FILE--
<?php 
use php\util\Flow;
$cursor = Flow::ofRange(1, 99);
var_dump('count_with_skip=' . $cursor->skip(19)->count());
$cursor = Flow::ofRange(1, 99);
var_dump('count_with_limit=' . $cursor->limit(20)->count());
$cursor = Flow::ofRange(1, 99);
var_dump('count_with_skip+limit=' . $cursor->skip(20)->limit(40)->count());
echo "--\n";
$cursor = Flow::of([10, 20, 30, 40, 50])->skip(1)->limit(3)->each(function ($el) {
    var_dump($el);
});
?>
--EXPECT--
string(18) "count_with_skip=80"
string(19) "count_with_limit=20"
string(24) "count_with_skip+limit=40"
--
int(20)
int(30)
int(40)