1919 * <a href="https://tokbox.com/developer/guides/broadcast/live-streaming/#configuring-video-layout-for-opentok-live-streaming-broadcasts">Configuring
2020 * video layout for OpenTok live streaming broadcasts</a>.
2121 */
22- class Layout
22+ class Layout implements \JsonSerializable
2323{
24- // NOTE: after PHP 5.3.0 support is dropped, the class can implement JsonSerializable
25-
26- /** @ignore */
27- private static $ bestFit ;
28- /** @ignore */
29- private static $ pip ;
30- /** @ignore */
31- private static $ verticalPresentation ;
32- /** @ignore */
33- private static $ horizontalPresentation ;
34-
35- /**
36- * Returns a Layout object defining the "best fit" predefined layout type.
37- */
38- public static function getBestFit ()
39- {
40- if (is_null (self ::$ bestFit )) {
41- self ::$ bestFit = new Layout ('bestFit ' );
42- }
43- return self ::$ bestFit ;
44- }
24+ public const LAYOUT_BESTFIT = 'bestFit ' ;
25+ public const LAYOUT_PIP = 'pip ' ;
26+ public const LAYOUT_VERTICAL = 'verticalPresentation ' ;
27+ public const LAYOUT_HORIZONTAL = 'horizontalPresentation ' ;
4528
4629 /**
47- * Returns a Layout object defining the "picture-in-picture" predefined layout type.
48- */
49- public static function getPIP ()
50- {
51- if (is_null (self ::$ pip )) {
52- self ::$ pip = new Layout ('pip ' );
53- }
54- return self ::$ pip ;
55- }
30+ * Type of layout that we are sending
31+ * @var string
32+ * @ignore
33+ * */
34+ private $ type ;
5635
5736 /**
58- * Returns a Layout object defining the "vertical presentation" predefined layout type.
37+ * Custom stylesheet if our type is 'custom'
38+ * @var string
39+ * @ignore
5940 */
60- public static function getVerticalPresentation ()
61- {
62- if (is_null (self ::$ verticalPresentation )) {
63- self ::$ verticalPresentation = new Layout ('verticalPresentation ' );
64- }
65- return self ::$ verticalPresentation ;
66- }
41+ private $ stylesheet ;
6742
68- /**
69- * Returns a Layout object defining the "horizontal presentation" predefined layout type.
70- */
71- public static function getHorizontalPresentation ()
43+ /** @ignore */
44+ private function __construct (string $ type , ?string $ stylesheet = null )
7245 {
73- if (is_null (self ::$ horizontalPresentation )) {
74- self ::$ horizontalPresentation = new Layout ('horizontalPresentation ' );
75- }
76- return self ::$ horizontalPresentation ;
46+ $ this ->type = $ type ;
47+ $ this ->stylesheet = $ stylesheet ;
7748 }
7849
7950 /**
@@ -82,7 +53,7 @@ public static function getHorizontalPresentation()
8253 * @param array $options An array containing one property: <code>$stylesheet<code>,
8354 * which is a string containing the stylesheet to be used for the layout.
8455 */
85- public static function createCustom ($ options )
56+ public static function createCustom (array $ options ): Layout
8657 {
8758 // unpack optional arguments (merging with default values) into named variables
8859 // NOTE: the default value of stylesheet=null will not pass validation, this essentially
@@ -99,28 +70,61 @@ public static function createCustom($options)
9970 }
10071
10172 /** @ignore */
102- public static function fromData ($ layoutData )
73+ public static function fromData (array $ layoutData ): Layout
10374 {
10475 if (array_key_exists ('stylesheet ' , $ layoutData )) {
10576 return new Layout ($ layoutData ['type ' ], $ layoutData ['stylesheet ' ]);
106- } else {
107- return new Layout ($ layoutData ['type ' ]);
10877 }
78+
79+ return new Layout ($ layoutData ['type ' ]);
10980 }
11081
111- /** @ignore */
112- private $ type ;
113- /** @ignore */
114- private $ stylesheet ;
82+ /**
83+ * Returns a Layout object defining the "best fit" predefined layout type.
84+ */
85+ public static function getBestFit (): Layout
86+ {
87+ return new Layout (static ::LAYOUT_BESTFIT );
88+ }
11589
116- /** @ignore */
117- private function __construct ($ type , $ stylesheet = null )
90+ /**
91+ * Returns a Layout object defining the "picture-in-picture" predefined layout type.
92+ */
93+ public static function getPIP (): Layout
11894 {
119- $ this ->type = $ type ;
120- $ this ->stylesheet = $ stylesheet ;
95+ return new Layout (static ::LAYOUT_PIP );
96+ }
97+
98+ /**
99+ * Returns a Layout object defining the "vertical presentation" predefined layout type.
100+ */
101+ public static function getVerticalPresentation (): Layout
102+ {
103+ return new Layout (static ::LAYOUT_VERTICAL );
104+ }
105+
106+ /**
107+ * Returns a Layout object defining the "horizontal presentation" predefined layout type.
108+ */
109+ public static function getHorizontalPresentation (): Layout
110+ {
111+ return new Layout (static ::LAYOUT_HORIZONTAL );
121112 }
122113
123114 public function jsonSerialize ()
115+ {
116+ return $ this ->toArray ();
117+ }
118+
119+ /**
120+ * Return a json-encoded string representation of the layout
121+ */
122+ public function toJson (): string
123+ {
124+ return json_encode ($ this ->jsonSerialize ());
125+ }
126+
127+ public function toArray (): array
124128 {
125129 $ data = array (
126130 'type ' => $ this ->type
@@ -132,9 +136,4 @@ public function jsonSerialize()
132136 }
133137 return $ data ;
134138 }
135-
136- public function toJson ()
137- {
138- return json_encode ($ this ->jsonSerialize ());
139- }
140139}
0 commit comments