mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-23 00:36:52 +01:00
Merge branch 'master' of /home/rdb/DEV/PhpstormProjects/sysPass with conflicts.
This commit is contained in:
312
lib/SP/Core/DataCollection.php
Normal file
312
lib/SP/Core/DataCollection.php
Normal file
@@ -0,0 +1,312 @@
|
||||
<?php
|
||||
/**
|
||||
* sysPass
|
||||
*
|
||||
* @author nuxsmin
|
||||
* @link https://syspass.org
|
||||
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
|
||||
*
|
||||
* This file is part of sysPass.
|
||||
*
|
||||
* sysPass is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* sysPass is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace SP\Core;
|
||||
|
||||
use ArrayAccess;
|
||||
use ArrayIterator;
|
||||
use Countable;
|
||||
use IteratorAggregate;
|
||||
use Traversable;
|
||||
|
||||
/**
|
||||
* Class DataCollection
|
||||
*
|
||||
* @package SP\Core\Context
|
||||
*/
|
||||
abstract class DataCollection implements IteratorAggregate, ArrayAccess, Countable
|
||||
{
|
||||
/**
|
||||
* Collection of data attributes
|
||||
*
|
||||
* @type array
|
||||
*/
|
||||
protected $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()
|
||||
{
|
||||
return new ArrayIterator($this->attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
return $this->exists($offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* See if an attribute exists in the collection
|
||||
*
|
||||
* @param string $key The name of the parameter
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function exists($key)
|
||||
{
|
||||
// Don't use "isset", since it returns false for null values
|
||||
return array_key_exists($key, $this->attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
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($key, $default_val = null)
|
||||
{
|
||||
if (isset($this->attributes[$key])) {
|
||||
return $this->attributes[$key];
|
||||
}
|
||||
|
||||
return $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)
|
||||
{
|
||||
$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($key, $value)
|
||||
{
|
||||
$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)
|
||||
{
|
||||
$this->remove($offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an attribute from the collection
|
||||
*
|
||||
* @param string $key The name of the parameter
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function remove($key)
|
||||
{
|
||||
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()
|
||||
{
|
||||
return count($this->attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the collection's contents
|
||||
*
|
||||
* Semantic alias of a no-argument `$this->replace` call
|
||||
*
|
||||
* @return DataCollection
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
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())
|
||||
{
|
||||
$this->attributes = $attributes;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the collection is empty
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isEmpty()
|
||||
{
|
||||
return empty($this->attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic "__get" method
|
||||
*
|
||||
* Allows the ability to arbitrarily request an attribute from
|
||||
* this instance while treating it as an instance property
|
||||
*
|
||||
* @see get()
|
||||
*
|
||||
* @param string $key The name of the parameter to return
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($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
|
||||
*
|
||||
* @see set()
|
||||
*
|
||||
* @param string $key The name of the parameter to set
|
||||
* @param mixed $value The value of the parameter to set
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __set($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
|
||||
*
|
||||
* @see exists()
|
||||
*
|
||||
* @param string $key The name of the parameter
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function __isset($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
|
||||
*
|
||||
* @see remove()
|
||||
*
|
||||
* @param string $key The name of the parameter
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __unset($key)
|
||||
{
|
||||
$this->remove($key);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user