Skip to content

Commit

Permalink
Merge pull request #9 from netglue/pr/8
Browse files Browse the repository at this point in the history
Add Boolean Fragment Type
  • Loading branch information
gsteel authored Mar 12, 2020
2 parents 8d4ca83 + 4ecc268 commit 68888a2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Prismic/Document/Fragment/Boolean.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace Prismic\Document\Fragment;

class Boolean extends AbstractScalarFragment
{
public function asBoolean() : bool
{
return (bool) $this->value;
}
}
7 changes: 7 additions & 0 deletions src/Prismic/Document/Fragment/FragmentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ private function v1Factory(string $key, $value, LinkResolver $linkResolver) : vo
case 'SliceZone':
$fragment = Group::factory($value, $linkResolver);
break;
case 'Boolean':
$fragment = Boolean::factory($value);
break;
}

$this->fragments[$key] = $fragment;
Expand All @@ -108,6 +111,10 @@ private function v2Factory(string $key, $value, LinkResolver $linkResolver) : vo
$this->fragments[$key] = Image::factory($value, $linkResolver);
return;
}
if (is_bool($value)) {
$this->fragments[$key] = Boolean::factory($value);
return;
}
if (is_float($value)) {
$this->fragments[$key] = Number::factory($value);
return;
Expand Down
30 changes: 30 additions & 0 deletions tests/Prismic/Document/Fragment/BooleanTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

namespace Prismic\Test\Document\Fragment;

use Prismic\Document\Fragment\Boolean;
use PHPUnit\Framework\TestCase;
use function json_decode;

class BooleanTest extends TestCase
{
public function possibleFragmentValues() : iterable
{
return [
'V1 Style, True' => ['{"type": "Boolean","value": true}', true],
'V1 Style, False' => ['{"type": "Boolean","value": false}', false],
'V2 Style, True' => ['true', true],
'V2 Style, False' => ['false', false],
];
}

/** @dataProvider possibleFragmentValues */
public function testFactoryWithV1FragmentPayload(string $json, bool $expect) : void
{
$data = json_decode($json, false);
$fragment = Boolean::factory($data);
$this->assertInstanceOf(Boolean::class, $fragment);
$this->assertSame($expect, $fragment->asBoolean());
}
}
4 changes: 4 additions & 0 deletions tests/fixtures/search-results-v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,10 @@
}
}
]
},
"bool_type" : {
"type": "Boolean",
"value": true
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/search-results.json
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,8 @@
}
}
}
]
],
"bool_type" : true
}
}
],
Expand Down

0 comments on commit 68888a2

Please sign in to comment.