Skip to content

Commit b0d129c

Browse files
authored
Fix for issue #194.
There was some missing implementation and the functionality was unfortunately not thoroughly tested, so it slipped through. This patch fixes functionality and tests.
1 parent fbe249b commit b0d129c

File tree

1 file changed

+83
-39
lines changed

1 file changed

+83
-39
lines changed

lib/triagens/ArangoDb/Document.php

Lines changed: 83 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,14 @@ class Document
7575
*
7676
* @var bool
7777
*/
78-
protected $_hidden = array();
78+
protected $_hiddenAttributes = array();
79+
80+
/**
81+
* Flag to indicate whether document was changed locally
82+
*
83+
* @var bool
84+
*/
85+
protected $_ignoreHiddenAttributes = false;
7986

8087
/**
8188
* Document id index
@@ -100,7 +107,12 @@ class Document
100107
/**
101108
* hidden attribute index
102109
*/
103-
const ENTRY_HIDDEN = '_hidden';
110+
const ENTRY_HIDDENATTRIBUTES = '_hiddenAttributes';
111+
112+
/**
113+
* hidden attribute index
114+
*/
115+
const ENTRY_IGNOREHIDDENATTRIBUTES = '_ignoreHiddenAttributes';
104116

105117
/**
106118
* waitForSync option index
@@ -121,7 +133,12 @@ class Document
121133
* Constructs an empty document
122134
*
123135
* @param array $options - optional, initial $options for document
124-
*
136+
* <p>Options are :<br>
137+
* <li>'_hiddenAttributes' - Set an array of hidden attributes for created documents.
138+
* <li>'hiddenAttributes' - Deprecated, please use '_hiddenAttributes'.</li>
139+
* <li>'_ignoreHiddenAttributes' - true to show hidden attributes. Defaults to false</li>
140+
* <p>
141+
* *
125142
* @return Document
126143
*/
127144
public function __construct(array $options = null)
@@ -131,8 +148,11 @@ public function __construct(array $options = null)
131148
if (isset($options['hiddenAttributes'])) {
132149
$this->setHiddenAttributes($options['hiddenAttributes']);
133150
}
134-
if (isset($options['_hiddenAttributes'])) {
135-
$this->setHiddenAttributes($options['_hiddenAttributes']);
151+
if (isset($options[self::ENTRY_HIDDENATTRIBUTES])) {
152+
$this->setHiddenAttributes($options[self::ENTRY_HIDDENATTRIBUTES]);
153+
}
154+
if (isset($options[self::ENTRY_IGNOREHIDDENATTRIBUTES])) {
155+
$this->setIgnoreHiddenAttributes($options[self::ENTRY_IGNOREHIDDENATTRIBUTES]);
136156
}
137157
if (isset($options[self::ENTRY_ISNEW])) {
138158
$this->setIsNew($options[self::ENTRY_ISNEW]);
@@ -148,7 +168,7 @@ public function __construct(array $options = null)
148168
*
149169
* @throws ClientException
150170
*
151-
* @param array $values - initial values for document
171+
* @param array $values - initial values for document
152172
* @param array $options - optional, initial options for document
153173
*
154174
* @return Document|Edge
@@ -161,7 +181,7 @@ public static function createFromArray(array $values, array $options = array())
161181
}
162182

163183
$document->setChanged(true);
164-
184+
165185
return $document;
166186
}
167187

@@ -174,7 +194,7 @@ public static function createFromArray(array $values, array $options = array())
174194
*/
175195
public function __clone()
176196
{
177-
$this->_id = null;
197+
$this->_id = null;
178198
$this->_key = null;
179199
$this->_rev = null;
180200
// do not change the _changed flag here
@@ -233,9 +253,9 @@ public function toSerialized($options = array())
233253
*
234254
* @return array - attributes array
235255
*/
236-
public function filterHiddenAttributes($attributes)
256+
public function filterHiddenAttributes($attributes, $_hiddenAttributes = null)
237257
{
238-
$hiddenAttributes = $this->getHiddenAttributes();
258+
$hiddenAttributes = $_hiddenAttributes !== null ? $_hiddenAttributes : $this->getHiddenAttributes();
239259

240260
if (is_array($hiddenAttributes)) {
241261
foreach ($hiddenAttributes as $hiddenAttributeName) {
@@ -245,7 +265,7 @@ public function filterHiddenAttributes($attributes)
245265
}
246266
}
247267

248-
unset ($attributes['_hidden']);
268+
unset ($attributes[self::ENTRY_HIDDENATTRIBUTES]);
249269

250270
return $attributes;
251271
}
@@ -259,8 +279,8 @@ public function filterHiddenAttributes($attributes)
259279
*
260280
* @throws ClientException
261281
*
262-
* @param string $key - attribute name
263-
* @param mixed $value - value for attribute
282+
* @param string $key - attribute name
283+
* @param mixed $value - value for attribute
264284
*
265285
* @return void
266286
*/
@@ -286,15 +306,15 @@ public function set($key, $value)
286306
$this->setRevision($value);
287307
return;
288308
}
289-
309+
290310
if ($key === self::ENTRY_ISNEW) {
291311
$this->setIsNew($value);
292312
return;
293313
}
294314
}
295315

296-
if (! $this->_changed) {
297-
if (! isset($this->_values[$key]) || $this->_values[$key] !== $value) {
316+
if (!$this->_changed) {
317+
if (!isset($this->_values[$key]) || $this->_values[$key] !== $value) {
298318
// set changed flag
299319
$this->_changed = true;
300320
}
@@ -313,8 +333,8 @@ public function set($key, $value)
313333
*
314334
* @throws ClientException
315335
*
316-
* @param string $key - attribute name
317-
* @param mixed $value - value for attribute
336+
* @param string $key - attribute name
337+
* @param mixed $value - value for attribute
318338
*
319339
* @return void
320340
*/
@@ -367,12 +387,14 @@ public function __get($key)
367387
public function getAll($options = array())
368388
{
369389
// This preserves compatibility for the old includeInternals parameter.
370-
$includeInternals = false;
371-
$ignoreHiddenAttributes = false;
390+
$includeInternals = false;
391+
$ignoreHiddenAttributes = $this->{self::ENTRY_IGNOREHIDDENATTRIBUTES};
392+
$_hiddenAttributes = $this->{self::ENTRY_HIDDENATTRIBUTES};
372393

373394
if (!is_array($options)) {
374395
$includeInternals = $options;
375-
} else {
396+
}
397+
else {
376398
// keeping the non-underscored version for backwards-compatibility
377399
$includeInternals = array_key_exists(
378400
'includeInternals',
@@ -391,13 +413,18 @@ public function getAll($options = array())
391413
) ? $options['ignoreHiddenAttributes'] : $ignoreHiddenAttributes;
392414

393415
$ignoreHiddenAttributes = array_key_exists(
394-
'_ignoreHiddenAttributes',
416+
self::ENTRY_IGNOREHIDDENATTRIBUTES,
417+
$options
418+
) ? $options[self::ENTRY_IGNOREHIDDENATTRIBUTES] : $ignoreHiddenAttributes;
419+
420+
$_hiddenAttributes = array_key_exists(
421+
self::ENTRY_HIDDENATTRIBUTES,
395422
$options
396-
) ? $options['_ignoreHiddenAttributes'] : $ignoreHiddenAttributes;
423+
) ? $options[self::ENTRY_HIDDENATTRIBUTES] : $_hiddenAttributes;
397424
}
398425

399-
$data = $this->_values;
400-
$nonInternals = array('_changed', '_values', '_hidden');
426+
$data = $this->_values;
427+
$nonInternals = array('_changed', '_values', self::ENTRY_HIDDENATTRIBUTES);
401428

402429
if ($includeInternals == true) {
403430
foreach ($this as $key => $value) {
@@ -407,8 +434,8 @@ public function getAll($options = array())
407434
}
408435
}
409436

410-
if ($ignoreHiddenAttributes == false) {
411-
$data = $this->filterHiddenAttributes($data);
437+
if ($ignoreHiddenAttributes === false) {
438+
$data = $this->filterHiddenAttributes($data, $_hiddenAttributes);
412439
}
413440

414441
if (!is_null($this->_key)) {
@@ -417,7 +444,7 @@ public function getAll($options = array())
417444

418445
return $data;
419446
}
420-
447+
421448
/**
422449
* Get all document attributes for insertion/update
423450
*
@@ -429,7 +456,8 @@ public function getAllForInsertUpdate()
429456
foreach ($this->_values as $key => $value) {
430457
if ($key === "_id" || $key === "_rev") {
431458
continue;
432-
} else if ($key === "_key") {
459+
}
460+
else if ($key === "_key") {
433461
if ($value === null) {
434462
// key value not yet set
435463
continue;
@@ -443,8 +471,8 @@ public function getAllForInsertUpdate()
443471

444472
return $data;
445473
}
446-
447-
474+
475+
448476
/**
449477
* Get all document attributes, and return an empty object if the documentapped into a DocumentWrapper class
450478
*
@@ -459,23 +487,23 @@ public function getAllForInsertUpdate()
459487
*/
460488
public function getAllAsObject($options = array())
461489
{
462-
$result = $this->getAll($options);
463-
if (count($result) === 0) {
464-
return new \StdClass;
465-
}
466-
return $result;
490+
$result = $this->getAll($options);
491+
if (count($result) === 0) {
492+
return new \StdClass;
493+
}
494+
return $result;
467495
}
468496

469497
/**
470498
* Set the hidden attributes
471-
*
499+
*$cursor
472500
* @param array $attributes - array of attributes
473501
*
474502
* @return void
475503
*/
476504
public function setHiddenAttributes(array $attributes)
477505
{
478-
$this->_hidden = $attributes;
506+
$this->{self::ENTRY_HIDDENATTRIBUTES} = $attributes;
479507
}
480508

481509
/**
@@ -485,7 +513,23 @@ public function setHiddenAttributes(array $attributes)
485513
*/
486514
public function getHiddenAttributes()
487515
{
488-
return $this->_hidden;
516+
return $this->{self::ENTRY_HIDDENATTRIBUTES};
517+
}
518+
519+
/**
520+
* @return boolean
521+
*/
522+
public function isIgnoreHiddenAttributes()
523+
{
524+
return $this->{self::ENTRY_IGNOREHIDDENATTRIBUTES};
525+
}
526+
527+
/**
528+
* @param boolean $ignoreHiddenAttributes
529+
*/
530+
public function setIgnoreHiddenAttributes($ignoreHiddenAttributes)
531+
{
532+
$this->{self::ENTRY_IGNOREHIDDENATTRIBUTES} = (bool) $ignoreHiddenAttributes;
489533
}
490534

491535
/**

0 commit comments

Comments
 (0)