Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Namespace
- namespace Modules;
- /**
- * PasswordGenerator
- * @author Jan Doušek <mail@jandousek.cz>
- */
- class PasswordGenerator {
- /* @var array */
- private $adjectives = ['female' => [], 'male' => []];
- /* @var array */
- private $nouns = ['female' => [], 'male' => []];
- /* @var array */
- private $verbs = [];
- /* @var object */
- public $page = null;
- /**
- * Constructor
- * @param void
- * @return void
- */
- public function __construct() {
- // Set page
- $this->page = new \Leximo\Post(['template' => GeneratorPage::POST_TEMPLATE]);
- // Set female adjectives
- $this->adjectives['female'] = explode("\n", $this->page->adjectives_female);
- // Set male adjectives
- $this->adjectives['male'] = explode("\n", $this->page->adjectives_male);
- // Set female nouns
- $this->nouns['female'] = explode("\n", $this->page->nouns_female);
- // Set male adjectives
- $this->nouns['male'] = explode("\n", $this->page->nouns_male);
- // Set verbs
- $this->verbs = explode("\n", $this->page->verbs);
- }
- /**
- * Get word
- * @param string $type
- * @param string $gender
- * @param bool $ucfirst
- * @return string
- */
- private function getWord($type, $gender, $ucfirst = false) {
- // Check type and set array
- if(in_array($type, ['adjective', 'noun'])) {
- // Check gender
- $array = $this->{$type . 's'}[($gender ? 'female' : 'male')];
- }
- // Set verbs
- else if ($type == 'verb') {
- $array = $this->verbs;
- }
- // Return with ucfirst
- if($ucfirst) {
- return ucfirst($array[array_rand($array)]);
- }
- // Return plain
- else {
- return array_rand($array[array_rand($array)]);
- }
- }
- /**
- * Get adjective
- * @param string $gender
- * @param bool $ucfirst
- * @return string
- */
- public function getAdjective($gender, $ucfirst = false) {
- return $this->getWord('adjective', $gender, $ucfirst);
- }
- /**
- * Get noun
- * @param bool $ucfirst
- * @return string
- */
- public function getNoun($gender, $ucfirst = false) {
- return $this->getWord('noun', $gender, $ucfirst);
- }
- /**
- * Get verb
- * @param bool $ucfirst
- * @return string
- */
- public function getVerb($ucfirst = false) {
- return $this->getWord('verb', $ucfirst);
- }
- /**
- * Generate
- * @param bool $ucfirst
- * @return string
- */
- public function generate($ucfirst = true) {
- // Set gender
- $gender = array_rand(['female', 'male']);
- // Return password
- return preg_replace("/\r|\n/", '', $this->getWord('adjective', $gender, $ucfirst) . $this->getWord('noun', $gender, $ucfirst) . $this->getWord('verb', $gender, $ucfirst));
- }
- /**
- * Conplicate
- * @param bool $password
- * @return string
- */
- public function complicate($password, $length = 12) {
- // Explode plain password by capitals
- $str1 = array_values(array_filter(preg_split('/(?=[A-Z])/', $password)));
- // Create second password
- $str2 = str_split(\Leximo\Generate::password($length, true, false), 1);
- // Split second password into columns
- $str2 = array_chunk($str2, ceil(count($str2) / count($str1)));
- // Join subarays in second password into string
- foreach($str2 as $key=>$val) $str2[$key] = implode('', $val);
- // Append the shorter string to the longer string
- for($x = 0; $x < count($str1); $x++) {
- $str1[$x] .= $str2[$x];
- }
- // Return
- return implode('', $str1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement