From c5faa0b99d1c4d9440f19f54e2e136a409ee23c8 Mon Sep 17 00:00:00 2001 From: nobohan Date: Fri, 30 Apr 2021 15:15:47 +0200 Subject: [PATCH] update the address fixture by adding some points --- .../DataFixtures/ORM/LoadPeople.php | 122 ++++++++++-------- 1 file changed, 71 insertions(+), 51 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/DataFixtures/ORM/LoadPeople.php b/src/Bundle/ChillPersonBundle/DataFixtures/ORM/LoadPeople.php index fa20d3457..e8697ff28 100644 --- a/src/Bundle/ChillPersonBundle/DataFixtures/ORM/LoadPeople.php +++ b/src/Bundle/ChillPersonBundle/DataFixtures/ORM/LoadPeople.php @@ -29,6 +29,7 @@ use Chill\PersonBundle\Entity\Person; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Chill\MainBundle\DataFixtures\ORM\LoadPostalCodes; use Chill\MainBundle\Entity\Address; +use Chill\MainBundle\Doctrine\Model\Point; /** * Load people into database @@ -37,17 +38,17 @@ use Chill\MainBundle\Entity\Address; * @author Marc Ducobu */ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface -{ - +{ + use \Symfony\Component\DependencyInjection\ContainerAwareTrait; - + protected $faker; - + public function __construct() { $this->faker = \Faker\Factory::create('fr_FR'); } - + public function prepare() { //prepare days, month, years @@ -56,57 +57,57 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, Con $this->years[] = $y; $y = $y +1; } while ($y >= 1990); - + $m = 1; do { $this->month[] = $m; $m = $m +1; } while ($m >= 12); - + $d = 1; do { $this->day[] = $d; $d = $d + 1; } while ($d <= 28); } - + public function getOrder() { return 10000; } - + public function load(ObjectManager $manager) { $this->loadRandPeople($manager); $this->loadExpectedPeople($manager); - + $manager->flush(); } - + public function loadExpectedPeople(ObjectManager $manager) { echo "loading expected people...\n"; - + foreach ($this->peoples as $person) { $this->addAPerson($this->fillWithDefault($person), $manager); } } - + public function loadRandPeople(ObjectManager $manager) { echo "loading rand people...\n"; - + $this->prepare(); - + $chooseLastNameOrTri = array('tri', 'tri', 'name', 'tri'); - + $i = 0; - + do { $i++; - + $sex = $this->genders[array_rand($this->genders)]; - + if ($chooseLastNameOrTri[array_rand($chooseLastNameOrTri)] === 'tri' ) { $length = rand(2, 3); $lastName = ''; @@ -117,13 +118,13 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, Con } else { $lastName = $this->lastNames[array_rand($this->lastNames)]; } - + if ($sex === Person::MALE_GENDER) { $firstName = $this->firstNamesMale[array_rand($this->firstNamesMale)]; } else { $firstName = $this->firstNamesFemale[array_rand($this->firstNamesFemale)]; } - + // add an address on 80% of the created people if (rand(0,100) < 80) { $address = $this->getRandomAddress(); @@ -137,7 +138,7 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, Con } else { $address = null; } - + $person = array( 'FirstName' => $firstName, 'LastName' => $lastName, @@ -147,15 +148,15 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, Con 'Address' => $address, 'maritalStatus' => $this->maritalStatusRef[array_rand($this->maritalStatusRef)] ); - + $this->addAPerson($this->fillWithDefault($person), $manager); - + } while ($i <= 100); } - + /** * fill a person array with default value - * + * * @param string[] $specific */ private function fillWithDefault(array $specific) @@ -171,10 +172,10 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, Con 'Address' => null ), $specific); } - + /** * create a new person from array data - * + * * @param array $person * @param ObjectManager $manager * @throws \Exception @@ -200,35 +201,51 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, Con $this->addAccompanyingPeriods($p, $value, $manager); break; } - + //try to add the data using the setSomething function, // if not possible, fallback to addSomething function if (method_exists($p, 'set'.$key)) { call_user_func(array($p, 'set'.$key), $value); } elseif (method_exists($p, 'add'.$key)) { // if we have a "addSomething", we may have multiple items to add - // so, we set the value in an array if it is not an array, and + // so, we set the value in an array if it is not an array, and // will call the function addSomething multiple times if (!is_array($value)) { $value = array($value); } - + foreach($value as $v) { if ($v !== NULL) { call_user_func(array($p, 'add'.$key), $v); } } - - } + + } } $manager->persist($p); echo "add person'".$p->__toString()."'\n"; } - + + /** - * Creata a random address - * + * Create a random point + * + * @return Point + */ + private function getRandomPoint() + { + $lonBrussels = 4.35243; + $latBrussels = 50.84676; + $lon = $lonBrussels + 0.01 * rand(-5, 5); + $lat = $latBrussels + 0.01 * rand(-5, 5); + return Point::fromLonLat($lon, $lat); + } + + + /** + * Create a random address + * * @return Address */ private function getRandomAddress() @@ -238,13 +255,16 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, Con ->setStreetAddress2( rand(0,9) > 5 ? $this->faker->streetAddress : '' ) + ->setPoint( + rand(0,9) > 5 ? $this->getRandomPoint() : NULL + ) ->setPostcode($this->getReference( LoadPostalCodes::$refs[array_rand(LoadPostalCodes::$refs)] )) ->setValidFrom($this->faker->dateTimeBetween('-5 years')) ; } - + private function getCountry($countryCode) { if ($countryCode === NULL) { @@ -257,30 +277,30 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, Con private $maritalStatusRef = ['ms_single', 'ms_married', 'ms_widow', 'ms_separat', 'ms_divorce', 'ms_legalco', 'ms_unknown']; - + private $firstNamesMale = array("Jean", "Mohamed", "Alfred", "Robert", "Justin", "Brian", "Compère", "Jean-de-Dieu", "Charles", "Pierre", "Luc", "Mathieu", "Alain", "Etienne", "Eric", "Corentin", "Gaston", "Spirou", "Fantasio", "Mahmadou", "Mohamidou", "Vursuv", "Youssef" ); - + private $firstNamesFemale = array("Svedana", "Sevlatina", "Irène", "Marcelle", "Corentine", "Alfonsine", "Caroline", "Solange", "Gostine", "Fatoumata", "Nicole", "Groseille", "Chana", "Oxana", "Ivana", "Julie", "Tina", "Adèle" ); - + private $lastNames = array("Diallo", "Bah", "Gaillot", "Martin"); - + private $lastNamesTrigrams = array("fas", "tré", "hu", 'blart', 'van', 'der', 'lin', 'den', 'ta', 'mi', 'net', 'gna', 'bol', 'sac', 'ré', 'jo', 'du', 'pont', 'cas', 'tor', 'rob', 'al', 'ma', 'gone', 'car',"fu", "ka", "lot", "no", "va", "du", "bu", "su", "jau", "tte", 'sir', "lo", 'to', "cho", "car", 'mo','zu', 'qi', 'mu'); - + private $genders = array(Person::MALE_GENDER, Person::FEMALE_GENDER); - + private $years = array(); - + private $month = array(); - + private $day = array(); - + private $peoples = array( array( 'LastName' => "Depardieu", @@ -362,21 +382,21 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, Con 'maritalStatus' => 'ms_legalco' ), ); - - + + private function addAccompanyingPeriods(Person $person, array $periods, ObjectManager $manager) { foreach ($periods as $period) { - + echo "adding new past Accompanying Period..\n"; - + /** @var AccompanyingPeriod $accompanyingPeriod */ $accompanyingPeriod = new AccompanyingPeriod(new \DateTime($period['from'])); $accompanyingPeriod ->setClosingDate(new \DateTime($period['to'])) ->setRemark($period['remark']) ; - + $person->addAccompanyingPeriod($accompanyingPeriod); } }