mirror of
https://github.com/SuiteCRM/SuiteCRM-Core.git
synced 2025-08-29 17:46:02 +08:00
SuiteCRM 8 initial commit
This commit is contained in:
commit
c895877b7e
547 changed files with 40449 additions and 0 deletions
127
core/base/Helper/Data/Collection.php
Normal file
127
core/base/Helper/Data/Collection.php
Normal file
|
@ -0,0 +1,127 @@
|
|||
<?php
|
||||
|
||||
namespace SuiteCRM\Core\Base\Helper\Data;
|
||||
|
||||
/**
|
||||
* Class Collection
|
||||
* @package SuiteCRM\Core\Base\Helper\Data
|
||||
*/
|
||||
class Collection implements CollectionInterface
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $data = [];
|
||||
|
||||
/**
|
||||
* Load the collection
|
||||
*
|
||||
* @param array $data Array of data to collect
|
||||
*/
|
||||
public function __construct($data = [])
|
||||
{
|
||||
return $this->load($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursive function to collect the collection data
|
||||
*
|
||||
* @param array $data The collection data
|
||||
* @param string $parent_key The parent keys attached to the child array
|
||||
* @return bool
|
||||
*/
|
||||
public function load($data, $parent_key = ''): bool
|
||||
{
|
||||
if (empty($data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$parentKey = ($parent_key !== '') ? $parent_key . '.' : '';
|
||||
|
||||
foreach ($data as $key => $val) {
|
||||
if (is_array($val)) {
|
||||
$this->load($data[$key], $parentKey . $key);
|
||||
}
|
||||
|
||||
$this->data[$parentKey . $key] = $val;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all config variable
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAll(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get collection data
|
||||
*
|
||||
* @param string $key
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
return ($this->has($key)) ? $this->data[$key] : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set collection data
|
||||
*
|
||||
* @param string $key Name of parameter to set
|
||||
* @param mixed $value Value to set
|
||||
*/
|
||||
public function set($key, $value): void
|
||||
{
|
||||
$this->data[$key] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find out if parameter exists
|
||||
*
|
||||
* @param string $key Name to find out
|
||||
* @return bool
|
||||
*/
|
||||
public function has($key): bool
|
||||
{
|
||||
return isset($this->data[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Count collection entry for key
|
||||
*
|
||||
* @param string|null $key The config key you want to count
|
||||
* @return int
|
||||
*/
|
||||
public function count($key = null): int
|
||||
{
|
||||
return ($key === null) ? count($this->data) : count($this->data[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the collection key is empty
|
||||
*
|
||||
* @param string $key
|
||||
* @return bool true if empty - false - if not
|
||||
*/
|
||||
public function isEmpty($key): bool
|
||||
{
|
||||
$has = $this->has($key);
|
||||
|
||||
if ($has) {
|
||||
$count = $this->count($key);
|
||||
|
||||
if ($count > 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
50
core/base/Helper/Data/CollectionInterface.php
Normal file
50
core/base/Helper/Data/CollectionInterface.php
Normal file
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace SuiteCRM\Core\Base\Helper\Data;
|
||||
|
||||
/**
|
||||
* Interface CollectionInterface
|
||||
* @package SuiteCRM\Core\Base\Helper\Data
|
||||
*/
|
||||
interface CollectionInterface
|
||||
{
|
||||
/**
|
||||
* @param $data
|
||||
* @param string $parent_key
|
||||
* @return mixed
|
||||
*/
|
||||
public function load($data, $parent_key = '');
|
||||
|
||||
public function getAll();
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key);
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return mixed
|
||||
*/
|
||||
public function set($key, $value);
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function has($key);
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function count($key);
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function isEmpty($key);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue