Skip to content

Commit

Permalink
Dev: Improvements to linting errors and fix for options on joins
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanJard committed Nov 12, 2024
1 parent 1d7edef commit 8f3926b
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 126 deletions.
9 changes: 6 additions & 3 deletions Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ public function __construct($opts)

/** @var callable */
private $_type;

/** @var callable|null */
private $_debugCallback;

private $query_driver;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Expand Down Expand Up @@ -127,7 +130,7 @@ public function commit()
* @param array $where Where condition for what to select - see {@see
* Query->where()}.
*
* @return Number
* @return int
*/
public function count($table, $field = 'id', $where = null)
{
Expand All @@ -145,14 +148,14 @@ public function count($table, $field = 'id', $where = null)
/**
* Get / set debug mode.
*
* @param bool $set Debug mode state. If not given, then used as a getter.
* @param false|callable $set Debug mode state. If not given, then used as a getter.
*
* @return ($set is null ? bool : $this) Debug mode state if no parameter is given.
*/
public function debug($set = null)
{
if ($set === null) {
return $this->_debugCallback ? true : false;
return is_callable($this->_debugCallback) ? true : false;
} elseif ($set === false) {
$this->_debugCallback = null;
} else {
Expand Down
58 changes: 30 additions & 28 deletions Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,8 @@ public function process($data)
$debugInfo = &$this->_debugInfo;

$debugInfo[] = 'Editor PHP libraries - version ' . $this->version;
$debugVal = $this->_db->debug(static function ($mess) use (&$debugInfo) {

$this->_db->debug(function ($mess) use (&$debugInfo) {
$debugInfo[] = $mess;
});
}
Expand Down Expand Up @@ -988,11 +989,12 @@ public function write($_writeVal = null)
private function _process($data)
{
$this->_out = [
'fieldErrors' => [],
'error' => '',
'cancelled' => [],
'data' => [],
'error' => '',
'fieldErrors' => [],
'ipOpts' => [],
'cancelled' => [],
'options' => []
];

$action = Editor::action($data);
Expand Down Expand Up @@ -1154,11 +1156,8 @@ private function _get($id = null, $http = null)
}

$res = $query->exec();
if (!$res) {
throw new \Exception('Error executing SQL for data get. Enable SQL debug using `->debug(true)`');
}

$out = [];

while ($row = $res->fetch()) {
$inner = [];
$inner['DT_RowId'] = $this->_idPrefix . $this->pkeyToValue($row, true);
Expand All @@ -1174,15 +1173,18 @@ private function _get($id = null, $http = null)

// Row based "joins"
for ($i = 0; $i < count($this->_join); ++$i) {
$this->_join[$i]->data($this, $out, $options);
$this->_join[$i]->data($this, $out);
}

$this->_trigger('postGet', $out, $id);

return [
'data' => $out,
'files' => $this->_fileData(null, null, $out),
];
return array_merge(
[
'data' => $out,
'files' => $this->_fileData(null, null, $out),
],
$ssp
);
}

/**
Expand Down Expand Up @@ -2047,7 +2049,7 @@ private function _get_where($query)
* @param string $name Field name
* @param string $type Matching name type
*
* @return Field Field instance
* @return Field|null Field instance or null if not found
*/
private function _find_field($name, $type)
{
Expand All @@ -2067,14 +2069,14 @@ private function _find_field($name, $type)
/**
* Insert or update a row for all main tables and left joined tables.
*
* @param int|string $id ID to use to condition the update. If null is
* given, the first query performed is an insert and the inserted id
* used as the value should there be any subsequent tables to operate
* on. Mote that for compound keys, this should be the "joined" value
* (i.e. a single string rather than an array).
* @param int|string|null $id ID to use to condition the update. If null is
* given, the first query performed is an insert and the inserted id
* used as the value should there be any subsequent tables to operate
* on. Mote that for compound keys, this should be the "joined" value
* (i.e. a single string rather than an array).
*
* @return Database\Result Result from the query or null if no
* query performed.
* @return int|string Result from the query or null if no
* query performed.
*/
private function _insert_or_update($id, $values)
{
Expand Down Expand Up @@ -2157,8 +2159,8 @@ private function _insert_or_update($id, $values)
* @param string $table Database table name to use (can include an alias)
* @param array $where Update condition
*
* @return Database\Result Result from the query or null if no query
* performed.
* @return Database\Result|null Result from the query or null if no query
* performed.
*/
private function _insert_or_update_table($table, $values, $where = null)
{
Expand Down Expand Up @@ -2251,13 +2253,9 @@ private function _options($refresh)
$options = $field->options();

if ($options) {
$opts = $options($this->_db, $refresh);
$opts = $options->exec($this->_db, $refresh);

if ($opts !== false) {
if (!isset($this->_out['options'])) {
$this->_out['options'] = [];
}

$this->_out['options'][$field->name()] = $opts;
}
}
Expand Down Expand Up @@ -2286,6 +2284,10 @@ private function _options($refresh)
}
}
}

for ($i = 0; $i < count($this->_join); ++$i) {
$this->_join[$i]->options($this->_out['options'], $this->_db, $refresh);
}
}

private function _optionsSearch()
Expand Down
8 changes: 4 additions & 4 deletions Editor/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ public function __construct($dbField = null, $name = null)
/** @var array[] */
private $_validator = [];

/** @var Upload */
private $_upload;
/** @var Upload|null */
private $_upload = null;

/** @var callable */
private $_xss;
Expand Down Expand Up @@ -297,7 +297,7 @@ public function name($_ = null)
* @param callable(array): string $format Function will render each label
* @param string $order SQL ordering
*
* @return ($table is null ? Options : $this)
* @return ($table is null ? Options|null : $this)
*/
public function options($table = null, $value = null, $label = null, $condition = null, $format = null, $order = null)
{
Expand Down Expand Up @@ -470,7 +470,7 @@ public function setValue($_ = null)
*
* @param Upload $_ Upload class if used as a setter
*
* @return ($_ is null ? Upload : $this) Value if used as a getter.
* @return ($_ is null ? Upload|null : $this) Value if used as a getter.
*/
public function upload($_ = null)
{
Expand Down
47 changes: 33 additions & 14 deletions Editor/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,11 +486,10 @@ public function whereSet($_ = null)
* @param Editor $editor Host Editor instance
* @param mixed[] $data Data from the parent table's get and were we need
* to add out output.
* @param array $options options array for fields
*
* @internal
*/
public function data($editor, &$data, &$options)
public function data($editor, &$data)
{
if (!$this->_get) {
return;
Expand Down Expand Up @@ -640,18 +639,6 @@ public function data($editor, &$data, &$options)
}
}
}

// Field options
foreach ($this->_fields as $field) {
$opts = $field->optionsExec($db);

if ($opts !== false) {
$name = $this->name() .
($this->_type === 'object' ? '.' : '[].') .
$field->name();
$options[$name] = $opts;
}
}
}

/**
Expand Down Expand Up @@ -687,6 +674,38 @@ public function create($editor, $parentId, $data)
}
}

/**
* Get options for the fields in this join
*
* @param array $options Array to write the read options into
* @param Database $db Database connection object
* @param bool $refresh Refresh indication flag
* @return void
*
* @internal
*/
public function options(&$options, $db, $refresh) {
// Field options
foreach ($this->_fields as $field) {
$optsInst = $field->options();

if ($optsInst) {
$opts = $optsInst->exec($db, $refresh);

if ($opts !== false) {
if ($this->_type === 'object') {
$name = $this->name() . '.' . $field->name();
}
else {
$name = $this->name() . '[].' . $field->name();
}

$options[$name] = $opts;
}
}
}
}

/**
* Update a row.
*
Expand Down
16 changes: 9 additions & 7 deletions Editor/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Options extends Ext
private $_manualAdd = [];

/** @var callable|null */
private $_customFn;
private $_customFn = null;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Public methods
Expand Down Expand Up @@ -301,14 +301,15 @@ public function where($_ = null)
/**
* Execute the options (i.e. get them).
*
* @param Database $db Database connection
* @param bool $refresh Indicate if this is a refresh or a full load
* @param Database $db Database connection
* @param bool $refresh Indicate if this is a refresh or a full load
* @param string|null $search Search term
*
* @return array List of options
* @return array|false List of options
*
* @internal
*/
public function exec($db, $refresh, $search)
public function exec($db, $refresh, $search=null)
{
// If search only, and not a search action, then just return false
if ($this->searchOnly() && !$search) {
Expand All @@ -320,8 +321,9 @@ public function exec($db, $refresh, $search)
return false;
}

if ($this->fn()) {
return $this->_customFn($db);
$fn = $this->_customFn;
if (is_callable($fn)) {
return $fn($db);
}

$label = $this->_label;
Expand Down
Loading

0 comments on commit 8f3926b

Please sign in to comment.