|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * ArangoDB PHP client: single document |
| 5 | + * |
| 6 | + * @package ArangoDbPhpClient |
| 7 | + * @author Jan Steemann |
| 8 | + * @copyright Copyright 2012, triagens GmbH, Cologne, Germany |
| 9 | + */ |
| 10 | + |
| 11 | +namespace triagens\ArangoDb; |
| 12 | + |
| 13 | +/** |
| 14 | + * Value object representing a single collection-based document |
| 15 | + * |
| 16 | + * @package ArangoDbPhpClient |
| 17 | + */ |
| 18 | +class Edge extends Document { |
| 19 | + /** |
| 20 | + * The document id (might be NULL for new documents) |
| 21 | + * @var string - document id |
| 22 | + */ |
| 23 | + private $_id = NULL; |
| 24 | + |
| 25 | + /** |
| 26 | + * The document revision (might be NULL for new documents) |
| 27 | + * @var mixed |
| 28 | + */ |
| 29 | + private $_rev = NULL; |
| 30 | + |
| 31 | + /** |
| 32 | + * The document attributes (names/values) |
| 33 | + * @var array |
| 34 | + */ |
| 35 | + private $_values = array(); |
| 36 | + |
| 37 | + /** |
| 38 | + * Flag to indicate whether document was changed locally |
| 39 | + * @var bool |
| 40 | + */ |
| 41 | + private $_changed; |
| 42 | + |
| 43 | + /** |
| 44 | + * Document _id index |
| 45 | + */ |
| 46 | + const ENTRY_ID = '_id'; |
| 47 | + |
| 48 | + /** |
| 49 | + * Revision _rev index |
| 50 | + */ |
| 51 | + const ENTRY_REV = '_rev'; |
| 52 | + |
| 53 | + /** |
| 54 | + * Document _from index |
| 55 | + */ |
| 56 | + |
| 57 | + const ENTRY_FROM = '_from'; |
| 58 | + |
| 59 | + /** |
| 60 | + * Revision _to index |
| 61 | + */ |
| 62 | + const ENTRY_TO = '_to'; |
| 63 | + |
| 64 | + |
| 65 | + /** |
| 66 | + * Clone a document |
| 67 | + * Returns the clone |
| 68 | + * |
| 69 | + * @return void |
| 70 | + */ |
| 71 | + public function __clone() { |
| 72 | + $this->_id = NULL; |
| 73 | + $this->_rev = NULL; |
| 74 | + $this->_to = NULL; |
| 75 | + $this->_from = NULL; |
| 76 | + |
| 77 | + // do not change the _changed flag here |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + /** |
| 82 | + * Set a document attribute |
| 83 | + * |
| 84 | + * The key (attribute name) must be a string. |
| 85 | + * This will validate the value of the attribute and might throw an |
| 86 | + * exception if the value is invalid. |
| 87 | + * |
| 88 | + * @throws ClientException |
| 89 | + * @param string $key - attribute name |
| 90 | + * @param mixed $value - value for attribute |
| 91 | + * @return void |
| 92 | + */ |
| 93 | + public function set($key, $value) { |
| 94 | + if (!is_string($key)) { |
| 95 | + throw new ClientException('Invalid document attribute key'); |
| 96 | + } |
| 97 | + |
| 98 | + // validate the value passed |
| 99 | + ValueValidator::validate($value); |
| 100 | + |
| 101 | + if ($key === self::ENTRY_ID) { |
| 102 | + $this->setInternalId($value); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + if ($key === self::ENTRY_REV) { |
| 107 | + $this->setRevision($value); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + if ($key === self::ENTRY_FROM) { |
| 112 | + $this->setInternalId($value); |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + if ($key === self::ENTRY_TO) { |
| 117 | + $this->setRevision($value); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + if (!$this->_changed) { |
| 122 | + if (!isset($this->_values[$key]) || $this->_values[$key] !== $value) { |
| 123 | + // set changed flag |
| 124 | + $this->_changed = true; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // and store the value |
| 129 | + $this->_values[$key] = $value; |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + /** |
| 134 | + * Get the document revision (if already known) |
| 135 | + * |
| 136 | + * @return mixed - revision id, might be NULL if document does not yet have an id |
| 137 | + */ |
| 138 | + public function getFrom() { |
| 139 | + return $this->_from; |
| 140 | + } |
| 141 | + /** |
| 142 | + * Get the document revision (if already known) |
| 143 | + * |
| 144 | + * @return mixed - revision id, might be NULL if document does not yet have an id |
| 145 | + */ |
| 146 | + public function getTo() { |
| 147 | + return $this->_to; |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments