-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
379 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
|
||
/** | ||
* This file is part of the Phalcon. | ||
* | ||
* (c) Phalcon Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Phalcon\Html\Helper; | ||
|
||
use Phalcon\Html\Exception; | ||
|
||
/** | ||
* Class Title | ||
* | ||
* @property array $append | ||
* @property string $delimiter | ||
* @property string $indent | ||
* @property array $prepend | ||
* @property string $title | ||
* @property string $separator | ||
*/ | ||
class Title extends AbstractHelper | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected append = []; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected delimiter = ""; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected indent = " "; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected prepend = []; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected title = ""; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected separator = ""; | ||
|
||
/** | ||
* Sets the separator and returns the object back | ||
* | ||
* @param string $separator | ||
* @param string|null $indent | ||
* @param string|null $delimiter | ||
* | ||
* @return Title | ||
*/ | ||
public function __invoke( | ||
string separator = "", | ||
string indent = null, | ||
string delimiter = null | ||
) -> <Title> { | ||
let this->delimiter = PHP_EOL, | ||
this->indent = indent, | ||
this->separator = separator; | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* Returns the title tags | ||
* | ||
* @return string | ||
* @throws Exception | ||
*/ | ||
public function __toString() | ||
{ | ||
var delimiter, indent, items; | ||
|
||
let items = array_merge( | ||
this->prepend, | ||
[this->title], | ||
this->append | ||
); | ||
|
||
let indent = this->indent ? this->indent : ""; | ||
let delimiter = this->delimiter ? this->delimiter : ""; | ||
|
||
let this->append = [], | ||
this->prepend = [], | ||
this->title = ""; | ||
|
||
return indent | ||
. this->renderFullElement( | ||
"title", | ||
implode(this->separator, items), | ||
[], | ||
true | ||
) | ||
. delimiter; | ||
} | ||
|
||
/** | ||
* Appends text to current document title | ||
* | ||
* @param string $text | ||
* @param bool $raw | ||
* | ||
* @return Title | ||
*/ | ||
public function append(string text, bool raw = false) -> <Title> | ||
{ | ||
let text = raw ? text : this->escaper->html(text); | ||
|
||
let this->append[] = text; | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* Returns the title | ||
* | ||
* @return string | ||
*/ | ||
public function get() -> string | ||
{ | ||
return this->title; | ||
} | ||
|
||
/** | ||
* Sets the title | ||
* | ||
* @param string $text | ||
* @param bool $raw | ||
* | ||
* @return Title | ||
*/ | ||
public function set(string text, bool raw = false) -> <Title> | ||
{ | ||
let text = raw ? text : this->escaper->html(text); | ||
|
||
let this->title = text; | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* Prepends text to current document title | ||
* | ||
* @param string $text | ||
* @param bool $raw | ||
* | ||
* @return Title | ||
*/ | ||
public function prepend(string text, bool raw = false) -> <Title> | ||
{ | ||
let text = raw ? text : this->escaper->html(text); | ||
|
||
let this->prepend[] = text; | ||
|
||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Phalcon Framework. | ||
* | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phalcon\Test\Unit\Html\Helper\Title; | ||
|
||
use Codeception\Example; | ||
use Phalcon\Escaper; | ||
use Phalcon\Html\Helper\Title; | ||
use Phalcon\Html\TagFactory; | ||
use UnitTester; | ||
|
||
class UnderscoreInvokeCest | ||
{ | ||
/** | ||
* Tests Phalcon\Html\Helper\Title :: __invoke() | ||
* | ||
* @param UnitTester $I | ||
* @param Example $example | ||
* | ||
* @dataProvider getExamples | ||
* @since 2020-01-07 | ||
*/ | ||
public function htmlHelperTitleUnderscoreInvoke(UnitTester $I, Example $example) | ||
{ | ||
$I->wantToTest('Html\Helper\Title - __invoke() - ' . $example['message']); | ||
|
||
$escaper = new Escaper(); | ||
$helper = new Title($escaper); | ||
|
||
$result = $helper($example['separator'], $example['indent'], $example['delimiter']); | ||
|
||
if (!empty($example['prepend'])) { | ||
foreach ($example['prepend'] as $text => $raw) { | ||
$result->prepend($text, $raw); | ||
} | ||
} | ||
|
||
$result->set($example['title'], $example['titleRaw']); | ||
|
||
if (!empty($example['append'])) { | ||
foreach ($example['append'] as $text => $raw) { | ||
$result->append($text, $raw); | ||
} | ||
} | ||
|
||
$I->assertEquals($example['get'], $result->get()); | ||
$I->assertEquals($example['render'], (string) $result); | ||
|
||
$factory = new TagFactory($escaper); | ||
$locator = $factory->newInstance('title'); | ||
|
||
$result = $locator($example['separator'], $example['indent'], $example['delimiter']); | ||
|
||
if (!empty($example['prepend'])) { | ||
foreach ($example['prepend'] as $text => $raw) { | ||
$result->prepend($text, $raw); | ||
} | ||
} | ||
|
||
$result->set($example['title'], $example['titleRaw']); | ||
|
||
if (!empty($example['append'])) { | ||
foreach ($example['append'] as $text => $raw) { | ||
$result->append($text, $raw); | ||
} | ||
} | ||
|
||
$I->assertEquals($example['get'], $result->get()); | ||
$I->assertEquals($example['render'], (string) $result); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
private function getExamples(): array | ||
{ | ||
return [ | ||
[ | ||
'message' => 'only title', | ||
'separator' => '', | ||
'indent' => null, | ||
'delimiter' => null, | ||
'prepend' => [], | ||
'title' => 'Accounting', | ||
'titleRaw' => false, | ||
'append' => [], | ||
'get' => 'Accounting', | ||
'render' => '<title>Accounting</title>', | ||
], | ||
[ | ||
'message' => 'only title escaped', | ||
'separator' => '', | ||
'indent' => null, | ||
'delimiter' => null, | ||
'prepend' => [], | ||
'title' => '<Accounting>', | ||
'titleRaw' => false, | ||
'append' => [], | ||
'get' => '<Accounting>', | ||
'render' => '<title><Accounting></title>', | ||
], | ||
[ | ||
'message' => 'only title raw', | ||
'separator' => '', | ||
'indent' => null, | ||
'delimiter' => null, | ||
'prepend' => [], | ||
'title' => '<Accounting>', | ||
'titleRaw' => true, | ||
'append' => [], | ||
'get' => '<Accounting>', | ||
'render' => '<title><Accounting></title>', | ||
], | ||
[ | ||
'message' => 'only title indent', | ||
'separator' => '', | ||
'indent' => '--', | ||
'delimiter' => null, | ||
'prepend' => [], | ||
'title' => 'Accounting', | ||
'titleRaw' => true, | ||
'append' => [], | ||
'get' => 'Accounting', | ||
'render' => '--<title>Accounting</title>', | ||
], | ||
[ | ||
'message' => 'only title delimiter', | ||
'separator' => '', | ||
'indent' => null, | ||
'delimiter' => '+', | ||
'prepend' => [], | ||
'title' => 'Accounting', | ||
'titleRaw' => true, | ||
'append' => [], | ||
'get' => 'Accounting', | ||
'render' => '<title>Accounting</title>+', | ||
], | ||
[ | ||
'message' => 'only title prepend', | ||
'separator' => ' | ', | ||
'indent' => null, | ||
'delimiter' => null, | ||
'prepend' => [ | ||
'Home >' => false, | ||
'Admin >' => false, | ||
], | ||
'title' => 'Accounting', | ||
'titleRaw' => false, | ||
'append' => [], | ||
'get' => 'Accounting', | ||
'render' => '<title>Home > | Admin > | Accounting</title>', | ||
], | ||
[ | ||
'message' => 'only title append', | ||
'separator' => ' | ', | ||
'indent' => null, | ||
'delimiter' => null, | ||
'prepend' => [], | ||
'title' => 'Accounting', | ||
'titleRaw' => false, | ||
'append' => [ | ||
'< Admin' => false, | ||
'< Home' => false, | ||
], | ||
'get' => 'Accounting', | ||
'render' => '<title>Accounting | < Admin | < Home</title>', | ||
], | ||
[ | ||
'message' => 'only title prepend raw', | ||
'separator' => ' | ', | ||
'indent' => null, | ||
'delimiter' => null, | ||
'prepend' => [ | ||
'Home >' => true, | ||
'Admin >' => true, | ||
], | ||
'title' => 'Accounting', | ||
'titleRaw' => false, | ||
'append' => [], | ||
'get' => 'Accounting', | ||
'render' => '<title>Home > | Admin > | Accounting</title>', | ||
], | ||
[ | ||
'message' => 'only title append', | ||
'separator' => ' | ', | ||
'indent' => null, | ||
'delimiter' => null, | ||
'prepend' => [], | ||
'title' => 'Accounting', | ||
'titleRaw' => false, | ||
'append' => [ | ||
'< Admin' => true, | ||
'< Home' => true, | ||
], | ||
'get' => 'Accounting', | ||
'render' => '<title>Accounting | < Admin | < Home</title>', | ||
], | ||
]; | ||
} | ||
} |