Completed
Push — master ( 3fe663...b13e46 )
by Adam
07:39
created

Slug::getTemplateFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Slug.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:FormSlug!
9
 * @subpackage     Controls
10
 * @since          1.0.0
11
 *
12
 * @date           08.01.15
13
 */
14
15
namespace IPub\FormSlug\Controls;
16
17
use Nette;
18
use Nette\Application\UI;
19
use Nette\Bridges;
20
use Nette\Forms;
21
22
use IPub;
23
use IPub\FormSlug;
24
use IPub\FormSlug\Exceptions;
25
26
/**
27
 * Form slug control element
28
 *
29
 * @package        iPublikuj:FormSlug!
30
 * @subpackage     Controls
31
 *
32
 * @author         Adam Kadlec <[email protected]>
33
 */
34
class Slug extends Forms\Controls\TextInput
35
{
36
	/**
37
	 * @var string|NULL
38
	 */
39
	private $templateFile;
40
41
	/**
42
	 * @var UI\ITemplate
43
	 */
44
	private $template;
45
46
	/**
47
	 * @var UI\ITemplateFactory
48
	 */
49
	private $templateFactory;
50
51
	/**
52
	 * @var Forms\Controls\BaseControl[]
53
	 */
54
	private $fields = [];
55
56
	/**
57
	 * Toggle box selector
58
	 *
59
	 * @var string
60
	 */
61
	private $toggleBox = '.ipub-slug-box';
62
63
	/**
64
	 * @var bool
65
	 */
66
	private static $registered = FALSE;
67
68
	/**
69
	 * Enable or disable one time auto updating slug field from watched fields
70
	 *
71
	 * @var bool
72
	 */
73
	private $onetimeAutoUpdate = TRUE;
74
75
	/**
76
	 * @param UI\ITemplateFactory $templateFactory
77
	 * @param string|NULL $label
78
	 * @param int|NULL $maxLength
79
	 */
80
	public function __construct(UI\ITemplateFactory $templateFactory, string $label = NULL, int $maxLength = NULL)
81
	{
82
		parent::__construct($label, $maxLength);
83
84
		$this->templateFactory = $templateFactory;
85
	}
86
87
	/**
88
	 * Add filed from which slug will be created
89
	 *
90
	 * @param Forms\Controls\BaseControl $field
91
	 *
92
	 * @return self
93
	 */
94
	public function addField(Forms\Controls\BaseControl $field)
95
	{
96
		// Assign filed to collection
97
		$this->fields[$field->getHtmlId()] = $field;
98
99
		return $this;
100
	}
101
102
	/**
103
	 * Generates control's HTML element
104
	 */
105
	public function getControl() : FormSlug\Utils\Html
106
	{
107
		// Create form input
108
		$input = parent::getControl();
109
110
		$template = $this->getTemplate();
111
112
		// If template file was not defined before...
113
		if ($template->getFile() === NULL) {
114
			// ...try to get base control template file
115
			$templateFile = $this->getTemplateFile();
116
117
			// ...& set it to template engine
118
			$template->setFile($templateFile);
119
		}
120
121
		// Assign vars to template
122
		$template->input = $input;
0 ignored issues
show
Bug introduced by
Accessing input on the interface Nette\Application\UI\ITemplate 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...
123
		$template->value = $this->getValue();
0 ignored issues
show
Bug introduced by
Accessing value on the interface Nette\Application\UI\ITemplate 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...
124
		$template->caption = $this->caption;
0 ignored issues
show
Bug introduced by
Accessing caption on the interface Nette\Application\UI\ITemplate 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...
125
		$template->_form = $this->getForm();
0 ignored issues
show
Bug introduced by
Accessing _form on the interface Nette\Application\UI\ITemplate 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...
126
127
		// Component js settings
128
		$template->settings = [
0 ignored issues
show
Bug introduced by
Accessing settings on the interface Nette\Application\UI\ITemplate 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...
129
			'toggle'  => $this->toggleBox,
130
			'onetime' => $this->onetimeAutoUpdate,
131
			'fields'  => (array_reduce($this->fields, function (array $result, Forms\Controls\BaseControl $row) {
132
				$result[] = '#' . $row->getHtmlId();
133
134
				return $result;
135
			}, [])),
136
		];
137
138
		return FormSlug\Utils\Html::el()
139
			->addHtml($template);
140
	}
141
142
	/**
143
	 * @return UI\ITemplate|Bridges\ApplicationLatte\Template
144
	 */
145
	public function getTemplate() : UI\ITemplate
146
	{
147
		if ($this->template === NULL) {
148
			$this->template = $this->templateFactory->createTemplate();
149
			$this->template->setFile($this->getTemplateFile());
150
		}
151
152
		return $this->template;
153
	}
154
155
	/**
156
	 * Change default control template path
157
	 *
158
	 * @param string $templateFile
159
	 *
160
	 * @return void
161
	 *
162
	 * @throws Exceptions\FileNotFoundException
163
	 */
164
	public function setTemplateFile(string $templateFile)
165
	{
166
		// Check if template file exists...
167
		if (!is_file($templateFile)) {
168
			// ...check if extension template is used
169
			if (is_file(__DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile)) {
170
				$templateFile = __DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile;
171
172
			} else {
173
				// ...if not throw exception
174
				throw new Exceptions\FileNotFoundException(sprintf('Template file "%s" was not found.', $templateFile));
175
			}
176
		}
177
178
		$this->templateFile = $templateFile;
179
	}
180
181
	/**
182
	 * @return string
183
	 */
184
	private function getTemplateFile() : string
185
	{
186
		return $this->templateFile !== NULL ? $this->templateFile : __DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . 'default.latte';
187
	}
188
189
	/**
190
	 * @param UI\ITemplateFactory $templateFactory
191
	 * @param string $method
192
	 *
193
	 * @return void
194
	 */
195
	public static function register(UI\ITemplateFactory $templateFactory, $method = 'addSlug')
196
	{
197
		// Check for multiple registration
198
		if (static::$registered) {
0 ignored issues
show
Bug introduced by
Since $registered is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $registered to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
199
			throw new Exceptions\InvalidStateException('Slug control already registered.');
200
		}
201
202
		static::$registered = TRUE;
0 ignored issues
show
Bug introduced by
Since $registered is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $registered to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
203
204
		$class = function_exists('get_called_class') ? get_called_class() : __CLASS__;
205
		Forms\Container::extensionMethod(
206
			$method, function (Forms\Container $form, $name, $label = NULL, $maxLength = NULL) use ($class, $templateFactory) {
207
			$component = new $class($templateFactory, $label, $maxLength);
208
			$form->addComponent($component, $name);
209
210
			return $component;
211
		}
212
		);
213
	}
214
}
215