Completed
Push — master ( 6b73b6...a2735d )
by Edgar
03:29
created

BaseFilter::defaultFilterWithChild()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 2
crap 2
1
<?php
2
namespace nstdio\svg\filter;
3
4
use nstdio\svg\container\ContainerInterface;
5
use nstdio\svg\SVGElement;
6
use nstdio\svg\util\KeyValueWriter;
7
8
/**
9
 * Class BaseFilter
10
 *
11
 * @property float  x            = "<coordinate>" The minimum x coordinate for the subregion which restricts
12
 *           calculation and rendering of the given filter primitive.
13
 * @property float  y            = "<coordinate>" The minimum y coordinate for the subregion which restricts
14
 *           calculation and rendering of the given filter primitive.
15
 * @property float  width        = "<length>" The width of the subregion which restricts calculation and
16
 *           rendering of the given filter primitive. See filter primitive subregion. A negative value is an error. A
17
 *           value of zero disables the effect of the given filter primitive (i.e., the result is a transparent black
18
 *           image).
19
 * @property float  height       = "<length>" The height of the subregion which restricts calculation and rendering of
20
 *           the given filter primitive. See filter primitive subregion. A negative value is an error (see Error
21
 *           processing). A value of zero disables the effect of the given filter primitive (i.e., the result is a
22
 *           transparent black image).
23
 * @property string result       = "<filter-primitive-reference>" Assigned name for this filter primitive. If supplied,
24
 *           then graphics that result from processing this filter primitive can be referenced by an 'in' attribute on
25
 *           a subsequent filter primitive within the same 'filter' element. If no value is provided, the output will
26
 *           only be available for re-use as the implicit input into the next filter primitive if that filter primitive
27
 *           provides no value for its 'in' attribute. Note that a <filter-primitive-reference> is not an XML ID;
28
 *           instead, a <filter-primitive-reference> is only meaningful within a given 'filter' element and thus have
29
 *           only local scope. It is legal for the same <filter-primitive-reference> to appear multiple times within
30
 *           the same 'filter' element. When referenced, the <filter-primitive-reference> will use the closest
31
 *           preceding filter primitive with the given result.
32
 * @property string in           = "SourceGraphic | SourceAlpha | BackgroundImage | BackgroundAlpha | FillPaint |
33
 *           StrokePaint | <filter-primitive-reference>" Identifies input for the given filter primitive. The value can
34
 *           be either one of six keywords or can be a string which matches a previous 'result' attribute value within
35
 *           the same 'filter' element. If no value is provided and this is the first filter primitive, then this
36
 *           filter primitive will use SourceGraphic as its input. If no value is provided and this is a subsequent
37
 *           filter primitive, then this filter primitive will use the result from the previous filter primitive as its
38
 *           input. {@link https://www.w3.org/TR/SVG11/filters.html#FilterPrimitiveInAttribute}
39
 * @package  nstdio\svg\filter
40
 * @author   Edgar Asatryan <[email protected]>
41
 */
42
abstract class BaseFilter extends SVGElement
43
{
44
    protected static function defaultFilterWithChild(ContainerInterface $container, array $options)
45
    {
46
        $filter = self::defaultFilter($container, $options['id']);
47
        unset($options['id']);
48
        $child = new static($container);
49
        KeyValueWriter::apply($child->getElement(), $options);
50
51
        $container->append($filter->append($child));
52
53
        return $filter;
54
    }
55
56
    /**
57
     * @param ContainerInterface  $container
58
     * @param                     $filterId
59
     *
60
     * @return Filter
61
     */
62 11
    protected static function defaultFilter(ContainerInterface $container, $filterId)
63
    {
64 11
        return self::filterWithOptions($container, [
65 11
            'id'          => $filterId,
66 11
            'filterUnits' => 'objectBoundingBox',
67 11
            'x'           => '0%',
68 11
            'y'           => '0%',
69 11
            'width'       => '100%',
70 11
            'height'      => '100%',
71 11
        ]);
72
    }
73
74 12
    protected static function filterWithOptions(ContainerInterface $container, array $options)
75
    {
76 12
        $filter = new Filter($container, $options['id']);
77 12
        unset($options['id']);
78 12
        KeyValueWriter::apply($filter->getElement(), $options);
79
80 12
        return $filter;
81
    }
82
}