diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..90ec22b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.svn diff --git a/Faker.class.php b/Faker.class.php new file mode 100644 index 0000000..7d2db57 --- /dev/null +++ b/Faker.class.php @@ -0,0 +1,64 @@ +rand_num(), $char ); - } - return join( $result ); - } - - /** - * Replaces all occurrences of ? with a random letter - * - * @param string $string String you wish to have parsed - * @return string - * @author Caius Durling - */ - public function lexify( $string ) - { - foreach ( str_split( $string ) as $char ) { - $result[] = str_replace( '?', $this->rand_letter(), $char ); - } - return join( $result ); - } - - /** - * Replaces all occurrences of # with a random number and - * replaces all occurrences of ? with a random letter - * - * @param string $string String you wish to have parsed - * @return string - * @author Caius Durling - */ - public function bothify( $string ) - { - $result = $this->numerify( $string ); - $result = $this->lexify( $result ); - return $result; - } - -} - -?> \ No newline at end of file diff --git a/index.php b/index.php index e174115..6712d24 100644 --- a/index.php +++ b/index.php @@ -1,4 +1,5 @@ - + + Generating random data @@ -6,47 +7,47 @@ array( + 'name' => array( 'name', - 'first_name', + 'firstName', 'surname', 'prefix', 'suffix', ), - 'Address' => array( - 'street_suffix', - 'street_name', - 'street_address', - 'abode_address', - array( 'abode_address', true ), - 'uk_county', - 'uk_country', - 'post_code', - 'us_state', - 'us_state_abbr', - 'zip_code' + 'address' => array( + 'streetSuffix', + 'streetName', + 'streetAddress', + 'abodeAddress', + array( 'abodeAddress', true ), + 'ukCounty', + 'ukCountry', + 'postCode', + 'usState', + 'usStateAbbr', + 'zipCode' ), - 'Phone_Number' => array( - 'phone_number' + 'phoneNumber' => array( + 'phoneNumber' ), - 'Company' => array( + 'company' => array( 'name', 'suffix', - 'catch_phrase', + 'catchPhrase', 'bs', ), - 'Internet' => array( - 'domain_suffix', - 'domain_word', - 'domain_name', - 'user_name', - array( 'user_name', 'caius durling' ), + 'internet' => array( + 'domainSuffix', + 'domainWord', + 'domainName', + 'userName', + array( 'userName', 'caius durling' ), 'email', array( 'email', 'caius durling' ), - 'free_email', - array( 'free_email', 'caius durling' ), + 'freeEmail', + array( 'freeEmail', 'caius durling' ), ), - 'Lorem' => array( + 'lorem' => array( 'words', 'sentence', 'sentences', @@ -57,41 +58,39 @@ ?> - $f = new Faker + Faker:: - - $methods) { - foreach ($methods as $method) { - if ( is_array( $method ) ) { - ?> - - - - - - - - - - - - - - + + $methods) { + foreach ($methods as $method) { + if ( is_array( $method ) ) { + ?> + + + + + + + + + + + + + + +
PHP Statement Output
$f->->( )$class->$method[0]( '$method[1]' ) ?>$class->$method[0]( $method[1] ) ?>
$f->->$class->$method ?>
Faker::->( )$method[0]( '$method[1]' ) ?>$method[0]( $method[1] ) ?>
Faker::->()$method() ?>
diff --git a/lib/address.php b/lib/address.php deleted file mode 100644 index c2843d9..0000000 --- a/lib/address.php +++ /dev/null @@ -1,112 +0,0 @@ -$var(); - } - - public function street_suffix() - { - - return parent::random( self::$_street_suffix ); - } - - public function street_name() - { - $method = parent::random( self::$_street_name_formats ); - $result[] = parent::__get('Name')->$method; - $result[] = self::street_suffix(); - return implode($result, " "); - } - - public function street_address() - { - return parent::numerify( implode( " ", array( '#####' , self::street_name() ) ) ); - } - - public function abode_address( $include_street=false ) - { - if ( $include_street ) { - $str[] = '#####'; - } - $formats = array( - 'Apt. ###', - 'Suite ###' - ); - $str[] = parent::random( $formats ); - if ( $include_street ) { - $str[] = self::street_name(); - } - return parent::numerify( implode( " ", $str ) ); - } - - - ######### UK Only ########### - - public function uk_county() - { - return parent::random( self::$_uk_counties ); - } - - public function uk_country() - { - return parent::random( self::$_uk_countries ); - } - - public function post_code() - { - $a = parent::random( self::$_uk_postcode_formats ); - $result = parent::bothify( $a ); - return strtoupper($result); - } - - - ###### American Only ######## - - public function us_state() - { - return parent::random( self::$_us_states ); - } - - public function us_state_abbr() - { - return parent::random( self::$_us_states_abbr ); - } - - public function zip_code() - { - $a = parent::random( self::$_us_zipcode_formats ); - $result = parent::numerify( $a ); - return $result; - } - -} - -?> \ No newline at end of file diff --git a/lib/company.php b/lib/company.php deleted file mode 100644 index 216a6b8..0000000 --- a/lib/company.php +++ /dev/null @@ -1,69 +0,0 @@ -$var(); - } - - public function name() - { - - - foreach ( parent::random( self::$_name_formats ) as $elem ) { - $result[] = $elem; - // And now replace it if it needs it - if ( $elem == 'surname' ) { - $result[ count($result) - 1 ] = parent::__get('Name')->surname; - } - } - return join( $result ); - } - - public function suffix() - { - return parent::random( self::$_suffix ); - } - - public function catch_phrase() - { - foreach (self::$_catch_phrase_words as &$word) { - $result[] = parent::random( $word ); - } - return join( $result, " " ); - } - - public function bs() - { - foreach (self::$_bs_words as &$word) { - $result[] = parent::random( $word ); - } - return join( $result, " " ); - } - -} - -?> \ No newline at end of file diff --git a/lib/fkAddress.class.php b/lib/fkAddress.class.php new file mode 100644 index 0000000..2d36c15 --- /dev/null +++ b/lib/fkAddress.class.php @@ -0,0 +1,147 @@ +random(self::$_street_suffix); + } + + public function streetName () + { + $method = fkUtils::getInstance()->random(self::$_street_name_formats); + $result[] = Faker::name()->$method(); + $result[] = self::streetSuffix(); + return implode($result, " "); + } + + public function streetAddress () + { + return fkUtils::getInstance()->numerify(implode(" ", array('#####', self::streetName()))); + } + + public function abodeAddress ($include_street = false) + { + if ($include_street) { + $str[] = '#####'; + } + $formats = array('Apt. ###', 'Suite ###'); + $str[] = fkUtils::getInstance()->random($formats); + if ($include_street) { + $str[] = self::streetName(); + } + return fkUtils::getInstance()->numerify(implode(" ", $str)); + } + + ######### UK Only ########### + + + public function ukCounty () + { + return fkUtils::getInstance()->random(self::$_uk_counties); + } + + public function ukCountry () + { + return fkUtils::getInstance()->random(self::$_uk_countries); + } + + public function postCode () + { + $a = fkUtils::getInstance()->random(self::$_uk_postcode_formats); + $result = fkUtils::getInstance()->bothify($a); + return strtoupper($result); + } + + ###### American Only ######## + + + public function usState () + { + return fkUtils::getInstance()->random(self::$_us_states); + } + + public function usStateAbbr () + { + return fkUtils::getInstance()->random(self::$_us_states_abbr); + } + + public function zipCode () + { + $a = fkUtils::getInstance()->random(self::$_us_zipcode_formats); + $result = fkUtils::getInstance()->numerify($a); + return $result; + } + + function __set ($property, $value) + { + throw new Exception('Unknow property "' . $property . '"'); + } +} \ No newline at end of file diff --git a/lib/fkCompany.class.php b/lib/fkCompany.class.php new file mode 100644 index 0000000..14895f2 --- /dev/null +++ b/lib/fkCompany.class.php @@ -0,0 +1,133 @@ +random(self::$_name_formats) as $elem) { + $result[] = $elem; + // And now replace it if it needs it + if ($elem == 'surname') { + $result[count($result) - 1] = Faker::name()->surname(); + } + } + return join($result); + } + + public function suffix () + { + return fkUtils::getInstance()->random(self::$_suffix); + } + + public function catchPhrase () + { + foreach (self::$_catch_phrase_words as &$word) { + $result[] = fkUtils::getInstance()->random($word); + } + return join($result, " "); + } + + public function bs () + { + foreach (self::$_bs_words as &$word) { + $result[] = fkUtils::getInstance()->random($word); + } + return join($result, " "); + } + + function __set ($property, $value) + { + throw new Exception('Unknow property "' . $property . '"'); + } + +} + +?> \ No newline at end of file diff --git a/lib/fkDate.class.php b/lib/fkDate.class.php new file mode 100644 index 0000000..8247a54 --- /dev/null +++ b/lib/fkDate.class.php @@ -0,0 +1,39 @@ + \ No newline at end of file diff --git a/lib/fkInternet.class.php b/lib/fkInternet.class.php new file mode 100644 index 0000000..19731e1 --- /dev/null +++ b/lib/fkInternet.class.php @@ -0,0 +1,91 @@ +random(self::$_domainSuffix); + } + + public function domainWord () + { + $result = explode(' ', Faker::company()->name()); + $result = $result[0]; + $result = strtolower($result); + $result = preg_replace("/\W/", '', $result); + return $result; + } + + public function domainName () + { + $result[] = $this->domainWord(); + $result[] = $this->domainSuffix(); + return join($result, '.'); + } + + public function userName ($name = null) + { + if ($name) { + return $this->sanitiseName($name); + } + + // get first_name, surname + $n = Faker::name(); + $a = fkUtils::getInstance()->random(self::$_name_formats); + + foreach ($a as $method) { + $na[] = $n->$method(); + } + + // run sanitiseName() + $na = join(' ', $na); + $result = $this->sanitiseName($na); + return $result; + } + + public function email ($name = null) + { + return join(array($this->userName($name), $this->domainName()), "@"); + } + + public function freeEmail ($name = null) + { + return join(array($this->userName($name), fkUtils::getInstance()->random(self::$_free)), "@"); + } + + protected function sanitiseName ($name) + { + $name = strtolower($name); + $n = explode(' ', $name); + $n = preg_replace("/\W/", "", $n); + $d = array('.', '_'); + // Randomise the array order + shuffle($n); + return join($n, fkUtils::getInstance()->random($d)); + } + + function __set ($property, $value) + { + throw new Exception('Unknow property "' . $property . '"'); + } + +} + +?> \ No newline at end of file diff --git a/lib/fkLorem.class.php b/lib/fkLorem.class.php new file mode 100644 index 0000000..61aca5c --- /dev/null +++ b/lib/fkLorem.class.php @@ -0,0 +1,119 @@ +wordList(); + shuffle($w); + return array_slice($w, 0, $num); + } + + /** + * Generate a random sentence + * + * @param string $word_count around how many words the sentence should contain + * @return string + * @author Caius Durling + */ + public function sentence ($word_count = 4) + { + $words = $this->words($word_count + rand(0, 4)); + $words[0] = ucwords($words[0]); + return join($words, " ") . "."; + } + + /** + * Generate an array of sentences + * + * @param string $sentence_count around how many sentences to generate + * @return array + * @author Caius Durling + */ + public function sentences ($sentence_count = 3) + { + $c = $sentence_count + rand(0, 3); + for ($i = 0; $i < $c; $i ++) { + $s[] = $this->sentence(); + } + return $s; + } + + /** + * Generate a single paragraph + * + * @param string $sentence_count around how many sentences the paragraph should contain + * @return string + * @author Caius Durling + */ + public function paragraph ($sentence_count = 3) + { + return join($this->sentences($sentence_count + rand(0, 3)), " "); + } + + /** + * Generate an array of paragraphs + * + * @param string $paragraph_count how many paragraphs to return + * @return array + * @author Caius Durling + */ + public function paragraphs ($paragraph_count = 3) + { + for ($i = 0; $i < $paragraph_count; $i ++) { + $p[] = $this->paragraph(); + } + return $p; + } + + function __set ($property, $value) + { + throw new Exception('Unknow property "' . $property . '"'); + } + +} + +?> \ No newline at end of file diff --git a/lib/fkName.class.php b/lib/fkName.class.php new file mode 100644 index 0000000..8e2e5e6 --- /dev/null +++ b/lib/fkName.class.php @@ -0,0 +1,396 @@ +random(self::$_formats); + + foreach ($a as $method) { + $b[] = $this->$method(); + } + + $result = join($b, " "); + + return $result; + } + + public function firstName () + { + return fkUtils::getInstance()->random(self::$_firstName); + } + + public function surname () + { + return fkUtils::getInstance()->random(self::$_surname); + } + + public function prefix () + { + return fkUtils::getInstance()->random(self::$_prefix); + } + + public function suffix () + { + return fkUtils::getInstance()->random(self::$_suffix); + } + + function __set ($property, $value) + { + throw new Exception('Unknow property "' . $property . '"'); + } +} + +?> \ No newline at end of file diff --git a/lib/fkPhoneNumber.class.php b/lib/fkPhoneNumber.class.php new file mode 100644 index 0000000..a3334cd --- /dev/null +++ b/lib/fkPhoneNumber.class.php @@ -0,0 +1,33 @@ +numerify(fkUtils::getInstance()->random(self::$_formats)); + } + + function __set ($property, $value) + { + throw new Exception('Unknow property "' . $property . '"'); + } +} + +?> \ No newline at end of file diff --git a/lib/fkUtils.class.php b/lib/fkUtils.class.php new file mode 100644 index 0000000..f3beb5b --- /dev/null +++ b/lib/fkUtils.class.php @@ -0,0 +1,116 @@ +randNum(), $char); + } + return join($result); + } + + /** + * Replaces all occurrences of ? with a random letter + * + * @param string $string String you wish to have parsed + * @return string + * @author Caius Durling + */ + public function lexify ($string) + { + foreach (str_split($string) as $char) { + $result[] = str_replace('?', $this->randLetter(), $char); + } + return join($result); + } + + /** + * Replaces all occurrences of # with a random number and + * replaces all occurrences of ? with a random letter + * + * @param string $string String you wish to have parsed + * @return string + * @author Caius Durling + */ + public function bothify ($string) + { + $result = $this->numerify($string); + $result = $this->lexify($result); + return $result; + } + + function __set ($property, $value) + { + throw new Exception('Unknow property "' . $property . '"'); + } + +} + +?> \ No newline at end of file diff --git a/lib/internet.php b/lib/internet.php deleted file mode 100644 index 236f979..0000000 --- a/lib/internet.php +++ /dev/null @@ -1,96 +0,0 @@ -$var; - } else { - return $this->$var(); - } - } - - public function domain_suffix() - { - return parent::random( self::$_domain_suffix ); - } - - public function domain_word() - { - $result = explode( ' ', parent::__get('Company')->name ); - $result = $result[0]; - $result = strtolower( $result ); - $result = preg_replace( "/\W/", '', $result ); - return $result; - } - - public function domain_name() - { - $result[] = $this->domain_word; - $result[] = $this->domain_suffix; - return join( $result, '.' ); - } - - public function user_name( $name = null ) - { - if ( $name ) { - return $this->sanitise_name( $name ); - } - - // get first_name, surname - $n = parent::__get('Name'); - $a = parent::random( self::$_name_formats ); - - foreach ( $a as $method ) { - $na[] = $n->$method; - } - - // run sanitise_name() - $na = join( ' ', $na ); - $result = $this->sanitise_name( $na ); - return $result; - } - - public function email( $name = null) - { - return join( array( $this->user_name($name), $this->domain_name() ), "@" ); - } - - public function free_email( $name = null) - { - return join( array( $this->user_name($name), parent::random( self::$_free ) ), "@" ); - } - - protected function sanitise_name( $name ) - { - $name = strtolower( $name ); - $n = explode(' ', $name ); - $n = preg_replace("/\W/", "", $n); - $d = array( '.', '_'); - // Randomise the array order - shuffle( $n ); - return join( $n, parent::random( $d ) ); - } - -} - -?> \ No newline at end of file diff --git a/lib/lorem.php b/lib/lorem.php deleted file mode 100644 index 999bf28..0000000 --- a/lib/lorem.php +++ /dev/null @@ -1,104 +0,0 @@ -$var(); - } - - private static function word_list() - { - return array("alias", "consequatur", "aut", "perferendis", "sit", "voluptatem", "accusantium", "doloremque", "aperiam", "eaque", "ipsa", "quae", "ab", "illo", "inventore", "veritatis", "et", "quasi", "architecto", "beatae", "vitae", "dicta", "sunt", "explicabo", "aspernatur", "aut", "odit", "aut", "fugit", "sed", "quia", "consequuntur", "magni", "dolores", "eos", "qui", "ratione", "voluptatem", "sequi", "nesciunt", "neque", "dolorem", "ipsum", "quia", "dolor", "sit", "amet", "consectetur", "adipisci", "velit", "sed", "quia", "non", "numquam", "eius", "modi", "tempora", "incidunt", "ut", "labore", "et", "dolore", "magnam", "aliquam", "quaerat", "voluptatem", "ut", "enim", "ad", "minima", "veniam", "quis", "nostrum", "exercitationem", "ullam", "corporis", "nemo", "enim", "ipsam", "voluptatem", "quia", "voluptas", "sit", "suscipit", "laboriosam", "nisi", "ut", "aliquid", "ex", "ea", "commodi", "consequatur", "quis", "autem", "vel", "eum", "iure", "reprehenderit", "qui", "in", "ea", "voluptate", "velit", "esse", "quam", "nihil", "molestiae", "et", "iusto", "odio", "dignissimos", "ducimus", "qui", "blanditiis", "praesentium", "laudantium", "totam", "rem", "voluptatum", "deleniti", "atque", "corrupti", "quos", "dolores", "et", "quas", "molestias", "excepturi", "sint", "occaecati", "cupiditate", "non", "provident", "sed", "ut", "perspiciatis", "unde", "omnis", "iste", "natus", "error", "similique", "sunt", "in", "culpa", "qui", "officia", "deserunt", "mollitia", "animi", "id", "est", "laborum", "et", "dolorum", "fuga", "et", "harum", "quidem", "rerum", "facilis", "est", "et", "expedita", "distinctio", "nam", "libero", "tempore", "cum", "soluta", "nobis", "est", "eligendi", "optio", "cumque", "nihil", "impedit", "quo", "porro", "quisquam", "est", "qui", "minus", "id", "quod", "maxime", "placeat", "facere", "possimus", "omnis", "voluptas", "assumenda", "est", "omnis", "dolor", "repellendus", "temporibus", "autem", "quibusdam", "et", "aut", "consequatur", "vel", "illum", "qui", "dolorem", "eum", "fugiat", "quo", "voluptas", "nulla", "pariatur", "at", "vero", "eos", "et", "accusamus", "officiis", "debitis", "aut", "rerum", "necessitatibus", "saepe", "eveniet", "ut", "et", "voluptates", "repudiandae", "sint", "et", "molestiae", "non", "recusandae", "itaque", "earum", "rerum", "hic", "tenetur", "a", "sapiente", "delectus", "ut", "aut", "reiciendis", "voluptatibus", "maiores", "doloribus", "asperiores", "repellat"); - } - - /** - * Generate an array of random words - * - * @param string $num how many words to return - * @return array - * @author Caius Durling - */ - public function words( $num=3 ) - { - $w = $this->word_list; - shuffle( $w ); - return array_slice( $w, 0, $num ); - } - - /** - * Generate a random sentence - * - * @param string $word_count around how many words the sentence should contain - * @return string - * @author Caius Durling - */ - public function sentence( $word_count = 4 ) - { - $words = $this->words( $word_count + rand(0, 4) ); - $words[0] = ucwords( $words[0] ); - return join( $words, " ") . "."; - } - - /** - * Generate an array of sentences - * - * @param string $sentence_count around how many sentences to generate - * @return array - * @author Caius Durling - */ - public function sentences( $sentence_count = 3 ) - { - $c = $sentence_count + rand(0, 3); - for ($i=0; $i < $c; $i++) { - $s[] = $this->sentence; - } - return $s; - } - - /** - * Generate a single paragraph - * - * @param string $sentence_count around how many sentences the paragraph should contain - * @return string - * @author Caius Durling - */ - public function paragraph( $sentence_count = 3 ) - { - return join( $this->sentences( $sentence_count + rand(0, 3) ), " " ); - } - - /** - * Generate an array of paragraphs - * - * @param string $paragraph_count how many paragraphs to return - * @return array - * @author Caius Durling - */ - public function paragraphs( $paragraph_count = 3) - { - for ($i=0; $i < $paragraph_count; $i++) { - $p[] = $this->paragraph; - } - return $p; - } - -} - -?> \ No newline at end of file diff --git a/lib/name.php b/lib/name.php deleted file mode 100644 index 4ad3672..0000000 --- a/lib/name.php +++ /dev/null @@ -1,71 +0,0 @@ -$var(); - } - - public function name() - { - $a = parent::random( self::$_formats ); - - foreach ($a as $method) { - $b[] = $this->$method; - } - - $result = join($b, " "); - - return $result; - } - - public function first_name() - { - return parent::random( self::$_first_name ); - } - - - public function surname() - { - return parent::random( self::$_surname ); - } - - public function prefix() - { - return parent::random( self::$_prefix ); - } - - public function suffix() - { - return parent::random( self::$_suffix ); - } -} - -?> \ No newline at end of file diff --git a/lib/phone_number.php b/lib/phone_number.php deleted file mode 100644 index 20012ef..0000000 --- a/lib/phone_number.php +++ /dev/null @@ -1,33 +0,0 @@ -$var(); - } - - public function phone_number() - { - return parent::numerify( parent::random( self::$_formats ) ); - } -} - - -?> \ No newline at end of file diff --git a/readme.md b/readme.md index 208893e..e01e34a 100644 --- a/readme.md +++ b/readme.md @@ -6,33 +6,43 @@ Requirements: * PHP5 -To use it simply create a new faker object and then call a subclass & method. +To use it simply call the Faker facade. eg: - - Name->name; - ?> - -You only need to include `faker.php` as it includes all the files under lib/ automatically as they are needed. + + sentence(); + ?> + +You only need to include `Faker.class.php` as it autoload all the files under lib/ automatically as they are needed. ## CHANGELOG +* 0.4 + Updates by Stéphane R. Richard + + Deep refactoring, involving : + - Files, classes and methods names + - No more inheritance from Faker + - Moved Faker to fkUtils + - Added a facade class Faker, which provides roughly the same API as the old Faker class + - No more \_\_get() and \_\_set() magic, use of explicit methods call instead + + Added SPL autoloading + * 0.3 - Updates by Fiona + Updates by Fiona * 0.2 - Updates by Adam for speed using PHP native functions and static variables + Updates by Adam for speed using PHP native functions and static variables - > From my own tests it performs roughly 10x faster than 0.1-dev -- ifunk + > From my own tests it performs roughly 10x faster than 0.1-dev -- ifunk * 0.1-dev - Initial Release + Initial Release ## LICENCE @@ -40,7 +50,8 @@ Released under the MIT Licence Copyright (c) 2008 Caius Durling Portions Copyright (c) 2008 Adam Royle -Portions Copyright (c) 2008 Fiona Burrows +Portions Copyright (c) 2008 Fiona Burrows +Portions Copyright (c) 2010 Stéphane R. Richard Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: