/** * toCsv * */ public static function toCsv(Ginq $ginq, $options = array()) { $options = array_merge(self::$options, $options); $encoding = $options['csvEncoding']; $d = $options['delimiter']; $e = $options['enclosure']; $nc = $options['newlineChar']; $fe = $options['forceEnclose']; if ($options['forceOutput']) { $fp = fopen('php://output', 'w'); foreach ($ginq->getIterator() as $line) { $line = array_map(function ($v) use($e, $d, $fe, $nc) { $v = preg_replace('/' . $e . '/', $e . $e, $v); if ($fe || preg_match('/[' . $e . $d . '\\r\\n]/', $v)) { return $e . $v . $e; } return $v; }, $line); if ($encoding !== 'UTF-8') { fwrite($fp, mb_convert_encoding(implode($d, $line) . $nc, $encoding)); } else { fwrite($fp, implode($d, $line) . $nc); } } fclose($fp); } else { $out = ''; foreach ($ginq->getIterator() as $line) { $line = array_map(function ($v) use($e, $d, $fe, $nc) { $v = preg_replace('/' . $e . '/', $e . $e, $v); if ($fe || preg_match('/[' . $e . $d . '\\r\\n]/', $v)) { return $e . $v . $e; } return $v; }, $line); $out .= implode($d, $line) . $nc; } if ($encoding !== 'UTF-8') { return mb_convert_encoding($out, $encoding); } return $out; } }
/** * testConstant(). */ public function testConstant() { $f = Ginq::constant(99); $this->assertEquals(99, $f()); $created = false; $g = Ginq::constant(function () use(&$created) { $created = true; return 999; }); $this->assertEquals(false, $created); $this->assertEquals(999, $g()); $this->assertEquals(true, $created); }
/** * @param \Iterator $it * @param mixed $key * @internal param \Ginq\Core\Comparer $comparer */ public function __construct($it, $key) { $this->key = $key; parent::__construct($it); }