Skip to content

Commit

Permalink
Feature/mediainfocontainer dump (#50)
Browse files Browse the repository at this point in the history
* added globally support to dump the "MediaInfoContainer" into json and array

* readme.md updated

* composer.json > PHP Required version updated

* added .gitignore

* composer.lock removed

* composer.phar removed

* vendor directory removed

* CI fix

* readme.md restores

* Added unit tests

* Added unit test without require mediainfo

* Removed 5.3.x test for CI
  • Loading branch information
javiertrejo authored and mhor committed May 2, 2016
1 parent 7edba90 commit 54f17be
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vendor/
composer.phar
composer.lock
MediaInfo.exe
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ language: php
sudo: false

php:
- 5.3.3
- 5.3
- 5.4
- 5.5.9
- 5.5
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ $mediaInfo->setConfig('command', '/usr/local/bin/mediainfo');
$mediaInfoContainer = $mediaInfo->getInfo('music.mp3');
```

### MediaInfoContainer to JSON and Array
```php
$mediaInfo = new MediaInfo();
$mediaInfoContainer = $mediaInfo->getInfo('music.mp3');

$json = json_encode($mediaInfoContainer);
$array = $mediaInfoContainer->__toArray();
```

### Symfony integration

Look at this bundle: [MhorMediaInfoBunde](https://github.com/mhor/MhorMediaInfoBundle)
Expand All @@ -231,4 +240,4 @@ Look at this bundle: [MhorMediaInfoBunde](https://github.com/mhor/MhorMediaInfoB
Look at [this](https://philsturgeon.uk/blog/2012/05/composer-with-codeigniter/) to use composer with Codeigniter

##License
See `LICENSE` for more information
See `LICENSE` for more information
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"php": ">=5.3.3",
"php": ">=5.4.0",
"symfony/process": "~2.3|~3.0",
"symfony/filesystem": "~2.3|~3.0"
},
Expand All @@ -29,7 +29,6 @@
"Mhor\\MediaInfo\\Test\\": "test/"
}
},

"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
Expand Down
22 changes: 21 additions & 1 deletion src/Container/MediaInfoContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Mhor\MediaInfo\Type\Subtitle;
use Mhor\MediaInfo\Type\Video;

class MediaInfoContainer
class MediaInfoContainer implements \JsonSerializable
{
const GENERAL_CLASS = 'Mhor\MediaInfo\Type\General';
const AUDIO_CLASS = 'Mhor\MediaInfo\Type\Audio';
Expand Down Expand Up @@ -222,4 +222,24 @@ private function addOther(Other $other)
{
$this->others[] = $other;
}

/**
* Convert the object into array
*
* @return array
*/
public function jsonSerialize()
{
return get_object_vars($this);
}

/**
* Dump object to array
*
* @return array
*/
public function __toArray()
{
return $this->jsonSerialize();
}
}
28 changes: 27 additions & 1 deletion src/Type/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Mhor\MediaInfo\Type;

abstract class AbstractType
abstract class AbstractType implements \JsonSerializable
{
/**
* @var array
Expand Down Expand Up @@ -37,4 +37,30 @@ public function get($attribute = null)

return;
}

/**
* Convert the object into json
*
* @return array
*/
public function jsonSerialize()
{
$array = get_object_vars($this);

if (isset($array['attributes'])) {
$array = $array['attributes'];
}

return $array;
}

/**
* Convert the object into array
*
* @return array
*/
public function __toArray()
{
return $this->jsonSerialize();
}
}
64 changes: 64 additions & 0 deletions test/Container/MediaInfoContainerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

use Mhor\MediaInfo\Container\MediaInfoContainer,
Mhor\MediaInfo\Type\General,
Mhor\MediaInfo\Type\Audio;

class MediaInfoContainerTest extends \PHPUnit_Framework_TestCase
{
private function createContainer()
{
$mediaInfoContainer = new MediaInfoContainer();

$general = new General();

$general->set('Format', 'MPEG Audio');
$general->set('Duration', '1mn 20s');

$audio = new Audio();

$audio->set('Format', 'MPEG Audio');
$audio->set('Bit rate', '56.0 Kbps');

$mediaInfoContainer->add($audio);
$mediaInfoContainer->add($general);

return $mediaInfoContainer;
}

public function testToJson()
{
$mediaInfoContainer = $this->createContainer();

$data = json_encode($mediaInfoContainer);

$this->assertRegExp('/^\{.+\}$/', $data);
}

public function testToJsonType()
{
$mediaInfoContainer = $this->createContainer();

$data = json_encode($mediaInfoContainer->getGeneral());

$this->assertRegExp('/^\{.+\}$/', $data);
}

public function testToArray()
{
$mediaInfoContainer = $this->createContainer();

$array = $mediaInfoContainer->__toArray();

$this->assertArrayHasKey('version', $array);
}

public function testToArrayType()
{
$mediaInfoContainer = $this->createContainer();

$array = $mediaInfoContainer->getGeneral()->__toArray();

$this->assertTrue(is_array($array));
}
}
Binary file modified test/fixtures/test.mp3
Binary file not shown.

0 comments on commit 54f17be

Please sign in to comment.