Marek Marek - 2 months ago 7
Javascript Question

Can't get the return value from a PHP class function

I used an online converter to turn the Javascript* at http://7xl.org/mugenzaru.htm into PHP. The code is valid but I don't know how to make the function $mugenzaru->doIt return the variable $resultString. Any ideas?


  • a Јapanеse Ⅼоrem Іpsuⅿ duⅿⅿy cоpy gеnerator



Original Javascript: http://7xl.org/mugenzaru.js.htm

Generated and slightly adapted PHP: http://7xl.org/mugenzaru.php.htm

Answer

There are differences in how "equivalent" php functions work, so there are problems there, specifically the pickOne() calculation. Math.random() does not work the same as random() so the number is way off. See if this gets you closer. One note, the Japanese characters are causing SO to think its spam, so i left those out but they do need to be filled in those arrays:

class   Characters
    {   
        private $mugenzaru;

        public  function getString(Mugenzaru $mugenzaru,$charType = 'kangi')
            {
                $this->mugenzaru    =   $mugenzaru;
                $kanji              =   array(2, 2, 2, 2, 2, 2, 1, 1, 1, 3, 3, 4);
                $hiragana           =   array(1, 1, 1, 1, 2, 2, 2, 3, 3, 4);
                $katakana           =   array(2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 6, 6, 7);
                $punctuation        =   array(1);
                $lengthWeighting    =   ${$charType};
                $charStringLength   =   $this->mugenzaru->eenieMeenie()->pickOne($lengthWeighting);
                $resultString       =   "";
                while ($charStringLength != 0) {
                    $resultString .= $this->mugenzaru->eenieMeenie()->pickOne($this->mugenzaru->{$charType}());
                    $charStringLength--;
                }
                return $resultString;
            }
    }

class   EenieMeenie 
    {
        public  function pickOne($characterList)
            {
                $rand   =   "0.".str_pad(mt_rand(1,9999999999999999),16,0,STR_PAD_LEFT);
                $var    =   floor($rand*count($characterList));
                return $characterList[$var];
            }
    }

class Mugenzaru
    {
        private $length =   10;

        public function eenieMeenie()
            {
                return new EenieMeenie();
            }

        public  function __call($name, $args = false)
            {
                $kanji      =   array(/*** array of kanji ***/);
                $hiragana   =   array(/*** array of hiragana ***/);
                $katakana   =   array(/*** array of katakana ***/);
                $punctuation    =   array(/*** array of punctuation ***/);

                return ${$name};
            }

        public  function setLength($num = 1000)
            {
                $this->length   =   $num;
                return $this;
            }

        public  function doIt(Characters $char) {
                $weighting = array(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 4, 4);
                $resultString = "";
                $targetLength = $this->length;

                while(strlen($resultString) < $targetLength) {
                    $charWeight =   $this->eenieMeenie()->pickOne($weighting);
                    switch ($charWeight){
                        case 1:
                            $resultString .= $char->getString($this,'kanji');
                            break;
                        case 2:
                            $resultString .= $char->getString($this,'hiragana');
                            break;
                        case 3:
                            $resultString .= $char->getString($this,'katakana');
                            break;
                        case 4:
                            $resultString .= $char->getString($this,'punctuation');
                            break;
                        default:
                            $resultString .= $char->getString($this,'kanji');
                    }
                }

                return $resultString;
            }
    }


$Mugenzaru  =    new Mugenzaru();
echo $Mugenzaru->setLength(1000)->doIt(new Characters);