1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-16 03:34:33 +02:00

Improve __debugInfo() in Selector and Selectors classes

This commit is contained in:
Ryan Cramer
2019-02-21 11:08:25 -05:00
parent 774c2152ee
commit 5483804d49
3 changed files with 76 additions and 18 deletions

View File

@@ -172,7 +172,7 @@ abstract class Selector extends WireData {
if($forceString === 1) {
$value = reset($value);
} else {
$value = implode('|', $value);
$value = $this->wire('sanitizer')->selectorValue($value);
}
}
return $value;
@@ -257,7 +257,7 @@ abstract class Selector extends WireData {
public function getValue($type = '') {
$value = $this->value;
if($type == 'string') {
if(is_array($value)) $value = implode('|', $value);
if(is_array($value)) $value = $this->wire('sanitizer')->selectorValue($value);
} else if($type == 'array') {
if(!is_array($value)) $value = array($value);
} else if($this->quote == '[') {
@@ -410,21 +410,51 @@ abstract class Selector extends WireData {
*
*/
public function __toString() {
$openingQuote = $this->quote;
$closingQuote = $openingQuote;
if($openingQuote) {
if($openingQuote == '[') $closingQuote = ']';
else if($openingQuote == '{') $closingQuote = '}';
else if($openingQuote == '(') $closingQuote = ')';
}
$str = ($this->not ? '!' : '') .
$value = $this->value();
if($openingQuote) $value = trim($value, $openingQuote . $closingQuote);
$value = $openingQuote . $value . $closingQuote;
$str =
($this->not ? '!' : '') .
(is_null($this->group) ? '' : $this->group . '@') .
(is_array($this->field) ? implode('|', $this->field) : $this->field) .
$this->operator() .
(is_array($this->value) ? implode("|", $this->value) : $openingQuote . $this->value . $closingQuote);
$this->operator() . $value;
return $str;
}
/**
* Debug info
*
* #pw-internal
*
* @return array
*
*/
public function __debugInfo() {
$info = array(
'field' => $this->field,
'operator' => $this->operator,
'value' => $this->value,
);
if($this->not) $info['not'] = true;
if($this->forceMatch) $info['forceMatch'] = true;
if($this->group) $info['group'] = $this->group;
if($this->quote) $info['quote'] = $this->quote;
$info['string'] = $this->__toString();
return $info;
}
/**
* Add all individual selector types to the runtime Selectors
*

View File

@@ -1289,6 +1289,17 @@ class Selectors extends WireArray {
return $all ? $matches : $selector;
}
public function __debugInfo() {
$info = parent::__debugInfo();
$info['string'] = $this->__toString();
return $info;
}
public function debugInfoItem($item) {
if($item instanceof Selector) return $item->__debugInfo();
return parent::debugInfoItem($item);
}
/**
* See if the given $selector specifies the given $field somewhere

View File

@@ -2431,32 +2431,49 @@ class WireArray extends Wire implements \IteratorAggregate, \ArrayAccess, \Count
*
*/
public function __debugInfo() {
$info = parent::__debugInfo();
$info['count'] = $this->count();
if(count($this->data)) {
$info['items'] = array();
foreach($this->data as $key => $value) {
if(is_object($value)) {
if($value instanceof Page) {
$value = '/' . ltrim($value->path(), '/');
} else if($value instanceof WireData) {
$_value = $value;
$value = $value->get('name');
if(!$value) $value = $_value->get('id');
if(!$value) $value = $_value->className();
} else {
// keep $value as it is
}
}
$info['items'][$key] = $value;
$info['items'][$key] = $this->debugInfoItem($value);
}
}
if(count($this->extraData)) $info['extraData'] = $this->extraData;
if(count($this->itemsAdded)) $info['itemsAdded'] = $this->itemsAdded;
if(count($this->itemsRemoved)) $info['itemsRemoved'] = $this->itemsRemoved;
return $info;
}
/**
* Return debug info for one item from this WireArray
*
* #pw-internal
*
* @param mixed $item
* @return mixed|null|string
*
*/
public function debugInfoItem($item) {
if(is_object($item)) {
if($item instanceof Page) {
$item = '/' . ltrim($item->path(), '/');
} else if($item instanceof WireData) {
$_item = $item;
$item = $item->get('name');
if(!$item) $item = $_item->get('id');
if(!$item) $item = $_item->className();
} else {
// keep $value as it is
}
}
return $item;
}
/**
* Static method caller, primarily for support of WireArray::new() method
*