| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * This file is part of PHP-FFmpeg. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * (c) Alchemy <info@alchemy.fr> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * For the full copyright and license information, please view the LICENSE | 
					
						
							|  |  |  |  * file that was distributed with this source code. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FFMpeg\FFProbe\DataMapping; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class StreamCollection implements \Countable, \IteratorAggregate | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     private $streams; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function __construct(array $streams = array()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->streams = array_values($streams); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns the first stream of the collection, null if the collection is | 
					
						
							|  |  |  |      * empty. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return null|Stream | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function first() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $stream = reset($this->streams); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $stream ?: null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2013-06-25 10:40:20 +02:00
										 |  |  |      * Adds a stream to the collection. | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param Stream $stream | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return StreamCollection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function add(Stream $stream) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->streams[] = $stream; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2013-06-25 10:40:20 +02:00
										 |  |  |      * Returns a new StreamCollection with only video streams. | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return StreamCollection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function videos() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new static(array_filter($this->streams, function (Stream $stream) { | 
					
						
							|  |  |  |             return $stream->isVideo(); | 
					
						
							|  |  |  |         })); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2013-06-25 10:40:20 +02:00
										 |  |  |      * Returns a new StreamCollection with only audio streams. | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return StreamCollection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function audios() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new static(array_filter($this->streams, function (Stream $stream) { | 
					
						
							|  |  |  |             return $stream->isAudio(); | 
					
						
							|  |  |  |         })); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * {@inheritdoc} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function count() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return count($this->streams); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2013-06-25 10:40:20 +02:00
										 |  |  |      * Returns the array of contained streams. | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function all() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->streams; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * {@inheritdoc} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getIterator() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new \ArrayIterator($this->streams); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |