Skip to content

Commit 7ab0f74

Browse files
authored
Merge pull request #154 from tabacitu/custom-casts
added custom casts - CleanHtml, CleanHtmlInput, CleanHtmlOutput
2 parents 9d28db4 + edb1ea3 commit 7ab0f74

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

src/Casts/CleanHtml.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Mews\Purifier\Casts;
4+
5+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
6+
7+
class CleanHtml implements CastsAttributes
8+
{
9+
/**
10+
* Clean the HTML when casting the given value.
11+
*
12+
* @param \Illuminate\Database\Eloquent\Model $model
13+
* @param string $key
14+
* @param mixed $value
15+
* @param array $attributes
16+
* @return array
17+
*/
18+
public function get($model, $key, $value, $attributes)
19+
{
20+
return clean($value);
21+
}
22+
23+
/**
24+
* Prepare the given value for storage by cleaning the HTML.
25+
*
26+
* @param \Illuminate\Database\Eloquent\Model $model
27+
* @param string $key
28+
* @param array $value
29+
* @param array $attributes
30+
* @return string
31+
*/
32+
public function set($model, $key, $value, $attributes)
33+
{
34+
return clean($value);
35+
}
36+
}

src/Casts/CleanHtmlInput.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Mews\Purifier\Casts;
4+
5+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
6+
7+
class CleanHtmlInput implements CastsAttributes
8+
{
9+
/**
10+
* Cast the given value. Does not clean the HTML.
11+
*
12+
* @param \Illuminate\Database\Eloquent\Model $model
13+
* @param string $key
14+
* @param mixed $value
15+
* @param array $attributes
16+
* @return array
17+
*/
18+
public function get($model, $key, $value, $attributes)
19+
{
20+
return $value;
21+
}
22+
23+
/**
24+
* Prepare the given value for storage by cleaning the HTML.
25+
*
26+
* @param \Illuminate\Database\Eloquent\Model $model
27+
* @param string $key
28+
* @param array $value
29+
* @param array $attributes
30+
* @return string
31+
*/
32+
public function set($model, $key, $value, $attributes)
33+
{
34+
return clean($value);
35+
}
36+
}

src/Casts/CleanHtmlOutput.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Mews\Purifier\Casts;
4+
5+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
6+
7+
class CleanHtmlOutput implements CastsAttributes
8+
{
9+
/**
10+
* Clean the HTML when casting the given value.
11+
*
12+
* @param \Illuminate\Database\Eloquent\Model $model
13+
* @param string $key
14+
* @param mixed $value
15+
* @param array $attributes
16+
* @return array
17+
*/
18+
public function get($model, $key, $value, $attributes)
19+
{
20+
return clean($value);
21+
}
22+
23+
/**
24+
* Prepare the given value for storage. Does not clean the HTML.
25+
*
26+
* @param \Illuminate\Database\Eloquent\Model $model
27+
* @param string $key
28+
* @param array $value
29+
* @param array $attributes
30+
* @return string
31+
*/
32+
public function set($model, $key, $value, $attributes)
33+
{
34+
return $value;
35+
}
36+
}

0 commit comments

Comments
 (0)