mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-23 00:36:52 +01:00
chore: Implement AccountAclService tests.
Use scoped namespace for simple functions. Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
@@ -24,280 +24,32 @@
|
||||
|
||||
namespace SP\Core;
|
||||
|
||||
use ArrayAccess;
|
||||
use ArrayIterator;
|
||||
use Countable;
|
||||
use IteratorAggregate;
|
||||
use Traversable;
|
||||
use ArrayObject;
|
||||
|
||||
/**
|
||||
* Class DataCollection
|
||||
*
|
||||
* @package SP\Core\Context
|
||||
*/
|
||||
abstract class DataCollection implements IteratorAggregate, ArrayAccess, Countable
|
||||
abstract class DataCollection extends ArrayObject
|
||||
{
|
||||
/**
|
||||
* Collection of data attributes
|
||||
*/
|
||||
protected array $attributes = [];
|
||||
|
||||
/**
|
||||
* Retrieve an external iterator
|
||||
*
|
||||
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
|
||||
* @return Traversable An instance of an object implementing <b>Iterator</b> or
|
||||
* <b>Traversable</b>
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public function getIterator(): Traversable
|
||||
public function get(string $key, mixed $default = null): mixed
|
||||
{
|
||||
return new ArrayIterator($this->attributes);
|
||||
if (!property_exists($this, $key)) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
return $this->{$key};
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a offset exists
|
||||
*
|
||||
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
|
||||
*
|
||||
* @param mixed $offset <p>
|
||||
* An offset to check for.
|
||||
* </p>
|
||||
*
|
||||
* @return boolean true on success or false on failure.
|
||||
* </p>
|
||||
* <p>
|
||||
* The return value will be casted to boolean if non-boolean was returned.
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public function offsetExists($offset): bool
|
||||
public function set(string $key, mixed $value): void
|
||||
{
|
||||
return $this->exists($offset);
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* See if an attribute exists in the collection
|
||||
*
|
||||
* @param string $key The name of the parameter
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function exists(string $key): bool
|
||||
{
|
||||
// Don't use "isset", since it returns false for null values
|
||||
return array_key_exists($key, $this->attributes);
|
||||
return property_exists($this, $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Offset to retrieve
|
||||
*
|
||||
* @link http://php.net/manual/en/arrayaccess.offsetget.php
|
||||
*
|
||||
* @param mixed $offset <p>
|
||||
* The offset to retrieve.
|
||||
* </p>
|
||||
*
|
||||
* @return mixed Can return all value types.
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public function offsetGet($offset): mixed
|
||||
{
|
||||
return $this->get($offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an attribute of the collection
|
||||
*
|
||||
* Return a default value if the key doesn't exist
|
||||
*
|
||||
* @param string $key The name of the parameter to return
|
||||
* @param mixed $default_val The default value of the parameter if it contains no value
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $key, $default_val = null)
|
||||
{
|
||||
return $this->attributes[$key] ?? $default_val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Offset to set
|
||||
*
|
||||
* @link http://php.net/manual/en/arrayaccess.offsetset.php
|
||||
*
|
||||
* @param mixed $offset <p>
|
||||
* The offset to assign the value to.
|
||||
* </p>
|
||||
* @param mixed $value <p>
|
||||
* The value to set.
|
||||
* </p>
|
||||
*
|
||||
* @return void
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public function offsetSet($offset, $value): void
|
||||
{
|
||||
$this->set($offset, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an attribute of the collection
|
||||
*
|
||||
* @param string $key The name of the parameter to set
|
||||
* @param mixed $value The value of the parameter to set
|
||||
*
|
||||
* @return DataCollection
|
||||
*/
|
||||
public function set(string $key, $value): DataCollection
|
||||
{
|
||||
$this->attributes[$key] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Offset to unset
|
||||
*
|
||||
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
|
||||
*
|
||||
* @param mixed $offset <p>
|
||||
* The offset to unset.
|
||||
* </p>
|
||||
*
|
||||
* @return void
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public function offsetUnset($offset): void
|
||||
{
|
||||
$this->remove($offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an attribute from the collection
|
||||
*
|
||||
* @param string $key The name of the parameter
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function remove(string $key): void
|
||||
{
|
||||
unset($this->attributes[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Count elements of an object
|
||||
*
|
||||
* @link http://php.net/manual/en/countable.count.php
|
||||
* @return int The custom count as an integer.
|
||||
* </p>
|
||||
* <p>
|
||||
* The return value is cast to an integer.
|
||||
* @since 5.1.0
|
||||
*/
|
||||
public function count(): int
|
||||
{
|
||||
return count($this->attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the collection's contents
|
||||
*
|
||||
* Semantic alias of a no-argument `$this->replace` call
|
||||
*
|
||||
* @return DataCollection
|
||||
*/
|
||||
public function clear(): DataCollection
|
||||
{
|
||||
return $this->replace();
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the collection's attributes
|
||||
*
|
||||
* @param array $attributes The attributes to replace the collection's with
|
||||
*
|
||||
* @return DataCollection
|
||||
*/
|
||||
public function replace(array $attributes = array()): DataCollection
|
||||
{
|
||||
$this->attributes = $attributes;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the collection is empty
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
return count($this->attributes) === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic "__get" method
|
||||
*
|
||||
* Allows the ability to arbitrarily request an attribute from
|
||||
* this instance while treating it as an instance property
|
||||
*
|
||||
* @param string $key The name of the parameter to return
|
||||
*
|
||||
* @return mixed
|
||||
* @see get()
|
||||
*/
|
||||
public function __get(string $key)
|
||||
{
|
||||
return $this->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic "__set" method
|
||||
*
|
||||
* Allows the ability to arbitrarily set an attribute from
|
||||
* this instance while treating it as an instance property
|
||||
*
|
||||
* @param string $key The name of the parameter to set
|
||||
* @param mixed $value The value of the parameter to set
|
||||
*
|
||||
* @return void
|
||||
* @see set()
|
||||
*/
|
||||
public function __set(string $key, $value)
|
||||
{
|
||||
$this->set($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic "__isset" method
|
||||
*
|
||||
* Allows the ability to arbitrarily check the existence of an attribute
|
||||
* from this instance while treating it as an instance property
|
||||
*
|
||||
* @param string $key The name of the parameter
|
||||
*
|
||||
* @return boolean
|
||||
* @see exists()
|
||||
*/
|
||||
public function __isset(string $key)
|
||||
{
|
||||
return $this->exists($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic "__unset" method
|
||||
*
|
||||
* Allows the ability to arbitrarily remove an attribute from
|
||||
* this instance while treating it as an instance property
|
||||
*
|
||||
* @param string $key The name of the parameter
|
||||
*
|
||||
* @return void
|
||||
* @see remove()
|
||||
*
|
||||
*/
|
||||
public function __unset(string $key)
|
||||
{
|
||||
$this->remove($key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user