ffmpeg-mappable-media/src/FFMpeg/FFProbe/DataMapping/AbstractData.php

100 lines
2 KiB
PHP
Raw Normal View History

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;
use FFMpeg\Exception\InvalidArgumentException;
abstract class AbstractData implements \Countable
{
private $properties;
public function __construct(array $properties)
{
$this->properties = $properties;
}
/**
2013-06-25 10:40:20 +02:00
* Returns true if data has property.
2013-06-25 10:03:20 +02:00
*
* @param string $property
* @return Boolean
*/
public function has($property)
{
return isset($this->properties[$property]);
}
/**
2013-06-25 10:40:20 +02:00
* Returns the property value given its name.
2013-06-25 10:03:20 +02:00
*
* @param string $property
* @param mixed $default [optional]
2013-06-25 10:03:20 +02:00
* @return mixed
*
* @throws InvalidArgumentException In case the data does not have the property
*/
public function get($property, $default = null)
2013-06-25 10:03:20 +02:00
{
if (!isset($this->properties[$property])) {
if(!is_null($default)){
return $default;
}
2013-06-25 10:03:20 +02:00
throw new InvalidArgumentException(sprintf('Invalid property `%s`.', $property));
}
return $this->properties[$property];
}
/**
* Sets the property value given its name.
*
2013-12-17 17:39:24 +01:00
* @param string $property
* @param mixed $value
*
* @return AbstractData
*/
public function set($property, $value)
{
$this->properties[$property] = $value;
return $this;
}
2013-06-25 10:03:20 +02:00
/**
2013-06-25 10:40:20 +02:00
* Returns all property names.
2013-06-25 10:03:20 +02:00
*
* @return array
*/
public function keys()
{
return array_keys($this->properties);
}
/**
2013-06-25 10:40:20 +02:00
* Returns all properties and their values.
2013-06-25 10:03:20 +02:00
*
* @return array
*/
public function all()
{
return $this->properties;
}
/**
* {@inheritdoc}
*/
public function count()
{
return count($this->properties);
}
}