fix ; add errors catch
todo :
- status-bits
- apply changes to record
- json output
This commit is contained in:
jygaulier
2020-06-03 12:48:13 +02:00
parent b67c37c340
commit aeb7b4e60b

View File

@@ -126,43 +126,36 @@ class V3Controller extends Controller
if(!($action = (string)($_m->action))) { if(!($action = (string)($_m->action))) {
$action = 'set'; $action = 'set';
} }
switch($_m->action) {
try {
switch ($_m->action) {
case 'set': case 'set':
$metadatas_ops = array_merge( $ops = $this->setmetadatasAction_set($struct_fields, $caption_fields, $meta_id, $values);
$metadatas_ops,
$this->setmetadatasAction_set($struct_fields, $caption_fields, $meta_id, $values)
);
break; break;
case 'add': case 'add':
$metadatas_ops = array_merge( $ops = $this->setmetadatasAction_add($struct_fields, $values);
$metadatas_ops,
$this->setmetadatasAction_add($struct_fields, $values)
);
break; break;
case 'delete': case 'delete':
$metadatas_ops = array_merge( $ops = $this->setmetadatasAction_replace($caption_fields, $meta_id, $match_method, $values, null);
$metadatas_ops,
$this->setmetadatasAction_replace($caption_fields, $meta_id, $match_method, $values, null)
);
break; break;
case 'replace': case 'replace':
if(!is_string($_m->replace_with) && !is_null($_m->replace_with)) { if (!is_string($_m->replace_with) && !is_null($_m->replace_with)) {
return $this->app['controller.api.v1']->getBadRequestAction( throw new \Exception("bad \"replace_with\" for action \"replace\".");
$request,
"bad \"replace_with\" for action \"replace\"."
);
} }
$metadatas_ops = array_merge( $ops = $this->setmetadatasAction_replace($caption_fields, $meta_id, $match_method, $values, $_m->replace_with);
$metadatas_ops,
$this->setmetadatasAction_replace($caption_fields, $meta_id, $match_method, $values, $_m->replace_with)
);
break; break;
default: default:
throw new \Exception(sprintf("bad action (%s).", $action));
}
}
catch (\Exception $e) {
return $this->app['controller.api.v1']->getBadRequestAction( return $this->app['controller.api.v1']->getBadRequestAction(
$request, $request,
sprintf("bad action (%s).", $action) $e->getMessage()
); );
} }
$metadatas_ops = array_merge($metadatas_ops, $ops);
} }
return Result::create($request, $metadatas_ops)->createResponse(); return Result::create($request, $metadatas_ops)->createResponse();
@@ -187,6 +180,7 @@ class V3Controller extends Controller
* @param string[] $values * @param string[] $values
* *
* @return array ops to execute * @return array ops to execute
* @throws \Exception
*/ */
private function setmetadatasAction_set($struct_fields, $caption_fields, $meta_id, $values) private function setmetadatasAction_set($struct_fields, $caption_fields, $meta_id, $values)
{ {
@@ -196,7 +190,7 @@ class V3Controller extends Controller
foreach ($caption_fields as $cf) { foreach ($caption_fields as $cf) {
if ($cf->is_multi() && is_null($meta_id)) { if ($cf->is_multi() && is_null($meta_id)) {
foreach ($cf->get_values() as $field_value) { foreach ($cf->get_values() as $field_value) {
$a[] = [ $ops[] = [
'meta_struct_id' => $cf->get_meta_struct_id(), 'meta_struct_id' => $cf->get_meta_struct_id(),
'meta_id' => $field_value->getId(), 'meta_id' => $field_value->getId(),
'value' => null 'value' => null
@@ -220,6 +214,9 @@ class V3Controller extends Controller
} }
else { else {
// mono-valued // mono-valued
if(count($values) > 1) {
throw new \Exception(sprintf("setting mono-valued (%s) requires only one value.", $sf->get_name()));
}
$ops[] = [ $ops[] = [
'meta_struct_id' => $sf->get_id(), 'meta_struct_id' => $sf->get_id(),
'meta_id' => $meta_id, // probably null, 'meta_id' => $meta_id, // probably null,
@@ -236,6 +233,7 @@ class V3Controller extends Controller
* @param string[] $values * @param string[] $values
* *
* @return array ops to execute * @return array ops to execute
* @throws \Exception
*/ */
private function setmetadatasAction_add($struct_fields, $values) private function setmetadatasAction_add($struct_fields, $values)
{ {
@@ -244,8 +242,7 @@ class V3Controller extends Controller
// now set values to matching struct_fields // now set values to matching struct_fields
foreach ($struct_fields as $sf) { foreach ($struct_fields as $sf) {
if(!$sf->is_multi()) { if(!$sf->is_multi()) {
// todo : return error "cant add to mono-valued" throw new \Exception(sprintf("can't \"add\" to mono-valued (%s).", $sf->get_name()));
continue;
} }
// add the non-null value(s) // add the non-null value(s)
foreach ($values as $value) { foreach ($values as $value) {
@@ -290,7 +287,7 @@ class V3Controller extends Controller
if (!is_null($meta_id)) { if (!is_null($meta_id)) {
foreach ($cf->get_values() as $field_value) { foreach ($cf->get_values() as $field_value) {
if ($field_value->getId() === $meta_id) { if ($field_value->getId() === $meta_id) {
$a[] = [ $ops[] = [
'meta_struct_id' => $cf->get_meta_struct_id(), 'meta_struct_id' => $cf->get_meta_struct_id(),
'meta_id' => $field_value->getId(), 'meta_id' => $field_value->getId(),
'value' => $replace_with 'value' => $replace_with
@@ -301,11 +298,15 @@ class V3Controller extends Controller
// match by value(s) ? // match by value(s) ?
foreach ($values as $value) { foreach ($values as $value) {
foreach ($cf->get_values() as $field_value) { foreach ($cf->get_values() as $field_value) {
$rw = $replace_with;
if($match_method=='regexp' && !is_null($replace_with)) {
$rw = preg_replace($value, $replace_with, $field_value->getValue());
}
if ($this->match($value, $match_method, $field_value->getValue())) { if ($this->match($value, $match_method, $field_value->getValue())) {
$ops[] = [ $ops[] = [
'meta_struct_id' => $cf->get_meta_struct_id(), 'meta_struct_id' => $cf->get_meta_struct_id(),
'meta_id' => $field_value->getId(), 'meta_id' => $field_value->getId(),
'value' => $match_method=='regexp' ? preg_replace($value, $replace_with, $field_value->getValue()): $replace_with 'value' => $rw
]; ];
} }
} }