Jelajahi Sumber

Cleanup code

Alan Lam 4 tahun lalu
induk
melakukan
1b856e0338
4 mengubah file dengan 12 tambahan dan 12 penghapusan
  1. 3 3
      controllers/map_overview_ajax.php
  2. 1 1
      controllers/user_ajax.php
  3. 3 3
      engine/test.php
  4. 5 5
      engine/util.php

+ 3 - 3
controllers/map_overview_ajax.php

@@ -85,7 +85,7 @@ if($submit) {
 
 
 					$test = $location_test_array[$location->id] ?? false;
 					$test = $location_test_array[$location->id] ?? false;
 
 
-					$customer_slug = str_replace("-", "_", Util::slug($customer->name));
+					$customer_slug = Util::slug($customer->name, "_");
 
 
 					/** @var User $tester */
 					/** @var User $tester */
 					$tester = null;
 					$tester = null;
@@ -106,7 +106,7 @@ if($submit) {
 							$formatted_start_dts = date("d/m/Y H:i", $unix_start);
 							$formatted_start_dts = date("d/m/Y H:i", $unix_start);
 						}
 						}
 
 
-						if($test->pass === null) {
+						if($test->isPending()) {
 							$icon_file = "{$customer_slug}_pending.png";
 							$icon_file = "{$customer_slug}_pending.png";
 
 
 							$unix_due = null;
 							$unix_due = null;
@@ -227,7 +227,7 @@ if($submit) {
 						$pass_icon = $test->getPassIcon();
 						$pass_icon = $test->getPassIcon();
 					}
 					}
 
 
-					$filename = str_replace("-", "_", Util::slug($customer->name));
+					$filename = Util::slug($customer->name, "_");
 
 
 					$icon = null;
 					$icon = null;
 					if(file_exists("public/img/markers/$filename.png")) {
 					if(file_exists("public/img/markers/$filename.png")) {

+ 1 - 1
controllers/user_ajax.php

@@ -128,7 +128,7 @@ switch($action) {
 
 
 			// Get customer
 			// Get customer
 			$customer = $location->getCustomer();
 			$customer = $location->getCustomer();
-			$customer_slug = str_replace("-", "_", Util::slug($customer->name));
+			$customer_slug = Util::slug($customer->name, "_");
 
 
 			// Test
 			// Test
 			/** @var User $tester */
 			/** @var User $tester */

+ 3 - 3
engine/test.php

@@ -620,7 +620,7 @@ class Test extends Model {
 	 * @return bool
 	 * @return bool
 	 */
 	 */
 	public function isPass(): bool {
 	public function isPass(): bool {
-		if(is_null($this->pass)) return false;
+		if($this->isPending()) return false;
 		return !empty($this->pass);
 		return !empty($this->pass);
 	}
 	}
 
 
@@ -628,14 +628,14 @@ class Test extends Model {
 	 * @return bool
 	 * @return bool
 	 */
 	 */
 	public function isPending(): bool {
 	public function isPending(): bool {
-		return is_null($this->pass);
+		return $this->pass === null;
 	}
 	}
 
 
 	/**
 	/**
 	 * @return bool
 	 * @return bool
 	 */
 	 */
 	public function isFail(): bool {
 	public function isFail(): bool {
-		if(is_null($this->pass)) return false;
+		if($this->isPending()) return false;
 		return empty($this->pass);
 		return empty($this->pass);
 	}
 	}
 
 

+ 5 - 5
engine/util.php

@@ -104,14 +104,14 @@ class Util {
 
 
 	/**
 	/**
 	 * @param string $string
 	 * @param string $string
+	 * @param string $glue
 	 * @return string
 	 * @return string
 	 */
 	 */
-	public static function slug(string $string): string {
+	public static function slug(string $string, string $glue = "-"): string {
 		$slug = strtolower($string);
 		$slug = strtolower($string);
-		$slug = preg_replace("/[^0-9a-z]/i", "-", $slug); // Replace anything not 0-9 a-z with dash
-		$slug = preg_replace("/-+/", "-", $slug); // Replace consequential dashes with dash
-		$slug = trim($slug, "-");
-		return $slug;
+		$slug = preg_replace("/[^\da-z]/i", $glue, $slug); // Replace anything not 0-9 a-z with glue
+		$slug = preg_replace("/" . preg_quote($glue, "/") . "+/", $glue, $slug); // Replace consequential glue with dash
+		return trim($slug, $glue);
 	}
 	}
 
 
 	/**
 	/**