HtmlElement::setParent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 *  ____  _     _                       _ _
5
 * |  _ \| |__ | |_ ___ _ __ ___   __ _(_) |
6
 * | |_) | '_ \| __/ _ \ '_ ` _ \ / _` | | |
7
 * |  __/| | | | ||  __/ | | | | | (_| | | |
8
 * |_|   |_| |_|\__\___|_| |_| |_|\__,_|_|_|
9
 *
10
 * This file is part of Kristuff\Phtemail.
11
 *
12
 * (c) Kristuff <[email protected]>
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 *
17
 * @version    0.2.0
18
 * @copyright  2017-2020 Kristuff
19
 */
20
21
namespace Kristuff\Phtemail\Core;
22
23
use Kristuff\Phtemail\HtmlEmailBuilder;
24
25
/** 
26
 * Class HtmlElement
27
 * Abstract base class for all Html email elements
28
 */
29
abstract class HtmlElement extends StylizedElement
30
{
31
    /** 
32
     * The HtmlElement parent element
33
     * 
34
     * @access protected
35
     * @var HtmlElement     $parent
36
     */
37
    protected $parent = null;
38
39
    /** 
40
     * Define the parent of of current htmlElement
41
     * 
42
     * @access public 
43
     * @param HtmlElement   $element    The parent HtmlElement
44
     * 
45
     * @return void
46
     */
47
    public function setParent(HtmlElement $element)
48
    {
49
        $this->parent = $element;
50
    }
51
    
52
    /** 
53
     * Gets the HtmlEmailBuilder parent instance
54
     * Could be direct parent of this element, sub parent (recursive function)
55
     * 
56
     * @access public 
57
     * @return HtmlEmailBuilder
58
     */
59
    public function getBuilder()
60
    {
61
        // try with the first level parent
62
        if ($this->parent instanceof HtmlEmailBuilder){
0 ignored issues
show
introduced by
$this->parent is never a sub-type of Kristuff\Phtemail\HtmlEmailBuilder.
Loading history...
63
            return $this->parent;
64
        }
65
66
        // look for parent of parent...
67
        return $this->parent->getBuilder();
68
    }
69
70
    /** 
71
     * Get the HTML 
72
     *
73
     * @access public
74
     * @param string    $indent
75
     * 
76
     * @return string
77
     */
78
    abstract public function getHtml(string $indent);
79
80
    /** 
81
     * Gets the effective style for the given key. Style can be set 
82
     * at current HtmlElement level, or parent level. If none of them
83
     * are set, returns a default value. 
84
     *
85
     * @access public
86
     * @param string    $key        The style name
87
     * 
88
     * @return string
89
     */
90
    public function getEffectiveStyle(string $key)
91
    {
92
        // first look if key style is defined in this element
93
        if (array_key_exists($key, $this->styles)){
94
            return $this->styles[$key];
95
        }
96
97
        // look into parent
98
        if (!$this->parent instanceof HtmlEmailBuilder){
0 ignored issues
show
introduced by
$this->parent is never a sub-type of Kristuff\Phtemail\HtmlEmailBuilder.
Loading history...
99
            $style = $this->parent->getEffectiveStyle($key);
100
            if (!empty($style)){
101
                return $style;
102
            }
103
        }
104
105
        // get default
106
        if (in_array($key, $this->knowStyles)){
107
            switch ($key){
108
                case 'padding-top':         return '0';
109
                case 'padding-bottom':      return '0';
110
                case 'padding-left':        return '0';
111
                case 'padding-right':       return '0';
112
                case 'font-size':           return $this->getBuilder()->emailBodyFontSize();
113
                case 'font-family':         return $this->getBuilder()->emailBodyFont();
114
                case 'color':               return $this->getBuilder()->emailBodyColor();
115
                case 'background-color':    return $this->getBuilder()->emailBodyBackground();
116
            }
117
        }
118
    }
119
120
    /** 
121
     * Gets the inline styles for current element. Styles may be set
122
     * explicitly at current HtmlElement level, or part of mandatory 
123
     * styles some HtmlElements will force to use. 
124
     *
125
     * @access public
126
     * @param string    $key        The style name
127
     * 
128
     * @return string
129
     */
130
    protected function getInlineStyles()
131
    {
132
        $stylesCollection = [];
133
        foreach ($this->styles as $key => $value){
134
           $stylesCollection[] = $key. ':' . $value;
135
        }
136
        
137
        // check for mandatory styles
138
        foreach ($this->mandatoryStyles as $style){
139
            if (!array_key_exists($style, $this->styles)){
140
                $stylesCollection[] = $style . ':' . $this->getEffectiveStyle($style); 
141
            }
142
        }
143
144
        return !empty($stylesCollection) ? ' style="'. implode(';', $stylesCollection) .'"' : '';
145
    }
146
147
}