Skip to content

Instantly share code, notes, and snippets.

@mateuslopes
Created May 14, 2021 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mateuslopes/e5888cc8d86b63b923bd43955348e4e5 to your computer and use it in GitHub Desktop.
Save mateuslopes/e5888cc8d86b63b923bd43955348e4e5 to your computer and use it in GitHub Desktop.
PHP Numerology Calculator
<?php
class NumerologyCalc {
const BASE_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const VALID_FINAL_NUMBERS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44];
public $numerology = null;
function __construct($input = "", $show = false) {
if (strlen($input) > 0){
$this->numerology = self::calculate_numerology($input);
if ($show) $this->show();
}
}
// calculates the numerological value for a letter position
private static function letter_position_value($pos) {
$mod = ($pos % 9) + 1;
return $mod;
}
// returns the base letter position in the base letters string
private static function base_letter_position($letter, $extendChars = "") {
return strpos(self::BASE_LETTERS . $extendChars, $letter);
}
// validates if it is a valid and allowed base letter
private static function is_valid_base_letter($letter, $extendChars = "") {
return self::base_letter_position($letter, $extendChars) !== false;
}
// clean and prepare the input string before calculating numerology
private static function prepare_input_string($input) {
$processed_input = str_split(strtoupper($input));
$result = "";
foreach ($processed_input as $idx => $letter){
if (self::is_valid_base_letter($letter, " ")) $result .= $letter;
}
return $result;
}
// validates if the final numerological result is a valid final number (including master numbers)
private static function is_valid_final_number($num) {
return array_search($num, self::VALID_FINAL_NUMBERS) !== false;
}
// returns the numeric value of one single letter or 0 if not a valid letter
public static function letter_numeric_value($letter) {
if (!self::is_valid_base_letter($letter)) return 0;
return self::letter_position_value(self::base_letter_position($letter));
}
private static function calculate_numerology_absolute($input) {
$prepared_input = self::prepare_input_string($input);
$total = 0;
$letters = [];
$split_input = str_split($prepared_input);
foreach ($split_input as $idx => $letter){
$lv = self::letter_numeric_value($letter);
$letters[] = [$letter, $lv];
$total += $lv;
}
return (object)[
"original" => $input,
"input" => $prepared_input,
"total" => $total,
"letters" => $letters
];
}
public static function resume_final_number($number) {
$total = 0;
$all = str_split("" . $number);
foreach ($all as $idx => $num){
$total += (int)$num;
}
if (self::is_valid_final_number($total)) return $total;
return self::resume_final_number($total);
}
public static function calculate_numerology($input) {
$numerology = self::calculate_numerology_absolute($input);
$total = self::resume_final_number($numerology->total);
$numerology->final_number = $total;
return $numerology;
}
public function show(){
$br = chr(10).chr(13);
echo "Input: ".$this->numerology->input . $br;
echo "Total: ".$this->numerology->total . $br;
echo "Final: ".$this->numerology->final_number . $br;
echo "---------------------".$br;
}
}
/* USAGE:
$n = new NumerologyCalc("PayTalk");
$n->show();
or
new NumerologyCalc("PaidTalk", true);
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment