nash-ye /
ebloodbank
| 1 | <?php |
||||||
| 2 | /** |
||||||
| 3 | * Entity meta trait file |
||||||
| 4 | * |
||||||
| 5 | * @package EBloodBank |
||||||
| 6 | * @subpackage Traits |
||||||
| 7 | * @since 1.0 |
||||||
| 8 | */ |
||||||
| 9 | namespace EBloodBank\Traits; |
||||||
| 10 | |||||||
| 11 | /** |
||||||
| 12 | * Entity meta trait |
||||||
| 13 | * |
||||||
| 14 | * @since 1.0 |
||||||
| 15 | */ |
||||||
| 16 | trait EntityMeta |
||||||
| 17 | { |
||||||
| 18 | /** |
||||||
| 19 | * Entity meta |
||||||
| 20 | * |
||||||
| 21 | * @var array |
||||||
| 22 | * @since 1.0 |
||||||
| 23 | */ |
||||||
| 24 | protected $meta = []; |
||||||
| 25 | |||||||
| 26 | /** |
||||||
| 27 | * Get entity meta value |
||||||
| 28 | * |
||||||
| 29 | * @return mixed |
||||||
| 30 | * @since 1.4 |
||||||
| 31 | */ |
||||||
| 32 | public function getMeta(string $key, $fallback = '') |
||||||
| 33 | { |
||||||
| 34 | $value = $fallback; |
||||||
| 35 | |||||||
| 36 | if (! is_array($this->meta)) { |
||||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||||
| 37 | return $value; |
||||||
| 38 | } |
||||||
| 39 | |||||||
| 40 | if (! isset($this->meta[$key])) { |
||||||
| 41 | return $value; |
||||||
| 42 | } |
||||||
| 43 | |||||||
| 44 | $value = $this->meta[$key]; |
||||||
| 45 | |||||||
| 46 | return $value; |
||||||
| 47 | } |
||||||
| 48 | |||||||
| 49 | /** |
||||||
| 50 | * Set entity meta value |
||||||
| 51 | * |
||||||
| 52 | * @return void |
||||||
| 53 | * @since 1.4 |
||||||
| 54 | */ |
||||||
| 55 | public function setMeta(string $key, $value, $sanitize = false, $validate = true) |
||||||
| 56 | { |
||||||
| 57 | if (is_null($this->meta)) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 58 | $this->meta = []; |
||||||
| 59 | } |
||||||
| 60 | |||||||
| 61 | if ($sanitize) { |
||||||
| 62 | $value = static::sanitizeMeta($key, $value); |
||||||
| 63 | } |
||||||
| 64 | |||||||
| 65 | if (! $validate || static::validateMeta($key, $value)) { |
||||||
| 66 | $this->meta[$key] = $value; |
||||||
| 67 | } |
||||||
| 68 | } |
||||||
| 69 | |||||||
| 70 | /** |
||||||
| 71 | * Delete meta value |
||||||
| 72 | * |
||||||
| 73 | * @return void |
||||||
| 74 | * @since 1.0 |
||||||
| 75 | */ |
||||||
| 76 | public function deleteMeta(string $key) |
||||||
| 77 | { |
||||||
| 78 | unset($this->meta[$key]); |
||||||
| 79 | } |
||||||
| 80 | |||||||
| 81 | /** |
||||||
| 82 | * Sanitize meta value |
||||||
| 83 | * |
||||||
| 84 | * @return mixed |
||||||
| 85 | * @since 1.0 |
||||||
| 86 | * @static |
||||||
| 87 | */ |
||||||
| 88 | public static function sanitizeMeta(string $key, $value) |
||||||
|
0 ignored issues
–
show
The parameter
$key is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||
| 89 | { |
||||||
| 90 | return $value; |
||||||
| 91 | } |
||||||
| 92 | |||||||
| 93 | /** |
||||||
| 94 | * Validate meta value |
||||||
| 95 | * |
||||||
| 96 | * @return bool |
||||||
| 97 | * @since 1.0 |
||||||
| 98 | * @static |
||||||
| 99 | */ |
||||||
| 100 | public static function validateMeta(string $key, $value) |
||||||
|
0 ignored issues
–
show
The parameter
$key is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$value is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||
| 101 | { |
||||||
| 102 | return true; |
||||||
| 103 | } |
||||||
| 104 | } |
||||||
| 105 |