Release of v4.0.1-alpha5

Add repositories for better integration with gitea. Refactored the Data classes. Add new Data classes.
This commit is contained in:
2024-06-21 01:37:13 +02:00
parent f36922f9a1
commit 5a2e137f31
120 changed files with 8945 additions and 3699 deletions

View File

@@ -0,0 +1,112 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 4th September, 2022
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VDM\Joomla\Data\Action;
use VDM\Joomla\Interfaces\DeleteInterface as Database;
use VDM\Joomla\Interfaces\Data\DeleteInterface;
/**
* Data Delete
*
* @since 3.2.2
*/
class Delete implements DeleteInterface
{
/**
* The Delete Class.
*
* @var Database
* @since 3.2.2
*/
protected Database $database;
/**
* Table Name
*
* @var string
* @since 3.2.2
*/
protected string $table;
/**
* Constructor.
*
* @param Database $database The Delete Class.
* @param string|null $table The table name.
*
* @since 3.2.2
*/
public function __construct(Database $database, ?string $table = null)
{
$this->database = $database;
if ($table !== null)
{
$this->table = $table;
}
}
/**
* Set the current active table
*
* @param string|null $table The table that should be active
*
* @return self
* @since 3.2.2
*/
public function table(?string $table): self
{
if ($table !== null)
{
$this->table = $table;
}
return $this;
}
/**
* Delete all items in the database that match these conditions
*
* @param array $conditions Conditions by which to delete the data in database [array of arrays (key => value)]
*
* @return bool
* @since 3.2.2
**/
public function items(array $conditions): bool
{
return $this->database->items($conditions, $this->getTable());
}
/**
* Truncate a table
*
* @return void
* @since 3.2.2
**/
public function truncate(): void
{
$this->database->truncate($this->getTable());
}
/**
* Get the current active table
*
* @return string
* @since 3.2.2
*/
public function getTable(): string
{
return $this->table;
}
}

View File

@@ -0,0 +1,202 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 4th September, 2022
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VDM\Joomla\Data\Action;
use VDM\Joomla\Interfaces\ModelInterface as Model;
use VDM\Joomla\Interfaces\InsertInterface as Database;
use VDM\Joomla\Interfaces\Data\InsertInterface;
/**
* Data Insert (GUID)
*
* @since 3.2.2
*/
class Insert implements InsertInterface
{
/**
* Model
*
* @var Model
* @since 3.2.0
*/
protected Model $model;
/**
* Database
*
* @var Database
* @since 3.2.0
*/
protected Database $database;
/**
* Table Name
*
* @var string
* @since 3.2.1
*/
protected string $table;
/**
* Constructor
*
* @param Model $model The set model object.
* @param Database $database The insert database object.
* @param string|null $table The table name.
*
* @since 3.2.2
*/
public function __construct(Model $model, Database $database, ?string $table = null)
{
$this->model = $model;
$this->database = $database;
if ($table !== null)
{
$this->table = $table;
}
}
/**
* Set the current active table
*
* @param string|null $table The table that should be active
*
* @return self
* @since 3.2.2
*/
public function table(?string $table): self
{
if ($table !== null)
{
$this->table = $table;
}
return $this;
}
/**
* Insert a value to a given table
* Example: $this->value(Value, 'value_key', 'GUID');
*
* @param mixed $value The field value
* @param string $field The field key
* @param string $keyValue The key value
* @param string $key The key name
*
* @return bool
* @since 3.2.0
*/
public function value($value, string $field, string $keyValue, string $key = 'guid'): bool
{
// build the array
$item = [];
$item[$key] = $keyValue;
$item[$field] = $value;
// Insert the column of this table
return $this->row($item);
}
/**
* Insert single row with multiple values to a given table
* Example: $this->item(Array);
*
* @param array $item The item to save
*
* @return bool
* @since 3.2.0
*/
public function row(array $item): bool
{
// check if object could be modelled
if (($item = $this->model->row($item, $this->getTable())) !== null)
{
// Insert the column of this table
return $this->database->row($item, $this->getTable());
}
return false;
}
/**
* Insert multiple rows to a given table
* Example: $this->items(Array);
*
* @param array|null $items The items updated in database (array of arrays)
*
* @return bool
* @since 3.2.0
*/
public function rows(?array $items): bool
{
// check if object could be modelled
if (($items = $this->model->rows($items, $this->getTable())) !== null)
{
// Insert the column of this table
return $this->database->rows($items, $this->getTable());
}
return false;
}
/**
* Insert single item with multiple values to a given table
* Example: $this->item(Object);
*
* @param object $item The item to save
*
* @return bool
* @since 3.2.0
*/
public function item(object $item): bool
{
// check if object could be modelled
if (($item = $this->model->item($item, $this->getTable())) !== null)
{
// Insert the column of this table
return $this->database->item($item, $this->getTable());
}
return false;
}
/**
* Insert multiple items to a given table
* Example: $this->items(Array);
*
* @param array|null $items The items updated in database (array of objects)
*
* @return bool
* @since 3.2.0
*/
public function items(?array $items): bool
{
// check if object could be modelled
if (($items = $this->model->items($items, $this->getTable())) !== null)
{
// Update the column of this table using guid as the primary key.
return $this->database->items($items, $this->getTable());
}
return false;
}
/**
* Get the current active table
*
* @return string
* @since 3.2.2
*/
public function getTable(): string
{
return $this->table;
}
}

View File

@@ -0,0 +1,197 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 4th September, 2022
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VDM\Joomla\Data\Action;
use VDM\Joomla\Interfaces\ModelInterface as Model;
use VDM\Joomla\Interfaces\LoadInterface as Database;
use VDM\Joomla\Interfaces\Data\LoadInterface;
/**
* Data Load (GUID)
*
* @since 3.2.2
*/
class Load implements LoadInterface
{
/**
* Model Load
*
* @var Model
* @since 2.0.1
*/
protected Model $model;
/**
* Database Load
*
* @var Database
* @since 2.0.1
*/
protected Database $load;
/**
* Table Name
*
* @var string
* @since 3.2.1
*/
protected string $table;
/**
* Constructor
*
* @param Model $model The model object.
* @param Database $load The database object.
* @param string|null $table The table name.
*
* @since 2.0.1
*/
public function __construct(Model $model, Database $load, ?string $table = null)
{
$this->model = $model;
$this->load = $load;
if ($table !== null)
{
$this->table = $table;
}
}
/**
* Set the current active table
*
* @param string|null $table The table that should be active
*
* @return self
* @since 3.2.2
*/
public function table(?string $table): self
{
if ($table !== null)
{
$this->table = $table;
}
return $this;
}
/**
* Get a value from a given table
* Example: $this->value(
* [
* 'guid' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
* ], 'value_key'
* );
*
* @param array $keys The item keys
* @param string $field The field key
*
* @return mixed
* @since 2.0.1
*/
public function value(array $keys, string $field)
{
return $this->model->value(
$this->load->value(
["a.{$field}" => $field],
['a' => $this->getTable()],
$this->prefix($keys)
),
$field,
$this->getTable()
);
}
/**
* Get values from a given table
* Example: $this->item(
* [
* 'guid' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
* ]
* );
*
* @param array $keys The item keys
*
* @return object|null
* @since 2.0.1
*/
public function item(array $keys): ?object
{
return $this->model->item(
$this->load->item(
['all' => 'a.*'],
['a' => $this->getTable()],
$this->prefix($keys)
),
$this->getTable()
);
}
/**
* Get values from a given table
* Example: $this->items(
* [
* 'guid' => [
* 'operator' => 'IN',
* 'value' => [''xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'', ''xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'']
* ]
* ]
* );
* Example: $this->items($ids);
*
* @param array $keys The item keys
*
* @return array|null
* @since 2.0.1
*/
public function items(array $keys): ?array
{
return $this->model->items(
$this->load->items(
['all' => 'a.*'], ['a' => $this->getTable()], $this->prefix($keys)
),
$this->getTable()
);
}
/**
* Get the current active table
*
* @return string
* @since 3.2.2
*/
public function getTable(): string
{
return $this->table;
}
/**
* Add prefix to the keys
*
* @param array $keys The query keys
*
* @return array
* @since 2.0.1
*/
private function prefix(array &$keys): array
{
// update the key values
$bucket = [];
foreach ($keys as $k => $v)
{
$bucket['a.' . $k] = $v;
}
return $bucket;
}
}

View File

@@ -0,0 +1,206 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 4th September, 2022
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VDM\Joomla\Data\Action;
use VDM\Joomla\Interfaces\ModelInterface as Model;
use VDM\Joomla\Interfaces\UpdateInterface as Database;
use VDM\Joomla\Interfaces\Data\UpdateInterface;
/**
* Data Update
*
* @since 3.2.2
*/
class Update implements UpdateInterface
{
/**
* Model
*
* @var Model
* @since 3.2.0
*/
protected Model $model;
/**
* Database
*
* @var Database
* @since 3.2.0
*/
protected Database $database;
/**
* Table Name
*
* @var string
* @since 3.2.1
*/
protected string $table;
/**
* Constructor
*
* @param Model $model The set model object.
* @param Database $database The update database object.
* @param string|null $table The table name.
*
* @since 3.2.0
*/
public function __construct(Model $model, Database $database, ?string $table = null)
{
$this->model = $model;
$this->database = $database;
if ($table !== null)
{
$this->table = $table;
}
}
/**
* Set the current active table
*
* @param string|null $table The table that should be active
*
* @return self
* @since 3.2.2
*/
public function table(?string $table): self
{
if ($table !== null)
{
$this->table = $table;
}
return $this;
}
/**
* Update a value to a given table
* Example: $this->value(Value, 'value_key', 'GUID');
*
* @param mixed $value The field value
* @param string $field The field key
* @param string $keyValue The key value
* @param string $key The key name
*
* @return bool
* @since 3.2.0
*/
public function value($value, string $field, string $keyValue, string $key = 'guid'): bool
{
// build the array
$item = [];
$item[$key] = $keyValue;
$item[$field] = $value;
// Update the column of this table using $key as the primary key.
return $this->row($item, $key);
}
/**
* Update single row with multiple values to a given table
* Example: $this->item(Array);
*
* @param array $item The item to save
* @param string $key The key name
*
* @return bool
* @since 3.2.0
*/
public function row(array $item, string $key = 'guid'): bool
{
// check if object could be modelled
if (($item = $this->model->row($item, $this->getTable())) !== null)
{
// Update the column of this table using $key as the primary key.
return $this->database->row($item, $key, $this->getTable());
}
return false;
}
/**
* Update multiple rows to a given table
* Example: $this->items(Array);
*
* @param array|null $items The items updated in database (array of arrays)
* @param string $key The key name
*
* @return bool
* @since 3.2.0
*/
public function rows(?array $items, string $key = 'guid'): bool
{
// check if object could be modelled
if (($items = $this->model->rows($items, $this->getTable())) !== null)
{
// Update the column of this table using $key as the primary key.
return $this->database->rows($items, $key, $this->getTable());
}
return false;
}
/**
* Update single item with multiple values to a given table
* Example: $this->item(Object);
*
* @param object $item The item to save
* @param string $key The key name
*
* @return bool
* @since 3.2.0
*/
public function item(object $item, string $key = 'guid'): bool
{
// check if object could be modelled
if (($item = $this->model->item($item, $this->getTable())) !== null)
{
// Update the column of this table using $key as the primary key.
return $this->database->item($item, $key, $this->getTable());
}
return false;
}
/**
* Update multiple items to a given table
* Example: $this->items(Array);
*
* @param array|null $items The items updated in database (array of objects)
* @param string $key The key name
*
* @return bool
* @since 3.2.0
*/
public function items(?array $items, string $key = 'guid'): bool
{
// check if object could be modelled
if (($items = $this->model->items($items, $this->getTable())) !== null)
{
// Update the column of this table using $key as the primary key.
return $this->database->items($items, $key, $this->getTable());
}
return false;
}
/**
* Get the current active table
*
* @return string
* @since 3.2.2
*/
public function getTable(): string
{
return $this->table;
}
}

View File

@@ -0,0 +1 @@
<html><body bgcolor="#FFFFFF"></body></html>

View File

@@ -0,0 +1,254 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 4th September, 2022
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VDM\Joomla\Data;
use VDM\Joomla\Interfaces\Data\LoadInterface as Load;
use VDM\Joomla\Interfaces\Data\InsertInterface as Insert;
use VDM\Joomla\Interfaces\Data\UpdateInterface as Update;
use VDM\Joomla\Interfaces\Data\DeleteInterface as Delete;
use VDM\Joomla\Interfaces\LoadInterface as Database;
use VDM\Joomla\Interfaces\Data\ItemInterface;
/**
* Data Item
*
* @since 3.2.2
*/
final class Item implements ItemInterface
{
/**
* The Load Class.
*
* @var Load
* @since 3.2.2
*/
protected Load $load;
/**
* The Insert Class.
*
* @var Insert
* @since 3.2.2
*/
protected Insert $insert;
/**
* The Update Class.
*
* @var Update
* @since 3.2.2
*/
protected Update $update;
/**
* The Delete Class.
*
* @var Delete
* @since 3.2.2
*/
protected Delete $delete;
/**
* The Load Class.
*
* @var Database
* @since 3.2.2
*/
protected Database $database;
/**
* Table Name
*
* @var string
* @since 3.2.1
*/
protected string $table;
/**
* Constructor.
*
* @param Load $load The LoadInterface Class.
* @param Insert $insert The InsertInterface Class.
* @param Update $update The UpdateInterface Class.
* @param Delete $delete The UpdateInterface Class.
* @param Database $database The Database Load Class.
* @param string|null $table The table name.
*
* @since 3.2.2
*/
public function __construct(Load $load, Insert $insert, Update $update,
Delete $delete, Database $database, ?string $table = null)
{
$this->load = $load;
$this->insert = $insert;
$this->update = $update;
$this->delete = $delete;
$this->database = $database;
if ($table !== null)
{
$this->table = $table;
}
}
/**
* Set the current active table
*
* @param string $table The table that should be active
*
* @return self
* @since 3.2.2
*/
public function table(string $table): self
{
$this->table = $table;
return $this;
}
/**
* Get an item
*
* @param string $value The item key value
* @param string $key The item key
*
* @return object|null The item object or null
* @since 3.2.2
*/
public function get(string $value, string $key = 'guid'): ?object
{
return $this->load->table($this->getTable())->item([$key => $value]);
}
/**
* Get the value
*
* @param string $value The item key value
* @param string $key The item key
* @param string $get The key of the values we want back
*
* @return mixed
* @since 3.2.2
*/
public function value(string $value, string $key = 'guid', string $get = 'id')
{
// Perform the database query
$value = $this->database->value(
["a.$get" => $get],
["a" => $this->getTable()],
["a.$key" => $value]
);
// Check if rows are found
if ($value !== null)
{
// Return the value
return $value;
}
// Return null if no rows are found
return null;
}
/**
* Set an item
*
* @param object $item The item
* @param string $key The item key
* @param string|null $action The action to load power
*
* @return bool
* @since 3.2.2
*/
public function set(object $item, string $key = 'guid', ?string $action = null): bool
{
if ($action !== null || (isset($item->{$key}) && ($action = $this->action($item->{$key}, $key)) !== null))
{
return method_exists($this, $action) ? $this->{$action}($item, $key) : false;
}
return false;
}
/**
* Delete an item
*
* @param string $value The item key value
* @param string $key The item key
*
* @return bool
* @since 3.2.2
*/
public function delete(string $value, string $key = 'guid'): bool
{
return $this->delete->table($this->getTable())->items([$key => $value]);
}
/**
* Get the current active table
*
* @return string
* @since 3.2.2
*/
public function getTable(): string
{
return $this->table;
}
/**
* Insert a item
*
* @param object $item The item
*
* @return bool
* @since 3.2.2
*/
private function insert(object $item): bool
{
return $this->insert->table($this->getTable())->item($item);
}
/**
* Update a item
*
* @param object $item The item
* @param string $key The item key
*
* @return bool
* @since 3.2.2
*/
private function update(object $item, string $key): bool
{
return $this->update->table($this->getTable())->item($item, $key);
}
/**
* Get loading action
*
* @param string $value The key value the item
* @param string $key The item key
*
* @return string
* @since 3.2.2
*/
private function action(string $value, string $key): string
{
if (($id = $this->value($value, $key, 'id')) !== null && $id > 0)
{
return 'update';
}
return 'insert';
}
}

View File

@@ -0,0 +1,351 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 4th September, 2022
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VDM\Joomla\Data;
use VDM\Joomla\Interfaces\Data\LoadInterface as Load;
use VDM\Joomla\Interfaces\Data\InsertInterface as Insert;
use VDM\Joomla\Interfaces\Data\UpdateInterface as Update;
use VDM\Joomla\Interfaces\Data\DeleteInterface as Delete;
use VDM\Joomla\Interfaces\LoadInterface as Database;
use VDM\Joomla\Interfaces\Data\ItemsInterface;
/**
* Data Items
*
* @since 3.2.2
*/
final class Items implements ItemsInterface
{
/**
* The LoadInterface Class.
*
* @var Load
* @since 3.2.2
*/
protected Load $load;
/**
* The InsertInterface Class.
*
* @var Insert
* @since 3.2.2
*/
protected Insert $insert;
/**
* The UpdateInterface Class.
*
* @var Update
* @since 3.2.2
*/
protected Update $update;
/**
* The DeleteInterface Class.
*
* @var Delete
* @since 3.2.2
*/
protected Delete $delete;
/**
* The Load Class.
*
* @var Database
* @since 3.2.2
*/
protected Database $database;
/**
* Table Name
*
* @var string
* @since 3.2.1
*/
protected string $table;
/**
* Constructor.
*
* @param Load $load The LoadInterface Class.
* @param Insert $insert The InsertInterface Class.
* @param Update $update The UpdateInterface Class.
* @param Delete $delete The DeleteInterface Class.
* @param Database $database The Database Load Class.
* @param string|null $table The table name.
*
* @since 3.2.2
*/
public function __construct(Load $load, Insert $insert, Update $update, Delete $delete,
Database $database, ?string $table = null)
{
$this->load = $load;
$this->insert = $insert;
$this->update = $update;
$this->delete = $delete;
$this->database = $database;
if ($table !== null)
{
$this->table = $table;
}
}
/**
* Set the current active table
*
* @param string $table The table that should be active
*
* @return self
* @since 3.2.2
*/
public function table(string $table): self
{
$this->table = $table;
return $this;
}
/**
* Get list of items
*
* @param array $values The ids of the items
* @param string $key The key of the values
*
* @return array|null The item object or null
* @since 3.2.2
*/
public function get(array $values, string $key = 'guid'): ?array
{
return $this->load->table($this->getTable())->items([
$key => [
'operator' => 'IN',
'value' => array_values($values)
]
]);
}
/**
* Get the values
*
* @param array $values The list of values (to search by).
* @param string $key The key on which the values being searched.
* @param string $get The key of the values we want back
*
* @return array|null The array of found values.
* @since 3.2.2
*/
public function values(array $values, string $key = 'guid', string $get = 'id'): ?array
{
// Perform the database query
$rows = $this->database->rows(
["a.$get" => $get],
["a" => $this->getTable()],
["a.$key" => ['operator' => 'IN', 'value' => $values]]
);
// Check if rows are found
if ($rows !== null)
{
// Return the values from the found rows
return array_values(
array_map(
fn($row) => $row[$get],
$rows
)
);
}
// Return null if no rows are found
return null;
}
/**
* Set items
*
* @param array $items The list of items
* @param string $key The key on which the items should be set
*
* @return bool
* @since 3.2.2
*/
public function set(array $items, string $key = 'guid'): bool
{
if (($sets = $this->sort($items, $key)) !== null)
{
foreach ($sets as $action => $items)
{
$this->{$action}($items, $key);
}
return true;
}
return false;
}
/**
* Delete items
*
* @param array $values The item key value
* @param string $key The item key
*
* @return bool
* @since 3.2.2
*/
public function delete(string $values, string $key = 'guid'): bool
{
return $this->delete->table($this->getTable())->items([$key => ['operator' => 'IN', 'value' => $values]]);
}
/**
* Get the current active table
*
* @return string
* @since 3.2.2
*/
public function getTable(): string
{
return $this->table;
}
/**
* Insert a item
*
* @param array $items The item
*
* @return bool
* @since 3.2.2
*/
private function insert(array $items): bool
{
return $this->insert->table($this->getTable())->rows($items);
}
/**
* Update a item
*
* @param object $item The item
* @param string $key The item key
*
* @return bool
* @since 3.2.2
*/
private function update(array $items, string $key): bool
{
return $this->update->table($this->getTable())->rows($items, $key);
}
/**
* Sort items between insert and update.
*
* @param array $items The list of items.
* @param string $key The key on which the items should be sorted.
*
* @return array|null The sorted sets.
* @since 3.2.2
*/
private function sort(array $items, string $key): ?array
{
// Extract relevant items based on the key.
$values = $this->extractValues($items, $key);
if ($values === null)
{
return null;
}
$sets = [
'insert' => [],
'update' => []
];
// Check for existing items.
$existingItems = $this->values($values, $key, $key);
if ($existingItems !== null)
{
$sets['update'] = $this->extractSet($items, $existingItems, $key) ?? [];
$sets['insert'] = $this->extractSet($items, $existingItems, $key, true) ?? [];
}
else
{
$sets['insert'] = $items;
}
// If either set is empty, remove it from the result.
$sets = array_filter($sets);
return !empty($sets) ? $sets : null;
}
/**
* Extracts values for a given key from an array of items.
* Items can be either arrays or objects.
*
* @param array $items Array of items (arrays or objects)
* @param string $key The key to extract values for
*
* @return array|null Extracted values
* @since 3.2.2
*/
private function extractValues(array $items, string $key): ?array
{
$result = [];
foreach ($items as $item)
{
if (is_array($item) && !empty($item[$key]))
{
$result[] = $item[$key];
}
elseif (is_object($item) && !empty($item->{$key}))
{
$result[] = $item->{$key};
}
}
return ($result === []) ? null : $result;
}
/**
* Extracts items from an array of items based on a set.
* Items can be either arrays or objects.
*
* @param array $items Array of items (arrays or objects)
* @param array $set The set to match values against
* @param string $key The key of the set values
* @param bool $inverse Whether to extract items not in the set
*
* @return array|null Extracted values
* @since 3.2.2
*/
private function extractSet(array $items, array $set, string $key, bool $inverse = false): ?array
{
$result = [];
foreach ($items as $item)
{
$value = is_array($item) ? ($item[$key] ?? null) : ($item->{$key} ?? null);
if ($value !== null)
{
$inSet = in_array($value, $set);
if (($inSet && !$inverse) || (!$inSet && $inverse))
{
$result[] = is_array($item) ? $item : (array) $item; // convert all to arrays
}
}
}
return empty($result) ? null : $result;
}
}

View File

@@ -0,0 +1,169 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 4th September, 2022
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VDM\Joomla\Data;
use VDM\Joomla\Interfaces\GrepInterface as Grep;
use VDM\Joomla\Interfaces\Data\ItemInterface as Item;
use VDM\Joomla\Interfaces\Data\RemoteInterface;
/**
* Load data based on global unique ids from remote system
*
* @since 3.2.0
*/
class Remote implements RemoteInterface
{
/**
* The Grep Class.
*
* @var Grep
* @since 3.2.0
*/
protected Grep $grep;
/**
* The Item Class.
*
* @var Item
* @since 3.2.0
*/
protected Item $item;
/**
* Table Name
*
* @var string
* @since 3.2.1
*/
protected string $table;
/**
* Constructor.
*
* @param Grep $grep The GrepInterface Class.
* @param Item $item The ItemInterface Class.
* @param string|null $table The table name.
*
* @since 3.2.0
*/
public function __construct(Grep $grep, Item $item, ?string $table = null)
{
$this->grep = $grep;
$this->item = $item;
if ($table !== null)
{
$this->table = $table;
}
}
/**
* Set the current active table
*
* @param string $table The table that should be active
*
* @return self
* @since 3.2.2
*/
public function table(string $table): self
{
$this->table = $table;
return $this;
}
/**
* Init all items not found in database
*
* @return bool
* @since 3.2.0
*/
public function init(): bool
{
if (($items = $this->grep->getRemotePowersGuid()) !== null)
{
foreach($items as $guid)
{
if ($this->item->table($this->getTable())->value($guid) !== null &&
($item = $this->grep->get($guid, ['remote'])) !== null)
{
$this->item->set($item);
}
}
return true;
}
return false;
}
/**
* Reset the items
*
* @param array $items The global unique ids of the items
*
* @return bool
* @since 3.2.0
*/
public function reset(array $items): bool
{
if ($items === [])
{
return false;
}
$success = true;
foreach($items as $guid)
{
if (!$this->load($guid, ['remote']))
{
$success = false;
}
}
return $success;
}
/**
* Load a item
*
* @param string $guid The global unique id of the item
* @param array $order The search order
* @param string|null $action The action to load power
*
* @return bool
* @since 3.2.0
*/
public function load(string $guid, array $order = ['remote', 'local'], ?string $action = null): bool
{
if (($item = $this->grep->get($guid, $order)) !== null)
{
return $this->item->table($this->getTable())->set($item);
}
return false;
}
/**
* Get the current active table
*
* @return string
* @since 3.2.2
*/
public function getTable(): string
{
return $this->table;
}
}