Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] płeć z numeru pesel
dentopolis
post 1.05.2017, 19:21:00
Post #1





Grupa: Zarejestrowani
Postów: 252
Pomógł: 0
Dołączył: 14.08.2016

Ostrzeżenie: (0%)
-----


  1. <?php
  2.  
  3. /**
  4.  * Class that validate and retrieve data from PESEL number in object-oriented style
  5.  *
  6.  * Example:
  7.  * $pesel = new Pesel('85042510356');
  8.  * $pesel->getBirthday()->format("d-m-Y")
  9.  * $pesel->getSex()
  10.  * $pesel->getAge("%a")
  11.  *
  12.  *
  13.  * @author Bartosz 'B3k' Pietrzak
  14.  * @license MIT
  15.  */
  16. class Pesel {
  17.  
  18. private static $scales = array(1, 3, 7, 9, 1, 3, 7, 9, 1, 3);
  19. private $pesel, $birthday, $sex, $vaild;
  20.  
  21. /**
  22.   * Constructor
  23.   *
  24.   * @throws InvalidArgumentException
  25.   * @param string $pesel
  26.   * @return void
  27.   */
  28. public function __construct($pesel) {
  29. if (!is_numeric($pesel) || strlen($pesel) != 11 || $pesel == '00000000000') {
  30. throw new InvalidArgumentException("Invaild PESEL number");
  31. } else {
  32. $this->pesel = $pesel;
  33. $this->extract();
  34. }
  35. }
  36.  
  37. /**
  38.   * Return actual PESEL number
  39.   *
  40.   * @return string
  41.   */
  42. public function getPesel() {
  43. return $this->pesel;
  44. }
  45.  
  46. /**
  47.   * Return birthday DateTime object or date in format as parameter
  48.   *
  49.   * Example:
  50.   * $pesel->getBirthday("Y-m-d");
  51.   * $pesel->getBirthday()->format('Y-m-d')
  52.   *
  53.   * @see <a href="http://www.php.net/manual/en/datetime.format.php" target="_blank">http://www.php.net/manual/en/datetime.format.php</a>
  54.   * @param bool|string $format
  55.   * @return Mixed
  56.   */
  57. public function getBirthday($format = FALSE) {
  58. return ($format ? $this->birthday->format($format) : $this->birthday);
  59. }
  60.  
  61. /**
  62.   * Return diff between date from pesel and current time
  63.   * as DateInterval object or string in typed format
  64.   *
  65.   * Example:
  66.   * $pesel->getAge('%a days');
  67.   * $pesel->getAge()->format("%y years");
  68.   *
  69.   * @see <a href="http://www.php.net/manual/en/dateinterval.format.php" target="_blank">http://www.php.net/manual/en/dateinterval.format.php</a>
  70.   * @params string
  71.   * @return DateInterval
  72.   */
  73. public function getAge($format = FALSE) {
  74. $interval = $this->birthday->diff(new DateTime("now"));
  75. return ($format ? $interval->format($format) : $interval);
  76. }
  77.  
  78. /**
  79.   * Return sex: 1 - female , 2 - male
  80.   *
  81.   * @return int
  82.   */
  83. public function getSex() {
  84. return $this->sex;
  85. }
  86.  
  87. /**
  88.   * Return that if pesel checksum control is vaild.
  89.   *
  90.   * @return bool
  91.   */
  92. public function isVaild() {
  93. return $this->vaild;
  94. }
  95.  
  96. /**
  97.   * Extract data from PESEL
  98.   *
  99.   * @return void
  100.   */
  101. private function extract() {
  102. if (($this->pesel [9] % 2) == 0) {
  103. $this->sex = 1;
  104. } else {
  105. $this->sex = 2;
  106. }
  107. if (intval($this->pesel [2] . $this->pesel [3]) >= 81 && intval($this->pesel [2] . $this->pesel [3]) <= 92) {
  108. $month = (($this->pesel [2] . $this->pesel [3]) - 80);
  109. $year = (($this->pesel [0] . $this->pesel [1])) + 1800;
  110. } elseif (intval($this->pesel [2] . $this->pesel [3]) >= 1 && intval($this->pesel [2] . $this->pesel [3]) <= 12) {
  111. $month = ($this->pesel [2] . $this->pesel [3]);
  112. $year = (($this->pesel [0] . $this->pesel [1])) + 1900;
  113. } elseif (intval($this->pesel [2] . $this->pesel [3]) >= 21 && intval($this->pesel [2] . $this->pesel [3]) <= 32) {
  114. $month = (intval($this->pesel [2] . $this->pesel [3]) - 20);
  115. $year = (intval($this->pesel [0] . $this->pesel [1])) + 2000;
  116. } else {
  117. throw new InvalidArgumentException("Invaild PESEL number - birthday part out of range");
  118. }
  119. if (!checkdate($month, intval($this->pesel [4] . $this->pesel [5]), $year)) {
  120. throw new InvalidArgumentException("Invaild PESEL number - birthday part is invaild");
  121. }
  122. $this->birthday = new DateTime($year . "-" . $month . "-" . intval($this->pesel [4] . $this->pesel [5]));
  123. $sum = 0;
  124. for ($x = 0; $x < 10; $x++) {
  125. $sum += self::$scales [$x] * $this->pesel [$x];
  126. }
  127. $res = 10 - $sum % 10;
  128. $res = ($res == 10 ? 0 : $res);
  129. if ($res == $this->pesel [10]) {
  130. $this->vaild = TRUE;
  131. } else {
  132. $this->vaild = FALSE;
  133. }
  134. }
  135.  
  136. }
  137.  
  138.  
  139.  
  140. $pesel = new Pesel('08241305193');
  141. $dataurodzenia=$pesel->getBirthday()->format("d-m-Y");
  142. $plec=$pesel->getSex();
  143. $wiek=$pesel->getAge("%y");
  144.  
  145. echo $dataurodzenia;
  146. echo $plec;
  147. echo $wiek;
Go to the top of the page
+Quote Post
nospor
post 1.05.2017, 19:35:32
Post #2





Grupa: Moderatorzy
Postów: 36 441
Pomógł: 6290
Dołączył: 27.12.2004




A pytanie to?


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
dentopolis
post 1.05.2017, 19:46:03
Post #3





Grupa: Zarejestrowani
Postów: 252
Pomógł: 0
Dołączył: 14.08.2016

Ostrzeżenie: (0%)
-----


jak wyświetlić płeć?
Go to the top of the page
+Quote Post
mortus
post 1.05.2017, 20:49:45
Post #4





Grupa: Zarejestrowani
Postów: 2 178
Pomógł: 596
Dołączył: 25.09.2009
Skąd: Piwniczna-Zdrój

Ostrzeżenie: (0%)
-----


Przecież wyświetlasz płeć jako liczbę 1 lub 2. Jeśli chcesz wyświetlić tekst, to zmień funkcję getSex() i zastosuj prostego ifa, w którym sprawdzisz czy $this->sex to 1, czy dwa i zwrócisz (return) odpowiednio "female" lub "male".
Go to the top of the page
+Quote Post
dentopolis
post 1.05.2017, 21:22:12
Post #5





Grupa: Zarejestrowani
Postów: 252
Pomógł: 0
Dołączył: 14.08.2016

Ostrzeżenie: (0%)
-----


działa. może się komuś przyda

  1. if ($pesel->getSex() == '1') {
  2. echo "K";
  3. }
  4. else {
  5. echo "M";
  6. }
Go to the top of the page
+Quote Post
mortus
post 1.05.2017, 21:31:23
Post #6





Grupa: Zarejestrowani
Postów: 2 178
Pomógł: 596
Dołączył: 25.09.2009
Skąd: Piwniczna-Zdrój

Ostrzeżenie: (0%)
-----


Jeśli porównujesz liczby, to nie używaj cudzysłowów i apostrofów. W tym przypadku wystarczy po prostu 1 zamiast '1'.

PS. Może warto byłoby tę funkcjonalność umieścić w klasie Pesel?

Ten post edytował mortus 1.05.2017, 21:32:36
Go to the top of the page
+Quote Post
nospor
post 2.05.2017, 12:58:51
Post #7





Grupa: Moderatorzy
Postów: 36 441
Pomógł: 6290
Dołączył: 27.12.2004




Cytat
PS. Może warto byłoby tę funkcjonalność umieścić w klasie Pesel?

@Mortus zewnetrznych klas nie powinno sie modyfikowac


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
mortus
post 2.05.2017, 14:27:23
Post #8





Grupa: Zarejestrowani
Postów: 2 178
Pomógł: 596
Dołączył: 25.09.2009
Skąd: Piwniczna-Zdrój

Ostrzeżenie: (0%)
-----


Zgadzam się. Nie zwróciłem uwagi, że to klasa z zewnątrz.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 16.04.2024 - 19:31