Extract AssociativeCollection class

This commit is contained in:
Giuseppe Criscione 2019-05-07 17:00:07 +02:00
parent 744b7dc2b7
commit 2af0118846
5 changed files with 39 additions and 112 deletions

View File

@ -2,10 +2,10 @@
namespace Formwork\Admin\Fields;
use Formwork\Data\Collection;
use Formwork\Data\AssociativeCollection;
use Formwork\Data\DataGetter;
class Fields extends Collection
class Fields extends AssociativeCollection
{
/**
* Create a new Fields instance
@ -19,32 +19,6 @@ class Fields extends Collection
}
}
/**
* Return whether a field is in the collection
*
* @param string $field Field name
*
* @return bool
*/
public function has($field)
{
return isset($this->items[$field]);
}
/**
* Get a field by name
*
* @param string $field Field name
*
* @return Field
*/
public function get($field)
{
if ($this->has($field)) {
return $this->items[$field];
}
}
/**
* Recursively find a field by name
*

View File

@ -2,11 +2,11 @@
namespace Formwork\Admin\Users;
use Formwork\Data\Collection;
use Formwork\Data\AssociativeCollection;
use Formwork\Parsers\YAML;
use Formwork\Utils\FileSystem;
class Users extends Collection
class Users extends AssociativeCollection
{
/**
* All available roles
@ -15,32 +15,6 @@ class Users extends Collection
*/
protected static $roles = array();
/**
* Return whether a user is in the collection
*
* @param string $user
*
* @return bool
*/
public function has($user)
{
return isset($this->items[$user]);
}
/**
* Get a user by name
*
* @param string $user
*
* @return User
*/
public function get($user)
{
if ($this->has($user)) {
return $this->items[$user];
}
}
/**
* Load all users and roles
*

View File

@ -0,0 +1,31 @@
<?php
namespace Formwork\Data;
use Formwork\Utils\Arr;
class AssociativeCollection extends Collection
{
/**
* Get data by key returning a default value if key is not present
*
* @param string $key
* @param mixed|null $default
*/
public function get($key, $default = null)
{
return Arr::get($this->items, $key, $default);
}
/**
* Return whether a key is present
*
* @param string $key
*
* @return bool
*/
public function has($key)
{
return Arr::has($this->items, $key);
}
}

View File

@ -2,10 +2,10 @@
namespace Formwork\Files;
use Formwork\Data\Collection;
use Formwork\Data\AssociativeCollection;
use Formwork\Utils\FileSystem;
class Files extends Collection
class Files extends AssociativeCollection
{
/**
* Root path of the file collection
@ -28,32 +28,6 @@ class Files extends Collection
}
}
/**
* Return whether a file is present in the collection
*
* @param string $file
*
* @return bool
*/
public function has($file)
{
return isset($this->items[$file]);
}
/**
* Get a file from the collection
*
* @param string $file
*
* @return File|null
*/
public function get($file)
{
if ($this->has($file)) {
return $this->items[$file];
}
}
/**
* Get files path
*

View File

@ -2,9 +2,9 @@
namespace Formwork\Metadata;
use Formwork\Data\Collection;
use Formwork\Data\AssociativeCollection;
class Metadata extends Collection
class Metadata extends AssociativeCollection
{
/**
* Create a new Metadata instance
@ -14,32 +14,6 @@ class Metadata extends Collection
$this->setMultiple($items);
}
/**
* Return whether a metadatum in the collection
*
* @param string $name
*
* @return bool
*/
public function has($name)
{
return isset($this->items[$name]);
}
/**
* Return a metadatum by name
*
* @param string $name
*
* @return Metadatum
*/
public function get($name)
{
if ($this->has($name)) {
return $this->items[$name];
}
}
/**
* Set a metadatum
*