public function store($data) { foreach (array_keys($data) as $key) { if (!in_array($key, $this->getFields())) { throw new RuntimeException("Field {$key} was not defined for " . get_called_class()); } } $this->create(); $bindings = array(':::table' => $this->tableName); $query = 'INSERT INTO :::table (' . implode(',', array_keys($data)) . ') VALUES ('; $keys = array_keys($data); $n = count($keys); for ($i = 0; $i < $n; $i++) { $placeholder = ':' . $keys[$i]; $query .= $placeholder; if ($i < $n - 1) { $query .= ','; } $bindings[$placeholder] = $data[$keys[$i]]; } $query .= ')'; $this->db->execute($query, $bindings); $id = $this->db->lastInsertID(); assert($id >= 0 && $id <= PHP_INT_MAX); $gid = $this->gid->encode($this->gid->getType('row'), '', $id); $query = 'UPDATE :::table SET gid=:gid, parent=:parent WHERE id=:id'; $bindings = array(':::table' => $this->tableName, ':gid' => $gid, ':parent' => $this->parent, ':id' => $id); $this->db->execute($query, $bindings); $this->lastGID = $gid; }
/** * Checks for empty GIDs and creates new ones if necessary. * * @return boolean whether the configuration needs to be stored */ public function validateGIDs() { $count = 0; $hasChanged = false; foreach ($this->pages as $id => $page) { if (!isset($page['gid']) || $page['gid'] === null) { $this->pages[$id]['gid'] = $this->gid->encode($this->gid->getType('page'), $page['id'], $count); $hasChanged = true; } $count++; } return $hasChanged; }
/** * Tests a decoding failure. * * @covers empire\framework\gid\GID::decode */ public function testDecodeFail() { $this->assertFalse($this->gid->decode('invalid string')); }