Skip to content

Fix for issue #194 #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 83 additions & 39 deletions lib/triagens/ArangoDb/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ class Document
*
* @var bool
*/
protected $_hidden = array();
protected $_hiddenAttributes = array();

/**
* Flag to indicate whether document was changed locally
*
* @var bool
*/
protected $_ignoreHiddenAttributes = false;

/**
* Document id index
Expand All @@ -100,7 +107,12 @@ class Document
/**
* hidden attribute index
*/
const ENTRY_HIDDEN = '_hidden';
const ENTRY_HIDDENATTRIBUTES = '_hiddenAttributes';

/**
* hidden attribute index
*/
const ENTRY_IGNOREHIDDENATTRIBUTES = '_ignoreHiddenAttributes';

/**
* waitForSync option index
Expand All @@ -121,7 +133,12 @@ class Document
* Constructs an empty document
*
* @param array $options - optional, initial $options for document
*
* <p>Options are :<br>
* <li>'_hiddenAttributes' - Set an array of hidden attributes for created documents.
* <li>'hiddenAttributes' - Deprecated, please use '_hiddenAttributes'.</li>
* <li>'_ignoreHiddenAttributes' - true to show hidden attributes. Defaults to false</li>
* <p>
* *
* @return Document
*/
public function __construct(array $options = null)
Expand All @@ -131,8 +148,11 @@ public function __construct(array $options = null)
if (isset($options['hiddenAttributes'])) {
$this->setHiddenAttributes($options['hiddenAttributes']);
}
if (isset($options['_hiddenAttributes'])) {
$this->setHiddenAttributes($options['_hiddenAttributes']);
if (isset($options[self::ENTRY_HIDDENATTRIBUTES])) {
$this->setHiddenAttributes($options[self::ENTRY_HIDDENATTRIBUTES]);
}
if (isset($options[self::ENTRY_IGNOREHIDDENATTRIBUTES])) {
$this->setIgnoreHiddenAttributes($options[self::ENTRY_IGNOREHIDDENATTRIBUTES]);
}
if (isset($options[self::ENTRY_ISNEW])) {
$this->setIsNew($options[self::ENTRY_ISNEW]);
Expand All @@ -148,7 +168,7 @@ public function __construct(array $options = null)
*
* @throws ClientException
*
* @param array $values - initial values for document
* @param array $values - initial values for document
* @param array $options - optional, initial options for document
*
* @return Document|Edge
Expand All @@ -161,7 +181,7 @@ public static function createFromArray(array $values, array $options = array())
}

$document->setChanged(true);

return $document;
}

Expand All @@ -174,7 +194,7 @@ public static function createFromArray(array $values, array $options = array())
*/
public function __clone()
{
$this->_id = null;
$this->_id = null;
$this->_key = null;
$this->_rev = null;
// do not change the _changed flag here
Expand Down Expand Up @@ -233,9 +253,9 @@ public function toSerialized($options = array())
*
* @return array - attributes array
*/
public function filterHiddenAttributes($attributes)
public function filterHiddenAttributes($attributes, $_hiddenAttributes = null)
{
$hiddenAttributes = $this->getHiddenAttributes();
$hiddenAttributes = $_hiddenAttributes !== null ? $_hiddenAttributes : $this->getHiddenAttributes();

if (is_array($hiddenAttributes)) {
foreach ($hiddenAttributes as $hiddenAttributeName) {
Expand All @@ -245,7 +265,7 @@ public function filterHiddenAttributes($attributes)
}
}

unset ($attributes['_hidden']);
unset ($attributes[self::ENTRY_HIDDENATTRIBUTES]);

return $attributes;
}
Expand All @@ -259,8 +279,8 @@ public function filterHiddenAttributes($attributes)
*
* @throws ClientException
*
* @param string $key - attribute name
* @param mixed $value - value for attribute
* @param string $key - attribute name
* @param mixed $value - value for attribute
*
* @return void
*/
Expand All @@ -286,15 +306,15 @@ public function set($key, $value)
$this->setRevision($value);
return;
}

if ($key === self::ENTRY_ISNEW) {
$this->setIsNew($value);
return;
}
}

if (! $this->_changed) {
if (! isset($this->_values[$key]) || $this->_values[$key] !== $value) {
if (!$this->_changed) {
if (!isset($this->_values[$key]) || $this->_values[$key] !== $value) {
// set changed flag
$this->_changed = true;
}
Expand All @@ -313,8 +333,8 @@ public function set($key, $value)
*
* @throws ClientException
*
* @param string $key - attribute name
* @param mixed $value - value for attribute
* @param string $key - attribute name
* @param mixed $value - value for attribute
*
* @return void
*/
Expand Down Expand Up @@ -367,12 +387,14 @@ public function __get($key)
public function getAll($options = array())
{
// This preserves compatibility for the old includeInternals parameter.
$includeInternals = false;
$ignoreHiddenAttributes = false;
$includeInternals = false;
$ignoreHiddenAttributes = $this->{self::ENTRY_IGNOREHIDDENATTRIBUTES};
$_hiddenAttributes = $this->{self::ENTRY_HIDDENATTRIBUTES};

if (!is_array($options)) {
$includeInternals = $options;
} else {
}
else {
// keeping the non-underscored version for backwards-compatibility
$includeInternals = array_key_exists(
'includeInternals',
Expand All @@ -391,13 +413,18 @@ public function getAll($options = array())
) ? $options['ignoreHiddenAttributes'] : $ignoreHiddenAttributes;

$ignoreHiddenAttributes = array_key_exists(
'_ignoreHiddenAttributes',
self::ENTRY_IGNOREHIDDENATTRIBUTES,
$options
) ? $options[self::ENTRY_IGNOREHIDDENATTRIBUTES] : $ignoreHiddenAttributes;

$_hiddenAttributes = array_key_exists(
self::ENTRY_HIDDENATTRIBUTES,
$options
) ? $options['_ignoreHiddenAttributes'] : $ignoreHiddenAttributes;
) ? $options[self::ENTRY_HIDDENATTRIBUTES] : $_hiddenAttributes;
}

$data = $this->_values;
$nonInternals = array('_changed', '_values', '_hidden');
$data = $this->_values;
$nonInternals = array('_changed', '_values', self::ENTRY_HIDDENATTRIBUTES);

if ($includeInternals == true) {
foreach ($this as $key => $value) {
Expand All @@ -407,8 +434,8 @@ public function getAll($options = array())
}
}

if ($ignoreHiddenAttributes == false) {
$data = $this->filterHiddenAttributes($data);
if ($ignoreHiddenAttributes === false) {
$data = $this->filterHiddenAttributes($data, $_hiddenAttributes);
}

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

return $data;
}

/**
* Get all document attributes for insertion/update
*
Expand All @@ -429,7 +456,8 @@ public function getAllForInsertUpdate()
foreach ($this->_values as $key => $value) {
if ($key === "_id" || $key === "_rev") {
continue;
} else if ($key === "_key") {
}
else if ($key === "_key") {
if ($value === null) {
// key value not yet set
continue;
Expand All @@ -443,8 +471,8 @@ public function getAllForInsertUpdate()

return $data;
}


/**
* Get all document attributes, and return an empty object if the documentapped into a DocumentWrapper class
*
Expand All @@ -459,23 +487,23 @@ public function getAllForInsertUpdate()
*/
public function getAllAsObject($options = array())
{
$result = $this->getAll($options);
if (count($result) === 0) {
return new \StdClass;
}
return $result;
$result = $this->getAll($options);
if (count($result) === 0) {
return new \StdClass;
}
return $result;
}

/**
* Set the hidden attributes
*
*$cursor
* @param array $attributes - array of attributes
*
* @return void
*/
public function setHiddenAttributes(array $attributes)
{
$this->_hidden = $attributes;
$this->{self::ENTRY_HIDDENATTRIBUTES} = $attributes;
}

/**
Expand All @@ -485,7 +513,23 @@ public function setHiddenAttributes(array $attributes)
*/
public function getHiddenAttributes()
{
return $this->_hidden;
return $this->{self::ENTRY_HIDDENATTRIBUTES};
}

/**
* @return boolean
*/
public function isIgnoreHiddenAttributes()
{
return $this->{self::ENTRY_IGNOREHIDDENATTRIBUTES};
}

/**
* @param boolean $ignoreHiddenAttributes
*/
public function setIgnoreHiddenAttributes($ignoreHiddenAttributes)
{
$this->{self::ENTRY_IGNOREHIDDENATTRIBUTES} = (bool) $ignoreHiddenAttributes;
}

/**
Expand Down
Loading