Update Composer, update everything

This commit is contained in:
Oliver Davies 2018-11-23 12:29:20 +00:00
parent ea3e94409f
commit dda5c284b6
19527 changed files with 1135420 additions and 351004 deletions

View file

@ -0,0 +1,220 @@
<?php
/*
* This file is a part of dflydev/dot-access-data.
*
* (c) Dragonfly Development Inc.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Dflydev\DotAccessData;
class Data implements DataInterface
{
/**
* Internal representation of data data
*
* @var array
*/
protected $data;
/**
* Constructor
*
* @param array|null $data
*/
public function __construct(array $data = null)
{
$this->data = $data ?: array();
}
/**
* {@inheritdoc}
*/
public function append($key, $value = null)
{
if (0 == strlen($key)) {
throw new \RuntimeException("Key cannot be an empty string");
}
$currentValue =& $this->data;
$keyPath = explode('.', $key);
if (1 == count($keyPath)) {
if (!isset($currentValue[$key])) {
$currentValue[$key] = array();
}
if (!is_array($currentValue[$key])) {
// Promote this key to an array.
// TODO: Is this really what we want to do?
$currentValue[$key] = array($currentValue[$key]);
}
$currentValue[$key][] = $value;
return;
}
$endKey = array_pop($keyPath);
for ( $i = 0; $i < count($keyPath); $i++ ) {
$currentKey =& $keyPath[$i];
if ( ! isset($currentValue[$currentKey]) ) {
$currentValue[$currentKey] = array();
}
$currentValue =& $currentValue[$currentKey];
}
if (!isset($currentValue[$endKey])) {
$currentValue[$endKey] = array();
}
if (!is_array($currentValue[$endKey])) {
$currentValue[$endKey] = array($currentValue[$endKey]);
}
// Promote this key to an array.
// TODO: Is this really what we want to do?
$currentValue[$endKey][] = $value;
}
/**
* {@inheritdoc}
*/
public function set($key, $value = null)
{
if (0 == strlen($key)) {
throw new \RuntimeException("Key cannot be an empty string");
}
$currentValue =& $this->data;
$keyPath = explode('.', $key);
if (1 == count($keyPath)) {
$currentValue[$key] = $value;
return;
}
$endKey = array_pop($keyPath);
for ( $i = 0; $i < count($keyPath); $i++ ) {
$currentKey =& $keyPath[$i];
if (!isset($currentValue[$currentKey])) {
$currentValue[$currentKey] = array();
}
if (!is_array($currentValue[$currentKey])) {
throw new \RuntimeException("Key path at $currentKey of $key cannot be indexed into (is not an array)");
}
$currentValue =& $currentValue[$currentKey];
}
$currentValue[$endKey] = $value;
}
/**
* {@inheritdoc}
*/
public function remove($key)
{
if (0 == strlen($key)) {
throw new \RuntimeException("Key cannot be an empty string");
}
$currentValue =& $this->data;
$keyPath = explode('.', $key);
if (1 == count($keyPath)) {
unset($currentValue[$key]);
return;
}
$endKey = array_pop($keyPath);
for ( $i = 0; $i < count($keyPath); $i++ ) {
$currentKey =& $keyPath[$i];
if (!isset($currentValue[$currentKey])) {
return;
}
$currentValue =& $currentValue[$currentKey];
}
unset($currentValue[$endKey]);
}
/**
* {@inheritdoc}
*/
public function get($key, $default = null)
{
$currentValue = $this->data;
$keyPath = explode('.', $key);
for ( $i = 0; $i < count($keyPath); $i++ ) {
$currentKey = $keyPath[$i];
if (!isset($currentValue[$currentKey]) ) {
return $default;
}
if (!is_array($currentValue)) {
return $default;
}
$currentValue = $currentValue[$currentKey];
}
return $currentValue === null ? $default : $currentValue;
}
/**
* {@inheritdoc}
*/
public function has($key)
{
$currentValue = &$this->data;
$keyPath = explode('.', $key);
for ( $i = 0; $i < count($keyPath); $i++ ) {
$currentKey = $keyPath[$i];
if (
!is_array($currentValue) ||
!array_key_exists($currentKey, $currentValue)
) {
return false;
}
$currentValue = &$currentValue[$currentKey];
}
return true;
}
/**
* {@inheritdoc}
*/
public function getData($key)
{
$value = $this->get($key);
if (is_array($value) && Util::isAssoc($value)) {
return new Data($value);
}
throw new \RuntimeException("Value at '$key' could not be represented as a DataInterface");
}
/**
* {@inheritdoc}
*/
public function import(array $data, $clobber = true)
{
$this->data = Util::mergeAssocArray($this->data, $data, $clobber);
}
/**
* {@inheritdoc}
*/
public function importData(DataInterface $data, $clobber = true)
{
$this->import($data->export(), $clobber);
}
/**
* {@inheritdoc}
*/
public function export()
{
return $this->data;
}
}

View file

@ -0,0 +1,89 @@
<?php
/*
* This file is a part of dflydev/dot-access-data.
*
* (c) Dragonfly Development Inc.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Dflydev\DotAccessData;
interface DataInterface
{
/**
* Append a value to a key (assumes key refers to an array value)
*
* @param string $key
* @param mixed $value
*/
public function append($key, $value = null);
/**
* Set a value for a key
*
* @param string $key
* @param mixed $value
*/
public function set($key, $value = null);
/**
* Remove a key
*
* @param string $key
*/
public function remove($key);
/**
* Get the raw value for a key
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function get($key, $default = null);
/**
* Check if the key exists
*
* @param string $key
*
* @return bool
*/
public function has($key);
/**
* Get a data instance for a key
*
* @param string $key
*
* @return DataInterface
*/
public function getData($key);
/**
* Import data into existing data
*
* @param array $data
* @param bool $clobber
*/
public function import(array $data, $clobber = true);
/**
* Import data from an external data into existing data
*
* @param DataInterface $data
* @param bool $clobber
*/
public function importData(DataInterface $data, $clobber = true);
/**
* Export data as raw data
*
* @return array
*/
public function export();
}

View file

@ -0,0 +1,54 @@
<?php
/*
* This file is a part of dflydev/dot-access-data.
*
* (c) Dragonfly Development Inc.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Dflydev\DotAccessData;
class Util
{
/**
* Test if array is an associative array
*
* Note that this function will return true if an array is empty. Meaning
* empty arrays will be treated as if they are associative arrays.
*
* @param array $arr
*
* @return boolean
*/
public static function isAssoc(array $arr)
{
return (is_array($arr) && (!count($arr) || count(array_filter(array_keys($arr),'is_string')) == count($arr)));
}
/**
* Merge contents from one associtative array to another
*
* @param array $to
* @param array $from
* @param bool $clobber
*/
public static function mergeAssocArray($to, $from, $clobber = true)
{
if ( is_array($from) ) {
foreach ($from as $k => $v) {
if (!isset($to[$k])) {
$to[$k] = $v;
} else {
$to[$k] = self::mergeAssocArray($to[$k], $v, $clobber);
}
}
return $to;
}
return $clobber ? $from : $to;
}
}