Namespace_.php 1.64 KB
Newer Older
1
2
<?php
/*
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\ArrowInterface;
10
11
12
use Irstea\PlantUmlBundle\Writer\WriterInterface;

/**
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
13
 * Description of Namespace.
14
 */
15
class Namespace_ extends AbstractNamespace
16
17
{
    /**
18
     * @var AbstractNamespace
19
20
21
22
23
24
25
26
     */
    private $parent;

    /**
     * @var string
     */
    private $name;

27
    public function __construct(AbstractNamespace $parent, $name)
28
29
30
31
32
    {
        $this->parent = $parent;
        $this->name = $name;
    }

33
    /**
34
     * @param ArrowInterface $arrow
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
35
     *
36
37
     * @return self
     */
38
    public function addArrow(ArrowInterface $arrow)
39
40
    {
        $this->parent->addArrow($arrow);
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
41

42
43
44
45
        return $this;
    }

    /**
46
47
48
49
50
51
52
     * @return string
     */
    protected function getNamespacePrefix()
    {
        return $this->parent->getNamespacePrefix() . $this->name . '\\';
    }

53
54
55
56
57
58
    public function getNodeLabel($name)
    {
        $prefix = $this->getNamespacePrefix();
        if (0 === strpos($name, $prefix)) {
            return substr($name, strlen($prefix));
        }
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
59

60
61
62
        return $name;
    }

Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
63
    /**
64
     * @param resource WriterInterface $writer
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
65
     *
66
67
     * @return self
     */
68
    public function writeTo(WriterInterface $writer)
69
70
71
72
73
74
    {
        if ($this->isEmpty()) {
            return;
        }

        $writer
75
            ->writeFormatted("namespace %s {\n", $this->name)
76
77
            ->indent();
        $this
78
79
            ->writeNodesTo($writer)
            ->writeChildrenTo($writer);
80
81
82
83
84
85
86
        $writer
            ->dedent()
            ->write("}\n");

        return $this;
    }
}