Skip to content

Commit

Permalink
Merge pull request #12 from microsoft/dev
Browse files Browse the repository at this point in the history
Release 1.1.1
  • Loading branch information
SilasKenneth authored May 30, 2024
2 parents c813f8a + caa82a4 commit e6de40f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/FormParseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,24 @@ public function getChildNode(string $identifier): ?FormParseNode
*/
public function getStringValue(): ?string
{
return !$this->isNull()
? urldecode(addcslashes(strval($this->node), "\\\t\r\n"))
return is_string($this->node) && !$this->isNull()
? urldecode(addcslashes($this->node, "\\\t\r\n"))
: null;
}
/**
* @inheritDoc
*/
public function getBooleanValue(): ?bool
{
return !$this->isNull() ? (bool)$this->node : null;
return (!$this->isNull() && filter_var($this->node, FILTER_VALIDATE_BOOLEAN)) ? boolval($this->node) : null;
}

/**
* @inheritDoc
*/
public function getIntegerValue(): ?int
{
return !$this->isNull() ? intval($this->node) : null;
return !$this->isNull() && filter_var($this->node, FILTER_VALIDATE_INT, FILTER_FLAG_NONE) ? intval($this->node) : null;
}

/**
Expand Down Expand Up @@ -221,7 +221,7 @@ public function getAnyValue(string $type) {
*/
public function getDateTimeValue(): ?DateTime
{
return $this->node !== null ? new DateTime(strval($this->node)) : null;
return is_string($this->node) ? new DateTime($this->node) : null;
}

/**
Expand Down
12 changes: 8 additions & 4 deletions tests/FormParseNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ class FormParseNodeTest extends TestCase
private StreamInterface $stream;

protected function setUp(): void {
$this->stream = Utils::streamFor('@odata.type=Missing&name=Silas+Kenneth&age=98&height=123.122&maritalStatus=complicated,single&type=html&type=json&type=plain');
$this->stream = Utils::streamFor('@odata.type=Missing&name=Silas+Kenneth&age=98&height=123.122&maritalStatus=complicated,single&type=html&type=json&type=plain&seen=true');
}

public function testGetIntegerValue(): void {
$this->parseNode = new FormParseNode('1243.78');
$this->parseNode = new FormParseNode(1243.78);
$expected = $this->parseNode->getIntegerValue();
$this->assertEquals(null, $expected);
$this->parseNode = new FormParseNode(1243);
$expected = $this->parseNode->getIntegerValue();
$this->assertEquals(1243, $expected);
}
Expand All @@ -54,11 +57,12 @@ public function testGetObjectValue(): void {
$this->assertInstanceOf(Enum::class, $expected->getMaritalStatus());
$this->assertEquals(98, $expected->getAge());
$this->assertEquals(123.122, $expected->getHeight());
$this->assertEquals(true, $expected->getSeen());
$this->assertEquals([new BioContentType('html'), new BioContentType('json'), new BioContentType('plain')], $expected->getBioContentType());
}

public function testGetFloatValue(): void {
$this->parseNode = new FormParseNode('1243.12');
$this->parseNode = new FormParseNode(1243.12);
$expected = $this->parseNode->getFloatValue();
$this->assertEquals(1243.12, $expected);
}
Expand Down Expand Up @@ -124,7 +128,7 @@ public function testGetDateOnlyValue(): void{
}

public function testGetBooleanValue(): void {
$this->parseNode = new FormParseNode(true);
$this->parseNode = new FormParseNode('true');
$expected = $this->parseNode->getBooleanValue();
$this->assertEquals('bool', get_debug_type($expected));
$this->assertEquals(true, $expected);
Expand Down
22 changes: 21 additions & 1 deletion tests/Samples/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Person implements Parsable, AdditionalDataHolder

/** @var BioContentType[]|null */
private ?array $bioContentType = null;

private ?bool $seen = null;
/**
* @inheritDoc
*/
Expand All @@ -33,10 +35,27 @@ public function getFieldDeserializers(): array
"height" => function (ParseNode $n) use ($currentObject) {$currentObject->setHeight($n->getFloatValue());},
"maritalStatus" => function (ParseNode $n) use ($currentObject) {$currentObject->setMaritalStatus($n->getEnumValue(MaritalStatus::class));},
"address" => function (ParseNode $n) use ($currentObject) {$currentObject->setAddress($n->getObjectValue(array(Address::class, 'createFromDiscriminatorValue')));},
"type" => function (ParseNode $n) use ($currentObject) {$currentObject->setBioContentType($n->getCollectionOfEnumValues(BioContentType::class));}
"type" => function (ParseNode $n) use ($currentObject) {$currentObject->setBioContentType($n->getCollectionOfEnumValues(BioContentType::class));},
"seen" => function (ParseNode $n) use ($currentObject) {$currentObject->setSeen($n->getBooleanValue());}
];
}

/**
* @param bool|null $seen
*/
public function setSeen(?bool $seen): void
{
$this->seen = $seen;
}

/**
* @return bool|null
*/
public function getSeen(): ?bool
{
return $this->seen;
}

/**
* @inheritDoc
*/
Expand All @@ -45,6 +64,7 @@ public function serialize(SerializationWriter $writer): void {
$writer->writeIntegerValue('age', $this->age);
$writer->writeEnumValue('maritalStatus', $this->maritalStatus);
$writer->writeFloatValue('height', $this->height);
$writer->writeBooleanValue('seen', $this->seen);
$writer->writeCollectionOfEnumValues('type', $this->bioContentType);
$writer->writeAdditionalData($this->additionalData);
}
Expand Down

0 comments on commit e6de40f

Please sign in to comment.