mirror of
https://github.com/Yetangitu/ampache
synced 2025-10-05 19:41:55 +02:00
Add php-tmdb-api module
This commit is contained in:
parent
786c234d5f
commit
d5d84d6462
189 changed files with 20018 additions and 1 deletions
81
modules/Tmdb/Common/ObjectHydrator.php
Normal file
81
modules/Tmdb/Common/ObjectHydrator.php
Normal file
|
@ -0,0 +1,81 @@
|
|||
<?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
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue