<?php

namespace inter\utils;

class Mkdir
{

    private string $namespace;
    private string $path;
    private array $pathArr;
    public function __construct($namespace)
    {
        $this->namespace = $namespace;
    }

    public function Namespace2PathArr(): array
    {
        $this->namespace2();
        return $this->pathArr;
    }

    public  function Namespace2Path(): string
    {
        $this->namespace2();
        return $this->path;
    }

    public  function mkdirByNamespace()
    {
        $this->Namespace2Path();
        if (empty($this->path)) return;

        $path = "";
        foreach ($this->Namespace2PathArr() as $tmp) {
            if (empty($tmp)) continue;

            $path .= ("/" . $tmp . "/");
            $path = trim($path, '/');

            if (!file_exists($path)) {
                mkdir($path);
            }
        }
    }

    protected function  namespace2()
    {
        if (empty($this->path)) {
            $this->path = trim(str_replace("\\", "/", $this->namespace), ' ');
            $this->pathArr = explode("/", $this->path);
        }
    }
}
