3v4l.org

run code in 300+ PHP versions simultaneously
<?php class TableController extends Controller { private string $tableName; private array $conditions; public function __construct() { parent::__construct(); $this->tableName = $this->urlInst->params['tableName'] ?? 'book'; } /** * 更新 */ public function editAction(): void { $requestData = json_decode(file_get_contents('php://input'), true); $params = $this->urlInst->params; $tableName = $params['tableName']; // 編集できるテーブルか検証 if (!in_array($tableName, ['book'])) { throw new IllegalPostedException("Illegal tableNamee: {$tableName}"); } if ($tableName === 'book') { $itemMapperInst = new BookMapper(); // 個別に編集に $resItems に結果を追加していく $resItems = []; foreach ($requestData as $info) { // 一部が失敗したらその内容を返し処理を続けたいので // ループ内で catch し, 再 throw はしない try { $itemInst = new Book(); $itemInst->fromAry($info); $itemMapperInst->editItem($itemInst); $resItems[] = $itemInst; } catch (SkipException $e) { $parsed_exception = $e->getParsedException(); } catch (IllegalPostedException | \Exception | \Throwable $e) { $parsed_exception = parse_exception($e); } if (isset($parsed_exception)) { save_exception_log($parsed_exception); $resItems[] = $parsed_exception; } } } else { // 他の $tableName の場合の処理 } $this->adminRender([ 'resItems' => $resItems ]); } } class BookMapper extends ItemMapper { /** * 更新 */ public function editItem(object $bookInst): void { try { $this->pdo->beginTransaction(); // uniqueId から id を取得 $rowMapperInst = new CombinedBookRowMapper(); $combinedBookId = $rowMapperInst->getIdByUniqueId($bookInst->combinedBookUniqueId); if (!$combinedBookId) { // uniqueId が存在しない場合 throw new NotFoundException("not found uniqueId: {$bookInst->combinedBookUniqueId}"); } $bookInst->combinedBookId = $rowMapperInst->getIdByUniqueId($bookInst->combinedBookUniqueId); // tbl_books テーブルをUPDATE $rowMapperInst = new BookRowMapper(); $rowMapperInst->editByBookInst($bookInst); // tbl_combined_book_titles テーブルをUPDATE $rowMapperInst = new CombinedBookTitleRowMapper(); $rowMapperInst->editByBookInst($bookInst); $this->pdo->commit(); } catch (SkipException $e) { $this->pdo->rollBack(); throw new SkipException($e->getParsedException()); } catch (\Exception $e) { $this->pdo->rollBack(); throw new SkipException(parse_exception($e)); } } }

preferences:
30.16 ms | 405 KiB | 5 Q