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) {
case 'set': try {
$metadatas_ops = array_merge( switch ($_m->action) {
$metadatas_ops, case 'set':
$this->setmetadatasAction_set($struct_fields, $caption_fields, $meta_id, $values) $ops = $this->setmetadatasAction_set($struct_fields, $caption_fields, $meta_id, $values);
); break;
break; case 'add':
case 'add': $ops = $this->setmetadatasAction_add($struct_fields, $values);
$metadatas_ops = array_merge( break;
$metadatas_ops, case 'delete':
$this->setmetadatasAction_add($struct_fields, $values) $ops = $this->setmetadatasAction_replace($caption_fields, $meta_id, $match_method, $values, null);
); break;
break; case 'replace':
case 'delete': if (!is_string($_m->replace_with) && !is_null($_m->replace_with)) {
$metadatas_ops = array_merge( throw new \Exception("bad \"replace_with\" for action \"replace\".");
$metadatas_ops, }
$this->setmetadatasAction_replace($caption_fields, $meta_id, $match_method, $values, null) $ops = $this->setmetadatasAction_replace($caption_fields, $meta_id, $match_method, $values, $_m->replace_with);
); break;
break; default:
case 'replace': throw new \Exception(sprintf("bad action (%s).", $action));
if(!is_string($_m->replace_with) && !is_null($_m->replace_with)) { }
return $this->app['controller.api.v1']->getBadRequestAction(
$request,
"bad \"replace_with\" for action \"replace\"."
);
}
$metadatas_ops = array_merge(
$metadatas_ops,
$this->setmetadatasAction_replace($caption_fields, $meta_id, $match_method, $values, $_m->replace_with)
);
break;
default:
return $this->app['controller.api.v1']->getBadRequestAction(
$request,
sprintf("bad action (%s).", $action)
);
} }
catch (\Exception $e) {
return $this->app['controller.api.v1']->getBadRequestAction(
$request,
$e->getMessage()
);
}
$metadatas_ops = array_merge($metadatas_ops, $ops);
} }
return Result::create($request, $metadatas_ops)->createResponse(); return Result::create($request, $metadatas_ops)->createResponse();
@@ -181,12 +174,13 @@ class V3Controller extends Controller
} }
/** /**
* @param databox_field[] $struct_fields struct-fields (from struct) matching meta_struct_id or field_name * @param databox_field[] $struct_fields struct-fields (from struct) matching meta_struct_id or field_name
* @param \caption_field[] $caption_fields caption-fields (from record) matching meta_struct_id or field_name (or all if not set) * @param \caption_field[] $caption_fields caption-fields (from record) matching meta_struct_id or field_name (or all if not set)
* @param int|null $meta_id * @param int|null $meta_id
* @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,
@@ -232,10 +229,11 @@ class V3Controller extends Controller
} }
/** /**
* @param databox_field[] $struct_fields struct-fields (from struct) matching meta_struct_id or field_name * @param databox_field[] $struct_fields struct-fields (from struct) matching meta_struct_id or field_name
* @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
]; ];
} }
} }