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); }
public function select($hash = null) { $statement = 'SELECT * FROM `cfp`'; $values = []; if ($hash !== null) { $statement .= ' WHERE `hash`= :hash'; $values['hash'] = $hash; } $statement = $this->pdo->prepare($statement); $list = new \Callingallpapers\Api\Entity\CfpList(); $statement->execute($values); $content = $statement->fetchAll(); if (count($content) < 1) { throw new \UnexpectedValueException('No CFPs found', 404); } foreach ($content as $item) { $cfp = new \Callingallpapers\Api\Entity\Cfp(); $cfp->setName($item['name']); $cfp->setDateCfpEnd(new \DateTimeImmutable($item['dateCfpEnd'])); $cfp->setDateCfpStart(new \DateTimeImmutable($item['dateCfpStart'])); $cfp->setUri($item['uri']); $cfp->setTimezone(new \DateTimeZone($item['timezone'])); $cfp->setDateEventStart(new \DateTimeImmutable($item['dateEventStart'])); $cfp->setDateEventEnd(new \DateTimeImmutable($item['dateEventEnd'])); $cfp->setDescription($item['description']); $cfp->setEventUri($item['eventUri']); $cfp->setIconUri($item['iconUri']); $cfp->setLatitude($item['latitude']); $cfp->setLongitude($item['longitude']); $cfp->setLocation($item['location']); $cfp->setTags(explode(',', $item['tags'])); $cfp->setLastUpdated(new \DateTimeImmutable($item['lastUpdate'])); $list->add($cfp); } return $list; }
public function testUpdatingEntry() { $this->assertEquals(2, $this->getConnection()->getRowCount('cfp'), "Pre-Condition"); $cpl = new CfpPersistenceLayer($this->pdo); $cfp = new Cfp(); $cfp->setEventUri('http://example.com'); $newHash = $cpl->update($cfp, 'ff'); $this->assertEquals(sha1('http://example.com'), $newHash); $this->assertEquals(2, $this->getConnection()->getRowCount('cfp'), "Post-Condition"); $results = $cpl->select(sha1('http://example.com')); $this->assertEquals(1, $results->count()); }
public static function setTags(Cfp $cfp, array $array) { if (!isset($array['tags'])) { return; } $cfp->setTags(array_map(function ($item) { return filter_var($item, FILTER_SANITIZE_STRING); }, $array['tags'])); }