1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-07 15:26:54 +02:00

Update wireEmpty() function so that it also returns false when giving a NullPage or NullField object. Add WireNull interface for future Null objects.

This commit is contained in:
Ryan Cramer
2020-01-13 08:48:06 -05:00
parent 321ea0eed3
commit 0a1ec95a33
4 changed files with 10 additions and 2 deletions

View File

@@ -1087,6 +1087,7 @@ function wireCount($value) {
*
* - It returns true for Countable objects that have 0 items.
* - It considers whitespace-only strings to be empty.
* - It considers WireNull objects (like NullPage or any others) to be empty (3.0.149+).
* - You cannot pass it an undefined variable without triggering a PHP warning.
*
* ~~~~~
@@ -1117,6 +1118,7 @@ function wireEmpty($value) {
if(empty($value)) return true;
if(is_object($value)) {
if($value instanceof \Countable && !count($value)) return true;
if($value instanceof WireNull) return true; // 3.0.149+
} else if(is_string($value)) {
if(!strlen(trim($value))) return true;
}

View File

@@ -327,6 +327,12 @@ interface WirePageEditor {
public function getPage();
}
/**
* Interface shared by all ProcessWire Null objects
*
*/
interface WireNull { }
/**
* Interface that indicates the object supports its items being paginated
*

View File

@@ -10,7 +10,7 @@
*
*/
class NullField extends Field {
class NullField extends Field implements WireNull {
public function get($key) {
if($key == 'id') return 0;
if($key == 'name') return '';

View File

@@ -32,7 +32,7 @@
*
*/
class NullPage extends Page {
class NullPage extends Page implements WireNull {
/**
* #pw-internal
*