Skip to content

Commit c3a828c

Browse files
committed
Added Screenshare type to layout object
1 parent 573dac0 commit c3a828c

File tree

3 files changed

+107
-4
lines changed

3 files changed

+107
-4
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ You can set change the layout dynamically, using the
317317
```php
318318
use OpenTok\OpenTok;
319319

320-
$layout Layout::getPIP(); // Or use another get method of the Layout class.
320+
$layout = Layout::getPIP(); // Or use another get method of the Layout class.
321321
$opentok->updateBroadcastLayout($broadcastId, $layout);
322322
```
323323

@@ -338,6 +338,13 @@ $layoutType = Layout::createCustom($options);
338338
$opentok->setArchiveLayout($archiveId, $layoutType);
339339
```
340340

341+
You can also set the Screenshare Layout by calling the `setScreenshareType()` method on a layout object.
342+
343+
```php
344+
$layout = Layout::getBestFit(); // Other types are not currently supported
345+
$layout->setScreenshareType(Layout::LAYOUT_VERTICAL);
346+
```
347+
341348
You can set the initial layout class for a client's streams by setting the `layout` option when
342349
you create the token for the client, using the `OpenTok->generateToken()` method or the
343350
`Session->generateToken()` method. And you can change the layout classes for a stream

src/OpenTok/Layout.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
class Layout implements \JsonSerializable
2323
{
2424
public const LAYOUT_BESTFIT = 'bestFit';
25+
public const LAYOUT_CUSTOM = 'custom';
26+
public const LAYOUT_HORIZONTAL = 'horizontalPresentation';
2527
public const LAYOUT_PIP = 'pip';
2628
public const LAYOUT_VERTICAL = 'verticalPresentation';
27-
public const LAYOUT_HORIZONTAL = 'horizontalPresentation';
2829

2930
/**
3031
* Type of layout that we are sending
@@ -33,6 +34,13 @@ class Layout implements \JsonSerializable
3334
* */
3435
private $type;
3536

37+
/**
38+
* Type of layout to use for screen sharing
39+
* @var string
40+
* @ignore
41+
*/
42+
private $screenshareType;
43+
3644
/**
3745
* Custom stylesheet if our type is 'custom'
3846
* @var string
@@ -59,14 +67,14 @@ public static function createCustom(array $options): Layout
5967
// NOTE: the default value of stylesheet=null will not pass validation, this essentially
6068
// means that stylesheet is not optional. its still purposely left as part of the
6169
// $options argument so that it can become truly optional in the future.
62-
$defaults = array('stylesheet' => null);
70+
$defaults = ['stylesheet' => null];
6371
$options = array_merge($defaults, array_intersect_key($options, $defaults));
6472
list($stylesheet) = array_values($options);
6573

6674
// validate arguments
6775
Validators::validateLayoutStylesheet($stylesheet);
6876

69-
return new Layout('custom', $stylesheet);
77+
return new Layout(static::LAYOUT_CUSTOM, $stylesheet);
7078
}
7179

7280
/** @ignore */
@@ -111,6 +119,27 @@ public static function getHorizontalPresentation(): Layout
111119
return new Layout(static::LAYOUT_HORIZONTAL);
112120
}
113121

122+
public function setScreenshareType(string $screenshareType): Layout
123+
{
124+
if ($this->type === Layout::LAYOUT_BESTFIT) {
125+
$layouts = [
126+
Layout::LAYOUT_BESTFIT,
127+
Layout::LAYOUT_HORIZONTAL,
128+
Layout::LAYOUT_PIP,
129+
Layout::LAYOUT_VERTICAL
130+
];
131+
132+
if (!in_array($screenshareType, $layouts)) {
133+
throw new \RuntimeException('Screenshare type must be of a valid layout type');
134+
}
135+
136+
$this->screenshareType = $screenshareType;
137+
return $this;
138+
}
139+
140+
throw new \RuntimeException('Screenshare type cannot be set on a layout type other than bestFit');
141+
}
142+
114143
public function jsonSerialize()
115144
{
116145
return $this->toArray();
@@ -134,6 +163,12 @@ public function toArray(): array
134163
if (isset($this->stylesheet)) {
135164
$data['stylesheet'] = $this->stylesheet;
136165
}
166+
167+
// omit 'screenshareType' property unless it is explicitly defined
168+
if (isset($this->screenshareType)) {
169+
$data['screenshareType'] = $this->screenshareType;
170+
}
171+
137172
return $data;
138173
}
139174
}

tests/OpenTokTest/LayoutTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace OpenTokTest;
4+
5+
use OpenTok\Layout;
6+
use PHPStan\Testing\TestCase;
7+
8+
class LayoutTest extends TestCase
9+
{
10+
public function testStylesheetIsNotInSerializedArrayIfNotCustom()
11+
{
12+
$layouts = [
13+
Layout::LAYOUT_BESTFIT => Layout::getBestFit(),
14+
Layout::LAYOUT_HORIZONTAL => Layout::getHorizontalPresentation(),
15+
Layout::LAYOUT_PIP => Layout::getPIP(),
16+
Layout::LAYOUT_VERTICAL => Layout::getVerticalPresentation(),
17+
];
18+
19+
foreach ($layouts as $type => $object) {
20+
$this->assertSame(['type' => $type], $object->toArray());
21+
}
22+
}
23+
24+
public function testStylesheetIsInSerializedArrayIfCustom()
25+
{
26+
$layout = Layout::createCustom(['stylesheet' => 'foo']);
27+
28+
$this->assertSame(['type' => LAYOUT::LAYOUT_CUSTOM, 'stylesheet' => 'foo'], $layout->toArray());
29+
}
30+
31+
public function testScreenshareTypeSerializesProperly()
32+
{
33+
$layout = Layout::getBestFit();
34+
$layout->setScreenshareType(Layout::LAYOUT_PIP);
35+
36+
$expected = [
37+
'type' => 'bestFit',
38+
'screenshareType' => 'pip'
39+
];
40+
41+
$this->assertSame($expected, $layout->toArray());
42+
}
43+
44+
public function testScreenshareTypeCannotBeSetToInvalidValue()
45+
{
46+
$this->expectException(\RuntimeException::class);
47+
$this->expectExceptionMessage('Screenshare type must be of a valid layout type');
48+
49+
$layout = Layout::getBestFit();
50+
$layout->setScreenshareType('bar');
51+
}
52+
53+
public function testScreenshareTypeCannotBeSetOnInvalidLayoutType()
54+
{
55+
$this->expectException(\RuntimeException::class);
56+
$this->expectExceptionMessage('Screenshare type cannot be set on a layout type other than bestFit');
57+
58+
$layout = Layout::createCustom(['stylesheet' => 'foo']);
59+
$layout->setScreenshareType(Layout::LAYOUT_PIP);
60+
}
61+
}

0 commit comments

Comments
 (0)