+ $this->data = (array) $data;
+
+ if ($prime) {
+ $this->prime();
+ }
+ }
+
+ /**
+ * Copy constructor
+ * @param array $data
+ * @return \pq\Gateway\Row
+ */
+ function __invoke(array $data) {
+ $that = clone $this;
+ $that->data = $data;
+ return $that->prime();
+ }
+
+ /**
+ * Export current state as an array
+ * @return array
+ * @throws \UnexpectedValueException if a cell has been modified by an expression
+ */
+ function export() {
+ $export = array_merge($this->data, $this->cell);
+ foreach ($export as &$val) {
+ if ($val instanceof Cell) {
+ if ($val->isExpr()) {
+ throw new \UnexpectedValueException("Cannot export an SQL expression");
+ }
+ $val = $val->get();
+ }
+ }
+ return $export;
+ }
+
+ /**
+ * Export current state with security sensitive data removed. You should override that.
+ * Just calls export() by default.
+ * @return array
+ */
+ function exportPublic() {
+ return $this->export();
+ }
+
+ /**
+ * @implements JsonSerializable
+ * @return array
+ */
+ function jsonSerialize() {
+ return $this->exportPublic();
+ }
+
+ /**
+ * @return \pq\Gateway\Table
+ */
+ function getTable() {
+ return $this->table;
+ }
+
+ /**
+ * @return array
+ */
+ function getData() {
+ return $this->data;
+ }
+
+ /**
+ * Get all column/value pairs to possibly uniquely identify this row
+ * @return array
+ * @throws \OutOfBoundsException if any primary key column is not present in the row
+ */
+ function getIdentity() {
+ $cols = array();
+ if (count($identity = $this->getTable()->getIdentity())) {
+ foreach ($identity as $col) {
+ if (!array_key_exists($col, $this->data)) {
+ throw new \OutOfBoundsException(
+ sprintf("Column '%s' does not exist in row of table '%s'",
+ $col, $this->getTable()->getName()
+ )
+ );
+ }
+ $cols[$col] = $this->data[$col];
+ }
+ } else {
+ $cols = $this->data;
+ }
+ return $cols;
+ }
+
+ /**
+ * Check whether the row contains modifications
+ * @return boolean
+ */
+ function isDirty() {
+ foreach ($this->cell as $cell) {
+ if ($cell->isDirty()) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Refresh the rows data
+ * @return \pq\Gateway\Row
+ */
+ function refresh() {
+ $this->data = $this->table->find($this->criteria(), null, 1, 0)->current()->data;
+ $this->cell = array();
+ return $this;
+ }
+
+ /**
+ * Fill modified cells
+ * @return \pq\Gateway\Row
+ */
+ protected function prime() {
+ $this->cell = array();
+ foreach ($this->data as $key => $val) {
+ $this->cell[$key] = new Cell($this, $key, $val, true);
+ }
+ return $this;