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 decoding. * * @dataProvider decodeProvider * @covers empire\framework\gid\GID::decode * * @param int $type the entity type * @param string $name the entity name * @param int $localID the local entity ID */ public function testDecode($type, $name, $localID) { $gid = $this->gid->encode($type, $name, $localID); $this->assertSame(32, strlen($gid)); $this->assertTrue((bool) preg_match('#^[0-9a-f]+$#', $gid)); $decoded = $this->gid->decode($gid); $decodedType = $decoded['type']; $decodedCrc32Name = $decoded['crc32Name']; $decodedLocalID = $decoded['localID']; $decodedUniqueID = $decoded['uniqueID']; $this->assertSame($type, $decodedType); $this->assertEquals(dechex(crc32($name)), $decodedCrc32Name); $this->assertSame($decodedLocalID, $localID); $this->assertSame(13, strlen($decodedUniqueID)); $this->assertTrue((bool) preg_match('#^[0-9a-f]+$#', $decodedUniqueID)); }