Mercurial > packages > magicforger
comparison src/Replacer.php @ 1:ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
| author | luka |
|---|---|
| date | Sat, 24 Jun 2023 01:08:01 -0400 |
| parents | |
| children | cf9993c5c7df |
comparison
equal
deleted
inserted
replaced
| 0:329123c41eaf | 1:ca36acd2bef2 |
|---|---|
| 1 <?php | |
| 2 | |
| 3 namespace Wizzard\MagicForger; | |
| 4 | |
| 5 use Illuminate\Support\Str; | |
| 6 | |
| 7 class Replacer { | |
| 8 | |
| 9 /** | |
| 10 * Cached replacements for re-use. | |
| 11 * | |
| 12 * @var array | |
| 13 */ | |
| 14 protected $replacement_cache = []; | |
| 15 | |
| 16 | |
| 17 /** | |
| 18 * Prefix and Suffix for controller. | |
| 19 * Usage is up to the user. | |
| 20 * | |
| 21 * @var string | |
| 22 */ | |
| 23 protected $controller_prefix = ""; | |
| 24 | |
| 25 | |
| 26 /** | |
| 27 * Prefix and Suffix for controller. | |
| 28 * Usage is up to the user. | |
| 29 * | |
| 30 * @var string | |
| 31 */ | |
| 32 protected $controller_suffix = "Controller"; | |
| 33 | |
| 34 | |
| 35 /** | |
| 36 * The lowest level to show log outputs. | |
| 37 * | |
| 38 * @var int | |
| 39 */ | |
| 40 private $log_level = 1; | |
| 41 | |
| 42 | |
| 43 public function __construct() { | |
| 44 /* parent::__construct(); */ | |
| 45 } | |
| 46 | |
| 47 /** | |
| 48 * Model names are generated in uppercase first Camel case | |
| 49 */ | |
| 50 public function model_name(string $name) : string { | |
| 51 return Str::singular(Str::studly($name)); | |
| 52 } | |
| 53 | |
| 54 /** | |
| 55 * Controller names are generated in uppercase first Camel case | |
| 56 * and wrapped in the prefix and suffix | |
| 57 */ | |
| 58 public function controller_name(string $name) : string { | |
| 59 return $this->controller_prefix . | |
| 60 $this->model_name($name) . | |
| 61 $this->controller_suffix; | |
| 62 } | |
| 63 | |
| 64 /** | |
| 65 * Breaks up a string and makes it human readable | |
| 66 * | |
| 67 * This function assumes that the inputted name is camel case | |
| 68 */ | |
| 69 public function human_readable(string $name) : string { | |
| 70 return Str::title(Str::replace('_', ' ', $name)); | |
| 71 } | |
| 72 | |
| 73 /** | |
| 74 * Breaks up a string and makes it human readable and lowecase | |
| 75 * | |
| 76 * This function assumes that the inputted name is camel case | |
| 77 */ | |
| 78 public function human_readable_lc(string $name) : string { | |
| 79 return Str::lower($this->human_readable($name)); | |
| 80 } | |
| 81 | |
| 82 /** | |
| 83 * Outputs a log for the replacements based on log level. | |
| 84 */ | |
| 85 public function log(string $str, int $log_level = 1) : void { | |
| 86 | |
| 87 if($this->log_level <= $log_level) { | |
| 88 print($str . "\n"); | |
| 89 } | |
| 90 | |
| 91 } | |
| 92 } |
