Skip to content

Commit

Permalink
Make compatible with PHP 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Oct 21, 2021
1 parent c0b3f57 commit b3ffb72
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 48 deletions.
9 changes: 8 additions & 1 deletion src/main/php/text/csv/Lines.class.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php namespace text\csv;

use ReturnTypeWillChange, Iterator;

/**
* Lines in a CSV file
*
* @test xp://text.csv.unittest.LinesTest
*/
class Lines implements \Iterator {
class Lines implements Iterator {
const EOF = null;
private $reader, $line, $number;

Expand All @@ -19,15 +21,19 @@ public function __construct(CsvReader $reader) {
}

/** @return var */
#[ReturnTypeWillChange]
public function current() { return $this->line; }

/** @return int */
#[ReturnTypeWillChange]
public function key() { return $this->number; }

/** @return bool */
#[ReturnTypeWillChange]
public function valid() { return self::EOF !== $this->number; }

/** @return void */
#[ReturnTypeWillChange]
public function next() {
if (self::EOF === $this->number) {
// Already at EOF, don't attempt further reads
Expand All @@ -39,6 +45,7 @@ public function next() {
}

/** @return void */
#[ReturnTypeWillChange]
public function rewind() {
$this->number= -1;
$this->next();
Expand Down
4 changes: 2 additions & 2 deletions src/main/php/text/csv/processors/FormatNumber.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public function withFormat($decimals= 2, $decimalPoint= '.', $thousandsSeparator
* @throws lang.FormatException
*/
public function process($in) {
if (!(null === $in || is_numeric($in))) {
if (null !== $in && !is_numeric($in)) {
throw new FormatException('Cannot format non-number '.Objects::stringOf($in));
}
return $this->proceed(number_format($in, $this->decimals, $this->decimalPoint, $this->thousandsSeparator));
return $this->proceed(number_format((float)$in, $this->decimals, $this->decimalPoint, $this->thousandsSeparator));
}
}
36 changes: 18 additions & 18 deletions src/test/php/text/csv/unittest/CellProcessorTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function formatDate() {
null
]);
$writer->write([new Date('2009-09-09 15:45'), 'Order placed']);
$this->assertEquals("2009-09-09 15:45;Order placed\n", $this->out->getBytes());
$this->assertEquals("2009-09-09 15:45;Order placed\n", $this->out->bytes());
}

#[Test, Expect(FormatException::class)]
Expand All @@ -165,7 +165,7 @@ public function formatNullWithDefault() {
null
]);
$writer->write([null, 'Order placed']);
$this->assertEquals($now->toString('Y-m-d H:i').";Order placed\n", $this->out->getBytes());
$this->assertEquals($now->toString('Y-m-d H:i').";Order placed\n", $this->out->bytes());
}

#[Test]
Expand Down Expand Up @@ -237,7 +237,7 @@ public function formatTrue() {
new FormatBool()
]);
$writer->write(['A', true]);
$this->assertEquals("A;true\n", $this->out->getBytes());
$this->assertEquals("A;true\n", $this->out->bytes());
}

#[Test]
Expand All @@ -247,7 +247,7 @@ public function formatTrueAsY() {
new FormatBool('Y', 'N')
]);
$writer->write(['A', true]);
$this->assertEquals("A;Y\n", $this->out->getBytes());
$this->assertEquals("A;Y\n", $this->out->bytes());
}

#[Test]
Expand All @@ -257,7 +257,7 @@ public function formatFalse() {
new FormatBool()
]);
$writer->write(['A', false]);
$this->assertEquals("A;false\n", $this->out->getBytes());
$this->assertEquals("A;false\n", $this->out->bytes());
}

#[Test]
Expand All @@ -267,7 +267,7 @@ public function formatFalseAsN() {
new FormatBool('Y', 'N')
]);
$writer->write(['A', false]);
$this->assertEquals("A;N\n", $this->out->getBytes());
$this->assertEquals("A;N\n", $this->out->bytes());
}

#[Test]
Expand Down Expand Up @@ -306,7 +306,7 @@ public function formatEnumValue() {
new FormatEnum()
]);
$writer->write(['200', Coin::$penny]);
$this->assertEquals("200;penny\n", $this->out->getBytes());
$this->assertEquals("200;penny\n", $this->out->bytes());
}

#[Test, Expect(FormatException::class)]
Expand All @@ -324,7 +324,7 @@ public function formatNumber() {
(new FormatNumber())->withFormat(2, ',', "'")
]);
$writer->write([3.75, 10000000.5]);
$this->assertEquals("3.75000;10'000'000,50\n", $this->out->getBytes());
$this->assertEquals("3.75000;10'000'000,50\n", $this->out->bytes());
}

#[Test]
Expand All @@ -333,7 +333,7 @@ public function formatNumberNull() {
(new FormatNumber())->withFormat(2, '.')
]);
$writer->write([null]);
$this->assertEquals("0.00\n", $this->out->getBytes());
$this->assertEquals("0.00\n", $this->out->bytes());
}

#[Test, Expect(FormatException::class)]
Expand Down Expand Up @@ -376,7 +376,7 @@ public function writeOptionalString() {
new Optional(),
null
])->write(['A', 'Test']);
$this->assertEquals("A;Test\n", $this->out->getBytes());
$this->assertEquals("A;Test\n", $this->out->bytes());
}

#[Test]
Expand All @@ -385,7 +385,7 @@ public function writeOptionalEmpty() {
new Optional(),
null
])->write(['', 'Test']);
$this->assertEquals(";Test\n", $this->out->getBytes());
$this->assertEquals(";Test\n", $this->out->bytes());
}

#[Test]
Expand All @@ -394,7 +394,7 @@ public function writeOptionalNull() {
new Optional(),
null
])->write([null, 'Test']);
$this->assertEquals(";Test\n", $this->out->getBytes());
$this->assertEquals(";Test\n", $this->out->bytes());
}

#[Test]
Expand All @@ -403,7 +403,7 @@ public function writeOptionalWithDefault() {
(new Optional())->withDefault('(unknown)'),
null
])->write(['', 'Test']);
$this->assertEquals("(unknown);Test\n", $this->out->getBytes());
$this->assertEquals("(unknown);Test\n", $this->out->bytes());
}

#[Test]
Expand All @@ -412,7 +412,7 @@ public function writeOptionalNullWithDefault() {
(new Optional())->withDefault('(unknown)'),
null
])->write([null, 'Test']);
$this->assertEquals("(unknown);Test\n", $this->out->getBytes());
$this->assertEquals("(unknown);Test\n", $this->out->bytes());
}

#[Test]
Expand All @@ -438,7 +438,7 @@ public function writeRequired() {
new Required(),
null
])->write(['A', 'B']);
$this->assertEquals("A;B\n", $this->out->getBytes());
$this->assertEquals("A;B\n", $this->out->bytes());
}

#[Test, Expect(FormatException::class)]
Expand Down Expand Up @@ -493,7 +493,7 @@ public function writeUnique() {
$this->fail('Duplicate value not detected', null, 'lang.FormatException');
} catch (FormatException $expected) { }

$this->assertEquals("200;OK\n", $this->out->getBytes());
$this->assertEquals("200;OK\n", $this->out->bytes());
}

/**
Expand Down Expand Up @@ -558,7 +558,7 @@ public function processorExceptionsDoNotBreakWriting() {
} catch (FormatException $expected) { }

$writer->write(['404', 'Not found']);
$this->assertEquals("404;Not found\n", $this->out->getBytes());
$this->assertEquals("404;Not found\n", $this->out->bytes());
}

#[Test]
Expand All @@ -574,6 +574,6 @@ public function processorExceptionsDoNotCausePartialWriting() {
} catch (FormatException $expected) { }

$writer->write(['404', 'Not found']);
$this->assertEquals("404;Not found\n", $this->out->getBytes());
$this->assertEquals("404;Not found\n", $this->out->bytes());
}
}
6 changes: 3 additions & 3 deletions src/test/php/text/csv/unittest/CsvBeanWriterTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ protected function newWriter(CsvFormat $format= null) {
#[Test]
public function writePerson() {
$this->newWriter()->write(new Person(1549, 'Timm', 'friebe@example.com'));
$this->assertEquals("1549;Timm;friebe@example.com\n", $this->out->getBytes());
$this->assertEquals("1549;Timm;friebe@example.com\n", $this->out->bytes());
}

#[Test]
public function writePersonReSorted() {
$this->newWriter()->write(new Person(1549, 'Timm', 'friebe@example.com'), ['email', 'id', 'name']);
$this->assertEquals("friebe@example.com;1549;Timm\n", $this->out->getBytes());
$this->assertEquals("friebe@example.com;1549;Timm\n", $this->out->bytes());
}

#[Test]
public function writePersonPartially() {
$this->newWriter()->write(new Person(1549, 'Timm', 'friebe@example.com'), ['id', 'name']);
$this->assertEquals("1549;Timm\n", $this->out->getBytes());
$this->assertEquals("1549;Timm\n", $this->out->bytes());
}
}
26 changes: 13 additions & 13 deletions src/test/php/text/csv/unittest/CsvListWriterTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ protected function newWriter(CsvFormat $format= null) {
#[Test]
public function writeLine() {
$this->newWriter()->write(['Timm', 'Karlsruhe', '76137']);
$this->assertEquals("Timm;Karlsruhe;76137\n", $this->out->getBytes());
$this->assertEquals("Timm;Karlsruhe;76137\n", $this->out->bytes());
}

#[Test]
public function writeEmptyValue() {
$this->newWriter()->write(['Timm', '', '76137']);
$this->assertEquals("Timm;;76137\n", $this->out->getBytes());
$this->assertEquals("Timm;;76137\n", $this->out->bytes());
}

#[Test]
public function setHeaders() {
$writer= $this->newWriter();
$writer->setHeaders(['name', 'city', 'zip']);
$writer->write(['Timm', 'Karlsruhe', '76137']);
$this->assertEquals("name;city;zip\nTimm;Karlsruhe;76137\n", $this->out->getBytes());
$this->assertEquals("name;city;zip\nTimm;Karlsruhe;76137\n", $this->out->bytes());
}

#[Test, Expect(IllegalStateException::class)]
Expand All @@ -55,62 +55,62 @@ public function cannotSetHeadersAfterWriting() {
#[Test]
public function writeUnixMultiLineValue() {
$this->newWriter((new CsvFormat())->withQuote("'"))->write(['Timm', "76137\nKarlsruhe\nGermany"]);
$this->assertEquals("Timm;'76137\nKarlsruhe\nGermany'\n", $this->out->getBytes());
$this->assertEquals("Timm;'76137\nKarlsruhe\nGermany'\n", $this->out->bytes());
}

#[Test]
public function writeMacMultiLineValue() {
$this->newWriter((new CsvFormat())->withQuote("'"))->write(['Timm', "76137\rKarlsruhe\rGermany"]);
$this->assertEquals("Timm;'76137\rKarlsruhe\rGermany'\n", $this->out->getBytes());
$this->assertEquals("Timm;'76137\rKarlsruhe\rGermany'\n", $this->out->bytes());
}

#[Test]
public function writeWindowsMultiLineValue() {
$this->newWriter((new CsvFormat())->withQuote("'"))->write(['Timm', "76137\r\nKarlsruhe\r\nGermany"]);
$this->assertEquals("Timm;'76137\r\nKarlsruhe\r\nGermany'\n", $this->out->getBytes());
$this->assertEquals("Timm;'76137\r\nKarlsruhe\r\nGermany'\n", $this->out->bytes());
}

#[Test]
public function valueWithDelimiterIsQuoted() {
$this->newWriter((new CsvFormat())->withQuote("'"))->write(['Timm;Friebe', 'Karlsruhe', '76137']);
$this->assertEquals("'Timm;Friebe';Karlsruhe;76137\n", $this->out->getBytes());
$this->assertEquals("'Timm;Friebe';Karlsruhe;76137\n", $this->out->bytes());
}

#[Test]
public function delimiterIsQuoted() {
$this->newWriter((new CsvFormat())->withQuote("'"))->write([';', 'Karlsruhe', '76137']);
$this->assertEquals("';';Karlsruhe;76137\n", $this->out->getBytes());
$this->assertEquals("';';Karlsruhe;76137\n", $this->out->bytes());
}

#[Test]
public function quotesAroundValueAreEscaped() {
$this->newWriter((new CsvFormat())->withQuote("'"))->write(["'Hello'", 'Karlsruhe', '76137']);
$this->assertEquals("'''Hello''';Karlsruhe;76137\n", $this->out->getBytes());
$this->assertEquals("'''Hello''';Karlsruhe;76137\n", $this->out->bytes());
}

#[Test]
public function quotesInsideValueAreEscaped() {
$this->newWriter((new CsvFormat())->withQuote("'"))->write(["He said 'Hello' to me", 'Karlsruhe', '76137']);
$this->assertEquals("'He said ''Hello'' to me';Karlsruhe;76137\n", $this->out->getBytes());
$this->assertEquals("'He said ''Hello'' to me';Karlsruhe;76137\n", $this->out->bytes());
}

#[Test]
public function quotesAroundEmptyAreEscaped() {
$this->newWriter((new CsvFormat())->withQuote("'"))->write(["''", 'Karlsruhe', '76137']);
$this->assertEquals("'''''';Karlsruhe;76137\n", $this->out->getBytes());
$this->assertEquals("'''''';Karlsruhe;76137\n", $this->out->bytes());
}

#[Test]
public function writeLineFromMap() {
$this->newWriter()->write(['name' => 'Timm', 'city' => 'Karlsruhe', 'zip' => '76137']);
$this->assertEquals("Timm;Karlsruhe;76137\n", $this->out->getBytes());
$this->assertEquals("Timm;Karlsruhe;76137\n", $this->out->bytes());
}

#[Test]
public function writeLineFromMapWithProcessor() {
$writer= $this->newWriter();
$writer->setProcessor(1, new FormatDate('d.m.Y'));
$writer->write(['id' => 1, 'date' => new \util\Date('2012-02-10')]);
$this->assertEquals("1;10.02.2012\n", $this->out->getBytes());
$this->assertEquals("1;10.02.2012\n", $this->out->bytes());
}
}
12 changes: 6 additions & 6 deletions src/test/php/text/csv/unittest/CsvMapWriterTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ protected function newWriter(\text\csv\CsvFormat $format= null) {
#[Test]
public function writeRecord() {
$this->newWriter()->write(['id' => 1549, 'name' => 'Timm', 'email' => 'friebe@example.com']);
$this->assertEquals("1549;Timm;friebe@example.com\n", $this->out->getBytes());
$this->assertEquals("1549;Timm;friebe@example.com\n", $this->out->bytes());
}

#[Test]
public function writeRecordWithHeaders() {
$out= $this->newWriter();
$out->setHeaders(['id', 'name', 'email']);
$out->write(['id' => 1549, 'name' => 'Timm', 'email' => 'friebe@example.com']);
$this->assertEquals("id;name;email\n1549;Timm;friebe@example.com\n", $this->out->getBytes());
$this->assertEquals("id;name;email\n1549;Timm;friebe@example.com\n", $this->out->bytes());
}

#[Test]
public function writeUnorderedRecordWithHeaders() {
$out= $this->newWriter();
$out->setHeaders(['id', 'name', 'email']);
$out->write(['email' => 'friebe@example.com', 'name' => 'Timm', 'id' => 1549]);
$this->assertEquals("id;name;email\n1549;Timm;friebe@example.com\n", $this->out->getBytes());
$this->assertEquals("id;name;email\n1549;Timm;friebe@example.com\n", $this->out->bytes());
}


Expand All @@ -51,22 +51,22 @@ public function writeIncompleteRecordWithHeaders() {
$out= $this->newWriter();
$out->setHeaders(['id', 'name', 'email']);
$out->write(['id' => 1549, 'email' => 'friebe@example.com']);
$this->assertEquals("id;name;email\n1549;;friebe@example.com\n", $this->out->getBytes());
$this->assertEquals("id;name;email\n1549;;friebe@example.com\n", $this->out->bytes());
}

#[Test]
public function writeEmptyRecordWithHeaders() {
$out= $this->newWriter();
$out->setHeaders(['id', 'name', 'email']);
$out->write([]);
$this->assertEquals("id;name;email\n;;\n", $this->out->getBytes());
$this->assertEquals("id;name;email\n;;\n", $this->out->bytes());
}

#[Test]
public function writeRecordWithExtraDataWithHeaders() {
$out= $this->newWriter();
$out->setHeaders(['id', 'name', 'email']);
$out->write(['id' => 1549, 'name' => 'Timm', 'email' => 'friebe@example.com', 'extra' => 'WILL_NOT_APPEAR']);
$this->assertEquals("id;name;email\n1549;Timm;friebe@example.com\n", $this->out->getBytes());
$this->assertEquals("id;name;email\n1549;Timm;friebe@example.com\n", $this->out->bytes());
}
}
Loading

0 comments on commit b3ffb72

Please sign in to comment.