Issues (5)

src/Widget.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * This file is part of the sauls/widget package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Component\Widget;
14
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
use Sauls\Component\Collection\ArrayCollection;
17
use Sauls\Component\Collection\Collection;
18
19
abstract class Widget implements WidgetInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    private $id;
25
26
    /**
27
     * @var Collection
28
     */
29
    private $options;
30
31
    /**
32
     * @var string
33
     */
34
    public static $prefix = 'w';
35
36
    /**
37
     * @var int
38
     */
39
    public static $total = 0;
40
41
    /**
42
     *
43
     * @throws \Exception
44
     */
45 19
    private function configure(array $options = []): void
46
    {
47
        try {
48 19
            $resolver = new OptionsResolver;
49 19
            $this->configureOptions($resolver);
50 19
            $this->options = new ArrayCollection($resolver->resolve($this->resolveOptions($options)));
51 17
            $this->initialize();
52 3
        } catch (\Exception $e) {
53 3
            throw $e;
54
        }
55 16
    }
56
57
    /**
58
     * This method is called after all options and variables are configured
59
     * You can safely initialise your variables, or other logic here
60
     */
61 16
    protected function initialize(): void
62
    {
63 16
    }
64
65
    abstract protected function configureOptions(OptionsResolver $resolver): void;
66
67 19
    public function resolveOptions(array $options): array
68
    {
69 19
        return $options;
70
    }
71
72 5
    public function getId($generate = true): string
73
    {
74 5
        if ($generate && $this->id === null) {
75 4
            $this->id = static::$prefix.static::$total++;
76
        }
77
78 5
        return $this->id;
79
    }
80
81 1
    public function setId(string $value): void
82
    {
83 1
        $this->id = $value;
84 1
    }
85
86 1
    public function getOptionsCollection(): Collection
87
    {
88 1
        return $this->options;
89
    }
90
91
92 4
    public function getOptions(): array
93
    {
94 4
        return $this->options->all();
95
    }
96
97 13
    public function getOption($key, $default = null)
98
    {
99 13
        return $this->options->get($key, $default);
100
    }
101
102
    abstract public function render(): string;
103
104
105
    /**
106
     * @throws \Exception
107
     */
108 19
    public function widget(array $options = []): WidgetInterface
109
    {
110 19
        $this->configure($options);
111
112 16
        return $this;
113
    }
114
115 13
    public function __toString()
116
    {
117
        try {
118 13
            $this->startAndFlushOutputBuffers();
119
            try {
120 13
                $out = $this->render();
121 2
            } catch (\Exception $e) {
122 2
                $this->closeNotClosedOutputBuffer();
123 2
                throw $e;
124
            }
125
126 11
            return $this->outputOutputBufferAndWidgetContent($out);
127 2
        } catch (\Exception $e) {
128 2
            return $e->getMessage();
129
        }
130
    }
131
132 13
    private function startAndFlushOutputBuffers(): void
133
    {
134 13
        ob_start();
135 13
        ob_implicit_flush(false);
0 ignored issues
show
false of type false is incompatible with the type integer expected by parameter $flag of ob_implicit_flush(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
        ob_implicit_flush(/** @scrutinizer ignore-type */ false);
Loading history...
136 13
    }
137
138 2
    private function closeNotClosedOutputBuffer(): void
139
    {
140 2
        if (ob_get_level() > 0) {
141 2
            ob_end_clean();
142
        }
143 2
    }
144
145 11
    private function outputOutputBufferAndWidgetContent(string $out): string
146
    {
147 11
        return ob_get_clean().$out;
148
    }
149
}
150