ktlacaelel / php-csv-parser

Automatically exported from code.google.com/p/php-csv-parser
0 stars 0 forks source link

"appendRow" fails when there is no rows #6

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. load a csv file that just has header row
2. try appendRow with the corresponding # of row count in the parameter array
3. fails

What is the expected output? What do you see instead?
true

What version of the product are you using? On what operating system?
0.2.4
windows xp

Please provide any additional information below.
it returns true when you just pass an integer or a string

I fixed it like this
public function isSymmetric()
{
    $hc = count($this->headers);
    foreach ($this->rows as $data) {
        if(!count($data)){  //stops when there is no rows to begin with
            continue;
        }
        if (count($data) != $hc) {
            return false;
        }
    }
    return true;
}

Original issue reported on code.google.com by oop.co...@gmail.com on 19 Feb 2009 at 10:43

GoogleCodeExporter commented 9 years ago
Thanks, oop.coder.
Yes, indeed your right!
I did write some new tests just now and saw your code.
I opted for a different solution. but thanks for submitting the bug anyway.

I changed the simmetrize method, then thing is:
it simetrizes to the longest row, but if the row is empty then it will
do nothing.

so I made it default to the header length when all the rows return zero for 
length.
this, solves the problem and passes all previous tests together with three new 
ones
that check for array, string, and integers returning a valid appended row to the
collection!

you should see this changes in the next release 2.6
Thanks for contributing!

{{{
    public function symmetrize($value = '')
    {
        $max_length = 0;

        foreach ($this->rows as $row) {
            $row_length = count($row);
            if ($max_length < $row_length) {
                $max_length = $row_length;
            }
        }

        if ($max_length == 0) {
            $max_length = count($this->headers);
        }

        foreach ($this->rows as $key => $row) {
            $this->rows[$key] = array_pad($row, $max_length, $value);
        }

        $this->headers = array_pad($this->headers, $max_length, $value);
    }

}}}

Original comment by kazu....@gmail.com on 3 Mar 2009 at 9:41

GoogleCodeExporter commented 9 years ago
nice work.
I didn't even think about your symmetrize method.

I like your library, and I am waiting for your next version with 
- export (gets altered data as a csv string) 
implemented.

If it had a way to change the encoding of the data, it'd be better too.

Original comment by oop.co...@gmail.com on 4 Mar 2009 at 9:22