model = new ProductModel(); } public function __invoke(ServerRequestInterface $request) { $body = json_decode((string)$request->getBody(), true) ?? []; try { v::key('company_id', v::intType()->positive()) ->key('update_product_id', v::intType()->positive()) ->key('product_name', v::optional(v::stringType()->notEmpty()), false) ->key('product_price', v::optional(v::number()->positive()), false) ->assert($body); } catch (ValidationException $e) { return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400); } // Verifica se pelo menos um dos dois está presente $hasProductName = isset($body['product_name']) && $body['product_name'] !== null; $hasProductPrice = isset($body['product_price']) && $body['product_price'] !== null; if (!$hasProductName && !$hasProductPrice) { return ResponseLib::sendFail("Missing product_name or product_price for update", [], "E_VALIDATE")->withStatus(400); } $companyId = $body['company_id']; $productId = $body['update_product_id']; $productName = $hasProductName ? $body['product_name'] : null; $productPrice = $hasProductPrice ? (float)$body['product_price'] : null; $updated = $this->model->updateProduct( (int)$productId, (int)$companyId, $productName, $productPrice ); return $updated ? ResponseLib::sendOk(['updated' => true]) : ResponseLib::sendFail("Failed to Update Product or Product Not Found", [], "E_DATABASE")->withStatus(404); } }