Skip to content

Commit

Permalink
+result->notEmpty(), +notEmpty test
Browse files Browse the repository at this point in the history
  • Loading branch information
rudiedirkx committed Mar 7, 2012
1 parent f46acf3 commit ef0f923
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
20 changes: 19 additions & 1 deletion db_generic.php
Expand Up @@ -533,7 +533,9 @@ abstract static public function make( $db, $result, $options = array() );
public $mappers = array(); // typeof Array<callback>
public $filters = array(); // typeof Array<callback>

public $filtered = array(); // unknown type
public $notEmpty; // bool
public $firstRecord; // unknown type
public $filtered = array(); // Array<unknown type>


public function __construct( $db, $result, $options = array() ) {
Expand Down Expand Up @@ -580,6 +582,16 @@ public function valid() {
}


public function notEmpty() {
if ( null === $this->notEmpty ) {
$this->firstRecord = $this->nextMatchingObject();
$this->notEmpty = (bool)$this->firstRecord;
}

return $this->notEmpty;
}


public function all() {
return iterator_to_array($this);
}
Expand Down Expand Up @@ -609,6 +621,12 @@ public function filter($callback) {

// Helper methods
public function nextMatchingObject() {
if ( $this->firstRecord ) {
$object = $this->firstRecord;
$this->firstRecord = null;
return $object;
}

if ( !$this->mappers && !$this->filters ) {
return $this->nextObject($this->options['args']);
}
Expand Down
23 changes: 23 additions & 0 deletions test/resultCount.php
@@ -0,0 +1,23 @@
<?php

require 'inc.connect.php';

$stuffs = $db->select('stuffs', '(1 OR stuff = ?) ORDER BY id LIMIT 30', array('A'));
print_r($stuffs);

var_dump($stuffs->notEmpty());
var_dump($stuffs->notEmpty());
var_dump($stuffs->notEmpty());
var_dump($stuffs->notEmpty());

print_r(iterator_to_array($stuffs));

foreach ( $stuffs AS $stuff ) {
print_r($stuff);
}

print_r($stuffs);

echo "\n";

echo number_format(microtime(1) - $start, 4) . "\n";

0 comments on commit ef0f923

Please sign in to comment.