public function update(Cfp $cfp, $fetchHash) { if (!$fetchHash) { throw new \UnexpectedValueException('No Hash given', 400); } $oldValues = $this->select($fetchHash); if ($oldValues->count() != 1) { throw new \UnexpectedValueException('There is no CFP with this URL.', 404); } $statementElements = []; $values = []; $oldValues = $oldValues->current(); $options = ['dateCfpStart', 'dateCfpEnd', 'name', 'uri', 'hash', 'timezone', 'dateEventStart', 'dateEventEnd', 'description', 'eventUri', 'iconUri', 'latitude', 'longitude', 'location', 'tags']; foreach ($options as $option) { $method = 'get' . $option; if ($cfp->{$method}() != $oldValues->{$method}()) { $statementElements[] = '`' . $option . '` = :' . $option; $values[$option] = $cfp->{$method}(); if ($values[$option] instanceof \DateTimeInterface) { $values[$option] = $values[$option]->format('c'); } if (is_array($values[$option])) { $values[$option] = implode(',', $values[$option]); } } } // No values to update, just, return if (!$values) { return $cfp->getHash(); } $statement = 'UPDATE `cfp` SET ' . implode(',', $statementElements) . ',' . '`lastUpdate` = :lastUpdate ' . 'WHERE `hash` = :fetchHash'; $statement = $this->pdo->prepare($statement); $values['fetchHash'] = $fetchHash; $values['lastUpdate'] = (new \DateTime('now', new \DateTimezone('UTC')))->format('c'); if ($statement->execute($values)) { return $cfp->getHash(); } throw new \UnexpectedValueException('The CfP could not be updated', 400); }