1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelLaundromat; |
4
|
|
|
|
5
|
|
|
use Illuminate\Container\Container; |
6
|
|
|
|
7
|
|
|
trait Washable |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Clean the current object before sending to front end. |
11
|
|
|
* |
12
|
|
|
* @param string $cleanerName [Name of cleaner class or full cleaner namespace] |
13
|
|
|
* |
14
|
|
|
* @return CleanPost |
15
|
|
|
*/ |
16
|
|
|
public function clean($cleanerName = null) |
17
|
|
|
{ |
18
|
|
|
if ($cleanerName !== null) { |
19
|
|
|
return $this->callCleaner($cleanerName); |
20
|
|
|
} elseif (isset($this->defaultCleaner)) { |
21
|
|
|
return $this->callCleaner($this->defaultCleaner); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$className = $this->getStandardCleanerName(); |
25
|
|
|
|
26
|
|
|
return $this->callCleaner($className); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Create the cleaner and call the clean mehtod on it. |
31
|
|
|
* |
32
|
|
|
* @param string $cleanerName [Name of cleaner] |
33
|
|
|
* |
34
|
|
|
* @return Cleaner|HttpException |
35
|
|
|
*/ |
36
|
|
|
protected function callCleaner($cleanerName) |
37
|
|
|
{ |
38
|
|
|
$cleaner = $this->resolveCleaner($cleanerName); |
39
|
|
|
|
40
|
|
|
if (class_exists($cleaner)) { |
41
|
|
|
$cleanerObject = new $cleaner(); |
42
|
|
|
|
43
|
|
|
return $cleanerObject->clean($this); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
abort(500, "Class {$cleaner} does not exist. ". |
47
|
|
|
'Create the class or use a full namespace.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Resolve valid cleaner name for given name. |
52
|
|
|
* |
53
|
|
|
* @param string $name [Cleaner name] |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
protected function resolveCleaner($name) |
58
|
|
|
{ |
59
|
|
|
if (class_exists($name)) { |
60
|
|
|
return $name; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->getStandardCleanerNamespace($name); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get cleaner name from name of calling class. |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
protected function getStandardCleanerName() |
72
|
|
|
{ |
73
|
|
|
$reflection = new \ReflectionClass(get_class()); |
74
|
|
|
|
75
|
|
|
return 'Clean'.$reflection->getShortName(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get full namespace for cleaner. |
80
|
|
|
* |
81
|
|
|
* @param string $className [Name of cleaner class] |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
protected function getStandardCleanerNamespace($className) |
86
|
|
|
{ |
87
|
|
|
$namespace = Container::getInstance()->getNamespace().'Cleaners\\'; |
88
|
|
|
|
89
|
|
|
return $namespace.$className; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|