update the address fixture by adding some points

This commit is contained in:
nobohan 2021-04-30 15:15:47 +02:00
parent e9d142f3e8
commit c5faa0b99d

View File

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