Skip to content

Commit

Permalink
#39 #34 added the option to ignore children
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Franek committed Aug 31, 2018
1 parent 42d8b2d commit 3c61399
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/Configuration/WebuntisConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

use Webuntis\Repositories\Repository;
use Webuntis\WebuntisFactory;
use Webuntis\Models\AbstractModel;
use Webuntis\Models\Interfaces\ConfigurationModelInterface;

/**
* manages the different configurations and passes them to the WebuntisFactory
Expand Down Expand Up @@ -32,6 +34,27 @@ public static function addConfig(array $config) : void
WebuntisFactory::setConfig($newConfig);
}

/**
* gets the right instance config from given model
* @param AbstractModel $model
* @return array
*/
public static function getConfigByModel(AbstractModel $model) : array
{
$config = WebuntisFactory::getConfig();
$model = get_class($model);
$interfaces = class_implements($model);
if (isset($interfaces[ConfigurationModelInterface::class])) {
$configName = $model::CONFIG_NAME;
} else if (isset($config['only_admin']) && $config['only_admin']) {
$configName = 'admin';
} else {
$configName = 'default';
}

return $config[$configName];
}

/**
* gets the current config
* @return array
Expand Down
8 changes: 7 additions & 1 deletion src/Types/TypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


use Webuntis\Configuration\YAMLConfiguration;
use Webuntis\Configuration\WebuntisConfiguration;
use Webuntis\Exceptions\TypeException;
use Webuntis\Models\AbstractModel;
use Webuntis\Types\Interfaces\TypeInterface;
Expand Down Expand Up @@ -67,10 +68,15 @@ public static function getAllTypes() : array
*/
public function handle(AbstractModel &$model, array $data, array $fields) : void
{
$instanceConfig = WebuntisConfiguration::getConfigByModel($model);
foreach ($fields as $key => $value) {
$implements = class_implements(self::$types[$value['type']]);
if (isset($implements[TypeInterface::class])) {
self::$types[$value['type']]::execute($model, $data, [$key => $value]);
if(($value['type'] == 'model' || $value['type'] == 'modelCollection') && (isset($instanceConfig['ignore_children']) && $instanceConfig['ignore_children'])) {
self::$types['array']::execute($model, $data, [$key => $value]);
} else {
self::$types[$value['type']]::execute($model, $data, [$key => $value]);
}
} else {
throw new TypeException('this type is not supported');
}
Expand Down

0 comments on commit 3c61399

Please sign in to comment.