1
0
Fork 0
mirror of https://github.com/Yetangitu/ampache synced 2025-10-04 18:29:40 +02:00
ampache/modules/Tmdb/Common/ObjectHydrator.php
Afterster d842ebbb00 Add art on Videos
Add videos support to Subsonic and UPnP APIs
Update Tmdb module
2014-07-06 20:46:46 +02:00

87 lines
2.2 KiB
PHP

<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <michael@wtfz.net>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Common;
use Tmdb\Exception\RuntimeException;
use Tmdb\Model\AbstractModel;
/**
* Utilisation class to hydrate objects.
*
* Class ObjectHydrator
* @package Tmdb\Common
*/
class ObjectHydrator
{
/**
* Hydrate the object with data
*
* @param AbstractModel $object
* @param array $data
* @return AbstractModel
* @throws RuntimeException
*/
public function hydrate(AbstractModel $object, $data = array())
{
if (!empty($data)) {
foreach ($data as $k => $v) {
if (in_array($k, $object::$properties)) {
$method = $this->camelize(
sprintf('set_%s', $k)
);
if (!is_callable(array($object, $method))) {
throw new RuntimeException(sprintf(
'Trying to call method "%s" on "%s" but it does not exist or is private.',
$method,
get_class($object)
));
} else {
$object->$method($v);
}
}
}
}
return $object;
}
/**
* Transforms an under_scored_string to a camelCasedOne
*
* @see https://gist.github.com/troelskn/751517
*
* @param string $candidate
* @return string
*/
public function camelize($candidate)
{
return lcfirst(
implode(
'',
array_map(
'ucfirst',
array_map(
'strtolower',
explode(
'_',
$candidate
)
)
)
)
);
}
}