Completed
Push — master ( 35ee26...e7f3db )
by Edgar
04:19
created

Pattern::getDefaultConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace nstdio\svg\container;
3
4
use nstdio\svg\ElementInterface;
5
use nstdio\svg\shape\Line;
6
use nstdio\svg\shape\Shape;
7
8
/**
9
 * Class Pattern
10
 *
11
 * @package nstdio\svg\container
12
 * @author  Edgar Asatryan <[email protected]>
13
 */
14
class Pattern extends Container
15
{
16
    public function __construct(ElementInterface $parent, $id = null)
17
    {
18
        if ($parent instanceof SVG) {
19
            $defs = $parent->getFirstChild();
20
            $parent = $defs;
21
        }
22
        parent::__construct($parent);
23
24
        $this->id = $id;
25
    }
26
27
    public function getName()
28
    {
29
        return 'pattern';
30
    }
31
32
    public static function withShape(ContainerInterface $container, Shape $shape, array $patternConfig = [], $id = null)
33
    {
34
        $patternConfig = array_merge(self::getDefaultConfig(), $patternConfig);
35
36
        $shapeBox = $shape->getBoundingBox();
37
        $patternConfig['width'] = $shapeBox['width'];
38
        $patternConfig['height'] = $shapeBox['height'];
39
40
        $pattern = (new self($container, $id))->apply($patternConfig);
41
        $shape->selfRemove();
0 ignored issues
show
Bug introduced by
The method selfRemove() cannot be called from this context as it is declared protected in class nstdio\svg\SVGElement.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
42
        $pattern->append($shape);
43
44
        return $pattern;
45
    }
46
47
    public static function verticalHatch(ContainerInterface $container, array $patternConfig = [], array $lineConfig = [], $id = null)
48
    {
49
        return self::hatch($container, $patternConfig, $lineConfig, $id);
50
    }
51
52
    public static function horizontalHatch(ContainerInterface $container, array $patternConfig = [], array $lineConfig = [], $id = null)
53
    {
54
        $patternConfig['patternTransform'] = "rotate(90)";
55
56
        return self::hatch($container, $patternConfig, $lineConfig, $id);
57
    }
58
59
    public static function diagonalHatch(ContainerInterface $container, array $patternConfig = [], array $lineConfig = [], $id = null)
60
    {
61
        $patternConfig['patternTransform'] = "rotate(45)";
62
63
        return self::hatch($container, $patternConfig, $lineConfig, $id);
64
    }
65
66
    public static function crossHatch(ContainerInterface $container, array $patternConfig = [], array $lineConfig = [], $id = null)
67
    {
68
        if (isset($patternConfig['width'])) {
69
            $patternConfig['height'] = $patternConfig['width'];
70
        }
71
        if (isset($patternConfig['height'])) {
72
            $patternConfig['width'] = $patternConfig['height'];
73
        }
74
75
        $pattern = self::hatch($container, $patternConfig, $lineConfig, $id);
76
77
        /** @var Line $firstLine */
78
        $firstLine = $pattern->getFirstChild()->apply(['x1' => 0, 'y1' => 0, 'x2' => $pattern->height, 'y2' => $pattern->width]);
0 ignored issues
show
Bug introduced by
Accessing height on the interface nstdio\svg\ElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing width on the interface nstdio\svg\ElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
79
        $attrs = $firstLine->allAttributes(['x1', 'y1', 'x2', 'y2', 'id']);
80
        $line = new Line($pattern, 0, $pattern->width, $pattern->height, 0);
0 ignored issues
show
Bug introduced by
Accessing width on the interface nstdio\svg\ElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing height on the interface nstdio\svg\ElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
81
        $line->id = null;
82
        $line->apply($attrs);
83
84
        return $pattern;
85
    }
86
87
    public static function straightCrossHatch(ContainerInterface $container, array $patternConfig = [], array $lineConfig = [], $id = null)
88
    {
89
        $patternConfig['patternTransform'] = 'rotate(45)';
90
91
        return self::crossHatch($container, $patternConfig, $lineConfig, $id);
92
    }
93
94
    protected static function hatch(ContainerInterface $container, array $patternConfig = [], array $lineConfig = [], $id = null)
95
    {
96
        $patternConfig = array_merge(self::getDefaultConfig(), $patternConfig);
97
        $lineDefaultConfig = ['stroke' => 'black', 'stroke-width' => 1, 'fill' => 'none'];
98
        $lineConfig = array_merge($lineDefaultConfig, $lineConfig);
99
100
        $pattern = (new self($container, $id))->apply($patternConfig);
101
102
        (new Line($pattern, 0, 0, 0, $pattern->height))->apply($lineConfig);
0 ignored issues
show
Bug introduced by
Accessing height on the interface nstdio\svg\ElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
103
104
        return $pattern;
105
    }
106
107
    protected static function getDefaultConfig()
108
    {
109
        return ['x' => 0, 'y' => 0, 'height' => 4, 'width' => 4, 'patternUnits' => 'userSpaceOnUse'];
110
    }
111
}