Passed
Push — feature/issue-170 ( 2776fe )
by Mikaël
49:22
created

ClassMap::defineStringMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File;
4
5
use WsdlToPhp\PackageGenerator\Model\Struct as StructModel;
6
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
7
use WsdlToPhp\PhpGenerator\Element\PhpConstant;
8
use WsdlToPhp\PhpGenerator\Element\PhpAnnotation;
9
use WsdlToPhp\PhpGenerator\Element\PhpProperty;
10
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock;
11
use WsdlToPhp\PackageGenerator\Container\PhpElement\Property as PropertyContainer;
12
use WsdlToPhp\PackageGenerator\Container\PhpElement\Constant as ConstantContainer;
13
14
class ClassMap extends AbstractModelFile
15
{
16
    /**
17
     * @var string
18
     */
19
    const METHOD_NAME = 'get';
20
    /**
21
     * @param ConstantContainer $constants
22
     */
23
    protected function getClassConstants(ConstantContainer $constants)
24
    {
25
    }
26
    /**
27
     * @param PhpConstant $constant
28
     * @return PhpAnnotationBlock|null
29
     */
30
    protected function getConstantAnnotationBlock(PhpConstant $constant)
31
    {
32
    }
33
    /**
34
     * @param PropertyContainer $properties
35
     */
36
    protected function getClassProperties(PropertyContainer $properties)
37
    {
38
    }
39
    /**
40
     * @param PhpProperty $property
41
     * @return PhpAnnotationBlock|null
42
     */
43
    protected function getPropertyAnnotationBlock(PhpProperty $property)
44
    {
45
    }
46
    protected function fillClassMethods()
47
    {
48
        $method = new PhpMethod(self::METHOD_NAME, [], PhpMethod::ACCESS_PUBLIC, false, true, true);
49
        $this->addMethodBody($method);
50
        $this->methods->add($method);
51
    }
52
    /**
53
     * @param PhpMethod $method
54
     * @return PhpAnnotationBlock|null
55
     */
56
    protected function getMethodAnnotationBlock(PhpMethod $method)
57
    {
58
        return new PhpAnnotationBlock([
59
            'Returns the mapping between the WSDL Structs and generated Structs\' classes',
60
            'This array is sent to the \SoapClient when calling the WS',
61
            new PhpAnnotation(AbstractModelFile::ANNOTATION_RETURN, 'string[]'),
62
        ]);
63
    }
64
    /**
65
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getClassAnnotationBlock()
66
     * @return PhpAnnotationBlock
67
     */
68
    protected function getClassAnnotationBlock()
69
    {
70
        return new PhpAnnotationBlock([
71
            'Class which returns the class map definition',
72
            new PhpAnnotation(self::ANNOTATION_PACKAGE, $this->getGenerator()->getOptionPrefix()),
73
        ]);
74
    }
75
    /**
76
     * @param PhpMethod $method
77
     * @return ClassMap
78
     */
79
    protected function addMethodBody(PhpMethod $method)
80
    {
81
        if ($this->getGenerator()->getStructs()->count() > 0) {
82
            $method->addChild('return array(');
83
            foreach ($this->getGenerator()->getStructs() as $struct) {
84
                $this->addStructToClassMapList($method, $struct);
85
            }
86
            $method->addChild(');');
87
        }
88
        return $this;
89
    }
90
    /**
91
     * @param PhpMethod $method
92
     * @param StructModel $struct
93
     * @return ClassMap
94
     */
95
    protected function addStructToClassMapList(PhpMethod $method, StructModel $struct)
96
    {
97
        if ($struct->isStruct() && !$struct->isRestriction()) {
98
            $method->addChild($method->getIndentedString(sprintf('\'%s\' => \'%s\',', $struct->getName(), $this->getStructName($struct)), 1));
99
        }
100
        return $this;
101
    }
102
    /**
103
     * work around for https://bugs.php.net/bug.php?id=69280
104
     * @param StructModel $struct
105
     * @return string
106
     */
107
    protected function getStructName(StructModel $struct)
108
    {
109
        return str_replace('\\', '\\\\', $struct->getPackagedName(true));
110
    }
111
}
112