195 lines
6.9 KiB
PHP
195 lines
6.9 KiB
PHP
<?php
|
|
|
|
class stWebApiAdminGeneratorHelper
|
|
{
|
|
protected static $methods = null;
|
|
protected static $fields = null;
|
|
|
|
public static function addMethod(stAdminGenerator $generator, string $method, array $params)
|
|
{
|
|
self::getMethods($generator);
|
|
|
|
self::$methods[$method] = $params;
|
|
}
|
|
|
|
public static function getMethods(stAdminGenerator $generator): array
|
|
{
|
|
if (null === self::$methods)
|
|
{
|
|
$modelClass = $generator->getClassName();
|
|
$models = [$modelClass => $modelClass];
|
|
$methods = $generator->getParameterValue('webapi.methods', []);
|
|
|
|
foreach ($methods as $name => $params)
|
|
{
|
|
$type = isset($params['type']) ? $params['type'] : null;
|
|
|
|
$modelClass = $generator->getClassName();
|
|
|
|
if (isset($params['peer']))
|
|
{
|
|
$modelClass = $params['peer'];
|
|
}
|
|
elseif (isset($params['model_class']))
|
|
{
|
|
$modelClass = $params['model_class'];
|
|
}
|
|
|
|
if (!isset($models[$modelClass]))
|
|
{
|
|
$models[$modelClass] = $modelClass;
|
|
}
|
|
}
|
|
|
|
/*
|
|
CountProductI18n:
|
|
type: count
|
|
peer_method: doCountWithI18n
|
|
inOutTypes: {in: object, out: object}
|
|
fields:
|
|
in: [active]
|
|
out: [_count]
|
|
GetProductI18nList:
|
|
type: list
|
|
peer_method: doSelectWithI18n
|
|
inOutTypes: {in: object, out: array}
|
|
*/
|
|
|
|
foreach ($models as $model)
|
|
{
|
|
$positioningModelClass = $model.'HasPositioning';
|
|
|
|
if (class_exists($positioningModelClass))
|
|
{
|
|
$methods['Count' . $model . 'PositioningI18n'] = [
|
|
'model_class' => $positioningModelClass,
|
|
'peer_method' => 'doCountWithI18n',
|
|
'type' => 'count',
|
|
'inOutTypes' => ['in' => 'object', 'out' => 'object'],
|
|
'fields' => [
|
|
'out' => ['_count'],
|
|
],
|
|
];
|
|
|
|
$methods['Get' . $model . 'PositioningI18nList'] = [
|
|
'model_class' => $positioningModelClass,
|
|
'peer_method' => 'doSelectWithI18n',
|
|
'type' => 'list',
|
|
'inOutTypes' => ['in' => 'object', 'out' => 'array'],
|
|
'fields' => [
|
|
'in' => ['_offset', '_limit', '_modified_from', '_modified_to'],
|
|
'out' => ['id', 'title', 'keywords', 'description'],
|
|
],
|
|
'custom_fields' => [
|
|
'id' => ['type' => 'int'],
|
|
'title' => ['type' => 'string'],
|
|
'keywords' => ['type' => 'string'],
|
|
'description' => ['type' => 'string'],
|
|
]
|
|
];
|
|
|
|
$methods['Update' . $model . 'PositioningI18n'] = [
|
|
'model_class' => $positioningModelClass,
|
|
'type' => 'update',
|
|
'inOutTypes' => ['in' => 'object', 'out' => 'object'],
|
|
'fields' => [
|
|
'in' => ['=id', 'title', 'keywords', 'description'],
|
|
'out' => ['_update'],
|
|
],
|
|
'custom_fields' => [
|
|
'id' => ['type' => 'int'],
|
|
'title' => ['type' => 'string'],
|
|
'keywords' => ['type' => 'string'],
|
|
'description' => ['type' => 'string'],
|
|
]
|
|
];
|
|
}
|
|
}
|
|
|
|
self::$methods = $methods;
|
|
}
|
|
|
|
return self::$methods;
|
|
}
|
|
|
|
public static function getFields(stAdminGenerator $generator)
|
|
{
|
|
if (null === self::$fields)
|
|
{
|
|
$fields = $generator->getParameterValue('webapi.fields', []);
|
|
$fields['_delete']['type'] = "integer";
|
|
$fields['_offset']['type'] = "integer";
|
|
$fields['_limit']['type'] = "integer";
|
|
$fields['_count']['type'] = "integer";
|
|
$fields['_update']['type'] = "integer";
|
|
$fields['created_at']['type'] = "dateTime";
|
|
$fields['updated_at']['type'] = "dateTime";
|
|
$fields['_modified_from']['type'] = "dateTime";
|
|
$fields['_modified_to']['type'] = "dateTime";
|
|
$fields['_session_hash']['type'] = "string";
|
|
$fields['_culture']['type'] = "string";
|
|
$fields['_is_translated']['type'] = "boolean";
|
|
$fields['_errors'] = ['type' => 'ArrayOfBatchErrors'];
|
|
$fields['_updates'] = ['type' => 'ArrayOfBatchUpdates'];
|
|
|
|
self::$fields = $fields;
|
|
}
|
|
|
|
return self::$fields;
|
|
}
|
|
|
|
public static function getField(stAdminGenerator $generator, string $method, string $name)
|
|
{
|
|
$fields = self::getFields($generator);
|
|
$methods = self::getMethods($generator);
|
|
|
|
if (isset($methods[$method]['custom_fields'][$name]))
|
|
{
|
|
return $methods[$method]['custom_fields'][$name];
|
|
}
|
|
|
|
if (!isset($fields[$name]))
|
|
{
|
|
throw new Exception(sprintf('Field name "%s" is not defined in webapi.fields or webapi.[method_name].custom_fields', $name));
|
|
}
|
|
|
|
return $fields[$name];
|
|
}
|
|
|
|
public static function getPeerMethod(stAdminGenerator $generator, string $method)
|
|
{
|
|
$methods = self::getMethods($generator);
|
|
|
|
if (isset($methods[$method]['peer_method']))
|
|
{
|
|
return $methods[$method]['peer_method'];
|
|
}
|
|
|
|
return $methods[$method]['type'] == 'list' ? 'doSelect' : 'doCount';
|
|
}
|
|
|
|
public static function getMethodFields(stAdminGenerator $generator, string $method): array
|
|
{
|
|
$fields = self::getFields($generator);
|
|
$methods = self::getMethods($generator);
|
|
|
|
return isset($methods[$method]['custom_fields']) ? array_replace($fields, $methods[$method]['custom_fields']) : $fields;
|
|
}
|
|
|
|
public static function getModelClass(stAdminGenerator $generator, string $method)
|
|
{
|
|
$methods = self::getMethods($generator);
|
|
|
|
if (isset($methods[$method]['peer']))
|
|
{
|
|
return $methods[$method]['peer'];
|
|
}
|
|
|
|
if (isset($methods[$method]['model_class']))
|
|
{
|
|
return $methods[$method]['model_class'];
|
|
}
|
|
|
|
return $generator->getParameterValue('model_class');
|
|
}
|
|
} |