Wellness Living Live Coding Challenge

Most popular filesname:

.gitignore (3 instances)

Root folder contents:

\Dir::__set_state(array(
   'id' => '{3665F570-C01E-7731-1855-17BCF19BABFC}',
   'name' => '/',
   'dateStamp' => 1764987714,
   'mimeType' => NULL,
   'contents' => 
  array (
    0 => 
    \File::__set_state(array(
       'id' => '{14362DA6-738A-B672-F925-E6B77D23F4DC}',
       'name' => 'Readme.txt',
       'dateStamp' => 1764987714,
       'mimeType' => 'text/plain',
       'contents' => 'Hello World!',
    )),
    1 => 
    \File::__set_state(array(
       'id' => '{2960E0B0-031F-EC03-6CB5-443D4EF781CC}',
       'name' => 'favicon.ico',
       'dateStamp' => 1764987714,
       'mimeType' => 'image/icon',
       'contents' => 'data:image/x-icon;base64,AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAAAAAAABivMcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAERERERERERERERAAAAERERERAREREBERERARERERAREREBEREREBEREBERERERAREQEREREREBEQERERERERARAREREREREBEQEREREREBERAREREREQEREQEREREQERERARERERAREREQERERAREREREAAAARERERERERERERH//wAA+B8AAPfvAADv9wAA7/cAAN/7AADf+wAAv/0AAL/9AADf+wAA3/sAAO/3AADv9wAA9+8AAPgfAAD//wAA',
    )),
    2 => 
    \File::__set_state(array(
       'id' => '{63D7A36A-7360-7563-18A2-E3EF8FAE834A}',
       'name' => '.gitignore',
       'dateStamp' => 1764987714,
       'mimeType' => 'text/plain',
       'contents' => '.idea\\n',
    )),
    3 => 
    \Dir::__set_state(array(
       'id' => '{CA70DE5E-BB95-69AC-7EFF-9CEA1B38687E}',
       'name' => 'assets',
       'dateStamp' => 1764987714,
       'mimeType' => NULL,
       'contents' => 
      array (
        0 => 
        \File::__set_state(array(
           'id' => '{DD7C467B-2F97-286D-142C-7F3B325DAC9C}',
           'name' => '.gitignore',
           'dateStamp' => 1764987714,
           'mimeType' => 'text/plain',
           'contents' => '.idea\\n',
        )),
        1 => 
        \File::__set_state(array(
           'id' => '{AF9F47FF-FF43-3817-E60A-4D4E0E3F9786}',
           'name' => 'Readme.txt',
           'dateStamp' => 1764987714,
           'mimeType' => 'text/plain',
           'contents' => 'Empty Folder for now',
        )),
      ),
    )),
    4 => 
    \Dir::__set_state(array(
       'id' => '{0A4758F0-567B-01BE-70BC-5C60748221B1}',
       'name' => 'source',
       'dateStamp' => 1764987714,
       'mimeType' => NULL,
       'contents' => 
      array (
        0 => 
        \Dir::__set_state(array(
           'id' => '{D25243A5-BEF2-3ED6-462A-F450EA929CC1}',
           'name' => 'Controllers',
           'dateStamp' => 1764987714,
           'mimeType' => NULL,
           'contents' => 
          array (
          ),
        )),
      ),
    )),
    5 => 
    \Dir::__set_state(array(
       'id' => '{3CDD9731-F6EB-F493-38CD-59E1AD2EDAD7}',
       'name' => 'vendors',
       'dateStamp' => 1764987714,
       'mimeType' => NULL,
       'contents' => 
      array (
        0 => 
        \File::__set_state(array(
           'id' => '{702AE151-CA5E-60B6-8A49-3E90B362165E}',
           'name' => '.gitignore',
           'dateStamp' => 1764987714,
           'mimeType' => NULL,
           'contents' => '*',
        )),
      ),
    )),
  ),
))

Source Code:

<?php
/* Create a class hierarchy to represent files and directories.
Directories can contain files and other directories.
For both files and directories their names and creation dates are known.
We would like the classes, any properties, dependencies between classes but no methods, not even getters/setters or constructors.
The code you will be creating does not need to be run.

Add a method to find the most popular name between files and
directories, including all subdirectories
*/

class FileElement {
    protected 
$id;
    protected 
$name;
    protected 
$dateStamp;
    protected 
$mimeType;
    protected 
$contents;

    public function 
__construct(
        
string $name,
        
mixed $contents,
        
string $mimeType null
    
) {
        
$this->dateStamp time();
        
$this->id getGUID();
        
$this->name $name;
        
$this->contents $contents;
        
$this->mimeType $mimeType;
    }

    
/**
     * Getter for contents property;
     * @return mixed
     */
    
public function getContents() {
        return 
$this->contents;
    }

    
/**
     * Getter for name property;
     * @return string
     */
    
public function getName() {
        return 
$this->name;
    }

     
/**
     * Is this element a direcrory?
     * @return bool
     */
    
public function isDir() {
        return 
false;
    }
}

class 
Dir extends FileElement {

     
/**
     * Is this element a direcrory?
     * @return bool
     */
    
public function isDir() {
        return 
true;
    }

    
/*
     * Helper Function for cataloging file names
     * @param $namesArray
     * @return mixed
    */
    
public function findAllNames($namesArray = []) {
        foreach (
$this->getContents() as $item) {
            if (
$item->isDir()) {    
                
$namesArray $item->findAllNames($namesArray);
            }
            
$name $item->getName();
            if (!isset(
$namesArray[$name])) {
                
$namesArray[$name] = 0;
            }
            
$namesArray[$name] ++;
        }
        return 
$namesArray;
    }

    public function 
getPopular() {
        
$names $this->findAllNames();
        
arsort($names);
        return 
array_key_first($names);
    }

    public function 
getPopularCount() {
        
$names $this->findAllNames();
        
arsort($names);
        return 
$names[array_key_first($names)];
    }

    public function 
__toString()
    {
        return 
"<pre>" var_export($thistrue) . "</pre>";
    }
}

class 
File extends FileElement {
     
/**
     * Is this element a direcrory?
     * @return bool
     */
    
public function isDir() {
        return 
false;
    }
}

/**
 * Function to generate Microsoft-style GUID for entry indexing
 * @return string
 */
function getGUID(){
    if (
function_exists('com_create_guid')){
        return 
com_create_guid();
    }
    
mt_srand((int) (double)microtime()*10000);
    
$charid strtoupper(md5(uniqid(rand(), true)));
    
$hyphen chr(45);// "-"
    
$uuid chr(123)// "{"
        
.substr($charid08).$hyphen
        
.substr($charid84).$hyphen
        
.substr($charid,124).$hyphen
        
.substr($charid,164).$hyphen
        
.substr($charid,20,12)
        .
chr(125);// "}"
    
return $uuid;
}

$root = new Dir(
    
'/',
    [
        new 
File(
            
'Readme.txt',
            
'Hello World!',
            
'text/plain'
        
),
        new 
File(
            
'favicon.ico',
            
'data:image/x-icon;base64,AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAAAAAAABivMcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAERERERERERERERAAAAERERERAREREBERERARERERAREREBEREREBEREBERERERAREQEREREREBEQERERERERARAREREREREBEQEREREREBERAREREREQEREQEREREQERERARERERAREREQERERAREREREAAAARERERERERERERH//wAA+B8AAPfvAADv9wAA7/cAAN/7AADf+wAAv/0AAL/9AADf+wAA3/sAAO/3AADv9wAA9+8AAPgfAAD//wAA',
            
'image/icon'
        
),
        new 
File(
            
'.gitignore',
            
'.idea\n',
            
'text/plain'
        
),
        new 
Dir(
            
'assets',
            [
                new 
File(
                    
'.gitignore',
                    
'.idea\n',
                    
'text/plain'
                
),
                new 
File(
                    
'Readme.txt',
                    
'Empty Folder for now',
                    
'text/plain'
                
)       
            ]
        ),
        new 
Dir(
            
'source',
            [
                new 
Dir(
                    
'Controllers',
                    []
                ),
            ]
        ),
        new 
Dir(
            
'vendors',
            [
                new 
File(
                    
'.gitignore',
                    
'*'
                
)
            ]
        )
    ]
);
?>
<html>
<head>
<title>Wellness Living Live Coding Challenge</title>
<style>
    .answer {
        display: inline-block;
        border: 1px dashed #f00;
        background: #fee;
        padding: 0.5em;
    }
    .scrollbox {
        border: 1px solid #000;
        padding: 1em;
        margin: 0;
        height: 200px;
        overflow: auto;
        background: #ffe;
    }
    .scrollbox pre {
        margin: 0;
        padding: 0;
    }
    .scrollbox.code {
        background: #efe;
    }
    h2 {
        margin: 1.5em 0 0.5em;
    }
    footer {
        margin: 1em;
        text-align: center;
    }
</style>
</head>
<body>
<h1>Wellness Living Live Coding Challenge</h1>
<h2>Most popular filesname:</h2>
<div class="answer"><strong><?php print $root->getPopular(); ?></strong> (<?php print $root->getPopularCount(); ?> instances)</div>
<h2>Root folder contents:</h2>
<div class="scrollbox">
    <?php print $root?>
</div>
<h2>Source Code:</h2>
<div class="scrollbox code">
    <?php show_source('index.php'); ?>
</div>
<footer>&copy; <a href="mailto:martin@classaxe.com">Martin Francis</a>, Friday November 3rd, 2023</footer>
</body>
</html>