1
0
Fork 0
mirror of synced 2024-06-03 03:14:50 +12:00
appwrite/src/Appwrite/Database/Document.php

255 lines
6.1 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
namespace Appwrite\Database;
2019-05-09 18:54:39 +12:00
use ArrayObject;
class Document extends ArrayObject
{
const SET_TYPE_ASSIGN = 'assign';
const SET_TYPE_PREPEND = 'prepend';
const SET_TYPE_APPEND = 'append';
2019-05-09 18:54:39 +12:00
/**
* Construct.
2019-05-09 18:54:39 +12:00
*
* Construct a new fields object
*
* @see ArrayObject::__construct
*
* @param array $input
* @param int $flags
2019-05-09 18:54:39 +12:00
* @param string $iterator_class
*/
public function __construct($input = [], $flags = 0, $iterator_class = 'ArrayIterator')
2019-05-09 18:54:39 +12:00
{
foreach ($input as $key => &$value) {
if (\is_array($value)) {
2020-06-24 18:05:43 +12:00
if ((isset($value['$id']) || isset($value['$collection'])) && (!$value instanceof self)) {
2019-05-09 18:54:39 +12:00
$input[$key] = new self($value);
} else {
2019-05-09 18:54:39 +12:00
foreach ($value as $childKey => $child) {
2020-06-24 18:05:43 +12:00
if ((isset($child['$id']) || isset($child['$collection'])) && (!$child instanceof self)) {
2019-05-09 18:54:39 +12:00
$value[$childKey] = new self($child);
}
}
}
}
}
parent::__construct($input, $flags, $iterator_class);
}
/**
* @return string|null
*/
2020-02-17 20:16:11 +13:00
public function getId()
2019-05-09 18:54:39 +12:00
{
2020-02-17 20:16:11 +13:00
return $this->getAttribute('$id', null);
2019-05-09 18:54:39 +12:00
}
/**
* @return string
2019-05-09 18:54:39 +12:00
*/
public function getCollection()
{
return $this->getAttribute('$collection', null);
}
/**
* @return array
*/
public function getPermissions()
{
return $this->getAttribute('$permissions', []);
}
/**
* Get Attribute.
2019-05-09 18:54:39 +12:00
*
* Method for getting a specific fields attribute. If $name is not found $default value will be returned.
*
* @param string $name
* @param mixed $default
*
2019-05-09 18:54:39 +12:00
* @return mixed
*/
public function getAttribute($name, $default = null)
{
$name = \explode('.', $name);
2019-05-09 18:54:39 +12:00
$temp = &$this;
foreach ($name as $key) {
if (!isset($temp[$key])) {
2019-05-09 18:54:39 +12:00
return $default;
}
$temp = &$temp[$key];
}
return $temp;
}
/**
* Set Attribute.
2019-05-09 18:54:39 +12:00
*
* Method for setting a specific field attribute
*
* @param string $key
* @param mixed $value
2019-05-09 18:54:39 +12:00
* @param string $type
*
2019-05-09 18:54:39 +12:00
* @return mixed
*/
public function setAttribute($key, $value, $type = self::SET_TYPE_ASSIGN)
{
switch ($type) {
case self::SET_TYPE_ASSIGN:
$this[$key] = $value;
break;
case self::SET_TYPE_APPEND:
$this[$key] = (!isset($this[$key]) || !\is_array($this[$key])) ? [] : $this[$key];
\array_push($this[$key], $value);
2019-05-09 18:54:39 +12:00
break;
case self::SET_TYPE_PREPEND:
$this[$key] = (!isset($this[$key]) || !\is_array($this[$key])) ? [] : $this[$key];
\array_unshift($this[$key], $value);
2019-05-09 18:54:39 +12:00
break;
}
return $this;
}
2020-02-11 05:15:18 +13:00
/**
* Remove Attribute.
*
* Method for removing a specific field attribute
*
* @param string $key
* @param mixed $value
* @param string $type
*
* @return mixed
*/
public function removeAttribute($key)
{
2020-06-25 09:02:27 +12:00
if (isset($this[$key])) {
2020-02-11 05:15:18 +13:00
unset($this[$key]);
}
return $this;
}
2019-05-09 18:54:39 +12:00
/**
* Search.
2019-05-09 18:54:39 +12:00
*
* Get array child by key and value match
*
* @param $key
* @param $value
* @param array|null $scope
*
2019-05-09 18:54:39 +12:00
* @return Document|Document[]|mixed|null|array
*/
public function search($key, $value, $scope = null)
{
$array = (!\is_null($scope)) ? $scope : $this;
2019-05-09 18:54:39 +12:00
if (\is_array($array) || $array instanceof self) {
if (isset($array[$key]) && $array[$key] == $value) {
2019-05-09 18:54:39 +12:00
return $array;
}
foreach ($array as $k => $v) {
if ((\is_array($v) || $v instanceof self) && (!empty($v))) {
2019-05-09 18:54:39 +12:00
$result = $this->search($key, $value, $v);
if (!empty($result)) {
2019-05-09 18:54:39 +12:00
return $result;
}
} else {
2019-05-09 18:54:39 +12:00
if ($k === $key && $v === $value) {
return $array;
}
}
}
}
if ($array === $value) {
2019-05-09 18:54:39 +12:00
return $array;
}
return;
2019-05-09 18:54:39 +12:00
}
/**
* Checks if document has data.
2019-05-09 18:54:39 +12:00
*
* @return bool
*/
public function isEmpty()
{
2020-02-17 20:16:11 +13:00
return empty($this->getId());
2019-05-09 18:54:39 +12:00
}
2020-06-24 18:05:43 +12:00
/**
* Checks if a document key is set.
*
* @param string $key
*
2020-06-24 18:05:43 +12:00
* @return bool
*/
public function isSet($key)
{
return isset($this[$key]);
}
2019-05-09 18:54:39 +12:00
/**
* Get Array Copy.
2019-05-09 18:54:39 +12:00
*
* Outputs entity as a PHP array
*
* @param array $whitelist
* @param array $blacklist
*
2019-05-09 18:54:39 +12:00
* @return array
*/
public function getArrayCopy(array $whitelist = [], array $blacklist = [])
{
$array = parent::getArrayCopy();
$output = array();
foreach ($array as $key => &$value) {
if (!empty($whitelist) && !\in_array($key, $whitelist)) { // Export only whitelisted fields
2019-05-09 18:54:39 +12:00
continue;
}
if (!empty($blacklist) && \in_array($key, $blacklist)) { // Don't export blacklisted fields
2019-05-09 18:54:39 +12:00
continue;
}
if ($value instanceof self) {
$output[$key] = $value->getArrayCopy($whitelist, $blacklist);
} elseif (\is_array($value)) {
2019-05-09 18:54:39 +12:00
foreach ($value as $childKey => &$child) {
if ($child instanceof self) {
$output[$key][$childKey] = $child->getArrayCopy($whitelist, $blacklist);
} else {
2019-05-09 18:54:39 +12:00
$output[$key][$childKey] = $child;
}
}
if (empty($value)) {
2019-05-09 18:54:39 +12:00
$output[$key] = $value;
}
} else {
2019-05-09 18:54:39 +12:00
$output[$key] = $value;
}
}
return $output;
}
}