From 22e188239113fa3761606ec46b4ca88798094461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Fri, 4 Dec 2020 12:13:43 +0100 Subject: [PATCH] :sparkles: Finit le 4eme jour --- day_4/part_1.php | 13 ++++++ day_4/part_2.php | 13 ++++++ day_4/passport.php | 108 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 day_4/part_1.php create mode 100644 day_4/part_2.php create mode 100644 day_4/passport.php diff --git a/day_4/part_1.php b/day_4/part_1.php new file mode 100644 index 0000000..84630ce --- /dev/null +++ b/day_4/part_1.php @@ -0,0 +1,13 @@ +isValid()) ? 1 : 0; +} + +var_dump($number_of_valid_passports); \ No newline at end of file diff --git a/day_4/part_2.php b/day_4/part_2.php new file mode 100644 index 0000000..ad24399 --- /dev/null +++ b/day_4/part_2.php @@ -0,0 +1,13 @@ +isReallyValid()) ? 1 : 0; +} + +var_dump($number_of_valid_passports); \ No newline at end of file diff --git a/day_4/passport.php b/day_4/passport.php new file mode 100644 index 0000000..2d698ae --- /dev/null +++ b/day_4/passport.php @@ -0,0 +1,108 @@ +{$field} = $data; + } + + return $new_passport; + } + + public function validateByr() + { + return !empty($this->byr) && + is_numeric($this->byr) && + $this->byr >= 1920 && $this->byr <= 2002; + } + + public function validateIyr() + { + return !empty($this->iyr) && + is_numeric($this->iyr) && + $this->iyr >= 2010 && $this->iyr <= 2020; + } + + public function validateEyr() + { + return !empty($this->eyr) && + is_numeric($this->eyr) && + $this->eyr >= 2020 && $this->eyr <= 2030; + } + + public function validateHgt() + { + $value = substr($this->hgt, 0, -2); + $unit = substr($this->hgt, -2); + return !empty($this->hgt) && + in_array($unit, ['in','cm']) && + ($unit === 'cm' ? $value >= 150 && $value <= 193 : $value >= 59 && $value <= 76) + ; + } + + public function validateHcl() + { + return 1 === preg_match('/^#[0-9a-f]{6}$/', $this->hcl); + } + + public function validateEcl() + { + return in_array($this->ecl, ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'], true); + } + + public function validatePid() + { + return 1 === preg_match('/^\d{9}$/', $this->pid); + } + + public function validateCid() + { + return true; + } + + public function isReallyValid() + { + if (!$this->isValid()) { +// echo sprintf("invalide basiquement\n"); + return false; + } + + $vars = get_object_vars($this); + + foreach ($vars as $var_name => $var_value) { + $method = sprintf('validate%s', ucfirst($var_name)); + if (!$this->{$method}()) { + return false; + } + } + + return true; + } + + public function isValid() + { + return + !empty($this->byr) && + !empty($this->iyr) && + !empty($this->eyr) && + !empty($this->hgt) && + !empty($this->hcl) && + !empty($this->ecl) && + !empty($this->pid); + } +} \ No newline at end of file