diff --git a/src/Casts/PurifyHtmlOnGet.php b/src/Casts/PurifyHtmlOnGet.php index bfa2138..84115f4 100644 --- a/src/Casts/PurifyHtmlOnGet.php +++ b/src/Casts/PurifyHtmlOnGet.php @@ -15,10 +15,14 @@ class PurifyHtmlOnGet extends Caster implements CastsAttributes * @param mixed $value * @param array $attributes * - * @return string|array + * @return string|array|null */ public function get($model, $key, $value, $attributes) { + if (is_null($value)) { + return null; + } + return Purify::config($this->config)->clean($value); } @@ -30,7 +34,7 @@ public function get($model, $key, $value, $attributes) * @param mixed $value * @param array $attributes * - * @return array|string + * @return array|string|null */ public function set($model, $key, $value, $attributes) { diff --git a/src/Casts/PurifyHtmlOnSet.php b/src/Casts/PurifyHtmlOnSet.php index a66cea9..a2d99f1 100644 --- a/src/Casts/PurifyHtmlOnSet.php +++ b/src/Casts/PurifyHtmlOnSet.php @@ -15,7 +15,7 @@ class PurifyHtmlOnSet extends Caster implements CastsAttributes * @param mixed $value * @param array $attributes * - * @return string|array + * @return string|array|null */ public function get($model, string $key, $value, array $attributes) { @@ -30,10 +30,14 @@ public function get($model, string $key, $value, array $attributes) * @param mixed $value * @param array $attributes * - * @return array|string + * @return array|string|null */ public function set($model, string $key, $value, array $attributes) { + if (is_null($value)) { + return null; + } + return Purify::config($this->config)->clean($value); } } diff --git a/tests/CastsTest.php b/tests/CastsTest.php index 3174893..b0061fb 100644 --- a/tests/CastsTest.php +++ b/tests/CastsTest.php @@ -36,6 +36,14 @@ public function test_purifies_on_get_with_custom_config() $this->assertEquals('
Testbar
', $model->body); } + public function test_returns_null_on_get_when_value_is_null() + { + $model = new PurifyingDefaultOnGetModel(); + $model->body = null; + + $this->assertNull($model->body); + } + public function test_purifies_on_set_with_default_config() { $this->app['config']->set('purify.configs.default', [ @@ -59,6 +67,14 @@ public function test_purifies_on_set_with_custom_config() $this->assertEquals('Testbar
', $model->getAttributes()['body']); } + + public function test_sets_null_on_set_when_value_is_null() + { + $model = new PurifyingDefaultOnSetModel(); + $model->body = null; + + $this->assertNull($model->getAttributes()['body']); + } } class PurifyingDefaultOnGetModel extends Model