├── .gitignore ├── README.md ├── composer.json └── src └── FakerProvider.php /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | vendor 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | CronExpressionGenerator 2 | ======================= 3 | 4 | Generate valid, random cron expressions. 5 | 6 | 7 | Installation 8 | ------------ 9 | 10 | Add the CronExpressionGenerator library to your `composer.json` file: 11 | 12 | composer require swekaj/cron-expression-generator 13 | 14 | Usage 15 | ----- 16 | 17 | To use this with [Faker](https://github.com/fzaninotto/Faker), you must add the `CronExpressionGenerator\FakerProvider` class to the Faker generator: 18 | 19 | ```php 20 | addProvider(new \CronExpressionGenerator\FakerProvider($faker)); 24 | 25 | // Generator sample output 26 | $faker->cron(); // 12-38 */4 * * 2000/5 27 | $faker->cronMinute(); // */12,30-39 28 | $faker->cronHour(); // 0-23 29 | $faker->cronDayOfMonth(); // L,14W,30 30 | $faker->cronMonth(); // 1/2,6-8 31 | $faker->cronDayOfWeek(); // 0-2,4W,3#2 32 | $faker->cronYear(); // 2012,2000-2039 33 | ``` 34 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "swekaj/cron-expression-generator", 3 | "description": "Generate valid cron expressions.", 4 | "homepage": "https://github.com/swekaj/CronExpressionGenerator", 5 | "version": "1.0.0", 6 | "license": "MIT", 7 | "authors": [ 8 | { "name": "Jake Soward" } 9 | ], 10 | "require": { 11 | "php": ">=5.3.0", 12 | "fzaninotto/faker": "~1.4" 13 | }, 14 | "autoload": { 15 | "psr-4": { 16 | "CronExpressionGenerator\\": "src" 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/FakerProvider.php: -------------------------------------------------------------------------------- 1 | randomElement(array( '*', $this->numberBetween(0, 59) )); 18 | 19 | if ($this->generator->boolean(30)) { 20 | $possibilities[] = $min.'/'.$this->numberBetween(1, 59); 21 | continue; 22 | } 23 | 24 | if ($min !== '*' && $min < 58 && $this->generator->boolean(30)) { 25 | $possibilities[] = $min.'-'.$this->numberBetween($min+1, 59); 26 | continue; 27 | } 28 | 29 | $possibilities[] = $min; 30 | } 31 | 32 | $combined = array_unique($possibilities); 33 | $possibilities[] = implode(',', $this->randomElements($combined, count($combined))); 34 | 35 | return $this->randomElement($possibilities); 36 | } 37 | 38 | public function cronHour() 39 | { 40 | $possibilities = array(); 41 | 42 | for ($i = 0; $i < 3; $i++) { 43 | $hour = $this->randomElement(array( '*', $this->numberBetween(0, 23) )); 44 | 45 | if ($this->generator->boolean(30)) { 46 | $possibilities[] = $hour.'/'.$this->numberBetween(1, 23); 47 | continue; 48 | } 49 | 50 | if ($hour !== '*' && $hour < 20 && $this->generator->boolean(3)) { 51 | $possibilities[] = $hour.'-'.$this->numberBetween($hour+1, 23); 52 | continue; 53 | } 54 | 55 | $possibilities[] = $hour; 56 | } 57 | 58 | $combined = array_unique($possibilities); 59 | $possibilities[] = implode(',', $this->randomElements($combined, count($combined))); 60 | 61 | return $this->randomElement($possibilities); 62 | } 63 | 64 | public function cronDayOfMonth($extended = true) 65 | { 66 | $possibilities = array(); 67 | 68 | for ($i = 0; $i < 3; $i++) { 69 | $elements = array( '*', $this->numberBetween(1, 31) ); 70 | if ($extended) { 71 | $elements[] = 'L'; 72 | } 73 | $day = $this->randomElement($elements); 74 | 75 | if ($day !== 'L' && $this->generator->boolean(30)) { 76 | $possibilities[] = $day.'/'.$this->numberBetween(1, 31); 77 | continue; 78 | } 79 | 80 | if (is_numeric($day) && $day < 28 && $this->generator->boolean(30)) { 81 | $possibilities[] = $day.'-'.$this->numberBetween($day+1, 31); 82 | continue; 83 | } 84 | 85 | if ($extended && is_numeric($day) && $this->generator->boolean(30)) { 86 | $possibilities[] = $day.'W'; 87 | continue; 88 | } 89 | 90 | $possibilities[] = $day; 91 | } 92 | 93 | $combined = array_unique($possibilities); 94 | $possibilities[] = implode(',', $this->randomElements($combined, count($combined))); 95 | 96 | return $this->randomElement($possibilities); 97 | } 98 | 99 | public function cronMonth() 100 | { 101 | $possibilities = array(); 102 | 103 | for ($i = 0; $i < 3; $i++) { 104 | $month = $this->randomElement(array( '*', $this->numberBetween(1, 12) )); 105 | 106 | if ($this->generator->boolean(30)) { 107 | $possibilities[] = $month.'/'.$this->numberBetween(1, 12); 108 | continue; 109 | } 110 | 111 | if ($month !== '*' && $month < 11 && $this->generator->boolean(30)) { 112 | $possibilities[] = $month.'-'.$this->numberBetween($month+1, 12); 113 | continue; 114 | } 115 | 116 | $possibilities[] = $month; 117 | } 118 | 119 | $combined = array_unique($possibilities); 120 | $possibilities[] = implode(',', $this->randomElements($combined, count($combined))); 121 | 122 | return $this->randomElement($possibilities); 123 | } 124 | 125 | public function cronDayOfWeek($extended = true) 126 | { 127 | $possibilities = array(); 128 | 129 | for ($i = 0; $i < 3; $i++) { 130 | $day = $this->randomElement(array( '*', $this->numberBetween(0, 6) )); 131 | 132 | if ($this->generator->boolean(30)) { 133 | $possibilities[] = $day.'/'.$this->numberBetween(1, 6); 134 | continue; 135 | } 136 | 137 | if ($day != '*' && $day < 5 && $this->generator->boolean(30)) { 138 | $possibilities[] = $day.'-'.$this->numberBetween($day+1, 6); 139 | continue; 140 | } 141 | 142 | if ($extended && $day !== '*' && $this->generator->boolean(30)) { 143 | $possibilities[] = $day.'L'; 144 | continue; 145 | } 146 | 147 | if ($extended && $day != '*' && $this->generator->boolean(30)) { 148 | $possibilities[] = $day.'#'.$this->numberBetween(1, 5); 149 | continue; 150 | } 151 | 152 | $possibilities[] = $day; 153 | } 154 | 155 | $combined = array_unique($possibilities); 156 | $possibilities[] = implode(',', $this->randomElements($combined, count($combined))); 157 | 158 | return $this->randomElement($possibilities); 159 | } 160 | 161 | public function cronYear() 162 | { 163 | $possibilities = array(); 164 | 165 | for ($i = 0; $i < 3; $i++) { 166 | $year = $this->randomElement(array( '*', $this->numberBetween(1970, 2099) )); 167 | 168 | if ($year != '*' && $this->generator->boolean(30)) { 169 | $possibilities[] = $year.'/'.$this->randomDigit(); 170 | continue; 171 | } 172 | 173 | if ($year != '*' && $year < 2098 && $this->generator->boolean(30)) { 174 | $possibilities[] = $year.'-'.$this->numberBetween($year+1, 2099); 175 | continue; 176 | } 177 | 178 | $possibilities[] = $year; 179 | } 180 | 181 | return $this->randomElement($possibilities); 182 | } 183 | 184 | public function cron($extended = true) 185 | { 186 | return $this->cronMinute() 187 | . ' ' . $this->cronHour() 188 | . ' ' . $this->cronDayOfMonth($extended) 189 | . ' ' . $this->cronMonth() 190 | . ' ' . $this->cronDayOfWeek($extended) 191 | . ($extended ? ' ' . $this->cronYear() : '' ); 192 | } 193 | } 194 | --------------------------------------------------------------------------------