AbstractNamespace.php 1.67 KB
Newer Older
1
<?php declare(strict_types=1);
2
/*
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
3
4
 * Copyright (C) 2016-2017 IRSTEA
 * All rights reserved.
5
6
 */

7
namespace Irstea\PlantUmlBundle\Model\Namespace_\Php;
8

9
use Irstea\PlantUmlBundle\Model\Namespace_\AbstractNamespace as Base;
10
11
12
13
use Irstea\PlantUmlBundle\Model\NamespaceInterface;
use Irstea\PlantUmlBundle\Writer\WriterInterface;

/**
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
14
 * Description of Namespace.
15
 */
16
abstract class AbstractNamespace extends Base
17
18
19
20
21
22
23
24
{
    /**
     * @var NamespaceInterface
     */
    private $children = [];

    /**
     * @param type $namespaceName
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
25
     *
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
     * @return NamespaceInterface
     */
    public function getNamespace($namespaceName)
    {
        $namespaceName = trim($namespaceName, '\\');
        if (!$namespaceName) {
            return $this;
        }
        @list($head, $tail) = explode('\\', $namespaceName, 2);
        if (!isset($this->children[$head])) {
            $this->children[$head] = new Namespace_($this, $head);
        }
        if (empty($tail)) {
            return $this->children[$head];
        }
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
41

42
43
44
45
46
47
48
49
        return $this->children[$head]->getNamespace($tail);
    }

    /**
     * @return $string
     */
    abstract protected function getNamespacePrefix();

50
51
    public function getNodeId($name)
    {
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
52
        return str_replace('\\', '.', $name) . '_node';
53
54
    }

55
56
    /**
     * @param WriterInterface $writer
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
57
     *
58
59
     * @return self
     */
60
    protected function writeChildrenTo(WriterInterface $writer)
61
62
    {
        foreach ($this->children as $child) {
63
            $child->writeTo($writer);
64
        }
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
65

66
67
68
69
70
71
72
73
74
75
76
        return $this;
    }

    /**
     * @return bool
     */
    protected function isEmpty()
    {
        return parent::isEmpty() && empty($this->children);
    }
}