From 0a1ec95a33dab00a99a787c219c8bd36eefb3f0f Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Mon, 13 Jan 2020 08:48:06 -0500 Subject: [PATCH] Update wireEmpty() function so that it also returns false when giving a NullPage or NullField object. Add WireNull interface for future Null objects. --- wire/core/Functions.php | 2 ++ wire/core/Interfaces.php | 6 ++++++ wire/core/NullField.php | 2 +- wire/core/NullPage.php | 2 +- 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/wire/core/Functions.php b/wire/core/Functions.php index 852cbf20..4e78b63b 100644 --- a/wire/core/Functions.php +++ b/wire/core/Functions.php @@ -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; } diff --git a/wire/core/Interfaces.php b/wire/core/Interfaces.php index 4a7cf6a4..8059df3d 100644 --- a/wire/core/Interfaces.php +++ b/wire/core/Interfaces.php @@ -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 * diff --git a/wire/core/NullField.php b/wire/core/NullField.php index e9cb877e..353c8b71 100644 --- a/wire/core/NullField.php +++ b/wire/core/NullField.php @@ -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 ''; diff --git a/wire/core/NullPage.php b/wire/core/NullPage.php index e5d02e78..6080a85b 100644 --- a/wire/core/NullPage.php +++ b/wire/core/NullPage.php @@ -32,7 +32,7 @@ * */ -class NullPage extends Page { +class NullPage extends Page implements WireNull { /** * #pw-internal *