Moved user settings to separate class

This commit is contained in:
Marcin Kurczewski 2014-05-13 23:10:56 +02:00
parent 331691e332
commit b811e76318
14 changed files with 268 additions and 123 deletions

View file

@ -114,7 +114,7 @@ class Access
return array_filter(PostSafety::getAll(), function($safety) return array_filter(PostSafety::getAll(), function($safety)
{ {
return Access::check(new Privilege(Privilege::ListPosts, $safety->toString())) return Access::check(new Privilege(Privilege::ListPosts, $safety->toString()))
and Auth::getCurrentUser()->hasEnabledSafety($safety); and Auth::getCurrentUser()->getSettings()->hasEnabledSafety($safety);
}); });
} }

View file

@ -81,11 +81,11 @@ class UserController
if (!is_array($suppliedSafety)) if (!is_array($suppliedSafety))
$suppliedSafety = []; $suppliedSafety = [];
foreach (PostSafety::getAll() as $safety) foreach (PostSafety::getAll() as $safety)
$user->enableSafety($safety, in_array($safety->toInteger(), $suppliedSafety)); $user->getSettings()->enableSafety($safety, in_array($safety->toInteger(), $suppliedSafety));
$user->enableEndlessScrolling(InputHelper::get('endless-scrolling')); $user->getSettings()->enableEndlessScrolling(InputHelper::get('endless-scrolling'));
$user->enablePostTagTitles(InputHelper::get('post-tag-titles')); $user->getSettings()->enablePostTagTitles(InputHelper::get('post-tag-titles'));
$user->enableHidingDislikedPosts(InputHelper::get('hide-disliked-posts')); $user->getSettings()->enableHidingDislikedPosts(InputHelper::get('hide-disliked-posts'));
if ($user->getAccessRank()->toInteger() != AccessRank::Anonymous) if ($user->getAccessRank()->toInteger() != AccessRank::Anonymous)
UserModel::save($user); UserModel::save($user);
@ -185,7 +185,7 @@ class UserController
$safety = new PostSafety($safety); $safety = new PostSafety($safety);
$safety->validate(); $safety->validate();
$user->enableSafety($safety, !$user->hasEnabledSafety($safety)); $user->getSettings()->enableSafety($safety, !$user->getSettings()->hasEnabledSafety($safety));
if ($user->getAccessRank()->toInteger() != AccessRank::Anonymous) if ($user->getAccessRank()->toInteger() != AccessRank::Anonymous)
UserModel::save($user); UserModel::save($user);

View file

@ -7,6 +7,48 @@ class TextHelper
return preg_match($emailRegex, $email); return preg_match($emailRegex, $email);
} }
public static function toIntegerOrNull($x)
{
if ($x === true or $x === false)
return null;
if ($x === 0 or $x === '0')
return 0;
$y = intval($x);
if ($y !== 0)
{
if (!preg_match('/^-?\d+$/', $x))
return null;
return $y;
}
return null;
}
public static function toBooleanOrNull($x)
{
switch (strtolower($x))
{
case '1':
case 'true':
case 'on':
case 'yes':
case 'y':
return true;
case '0':
case 'false':
case 'off':
case 'no':
case 'n':
return false;
default:
return null;
}
}
public static function replaceTokens($text, array $tokens) public static function replaceTokens($text, array $tokens)
{ {
foreach ($tokens as $key => $value) foreach ($tokens as $key => $value)

View file

@ -13,16 +13,17 @@ final class UserEntity extends AbstractEntity implements IValidatable
private $joinDate; private $joinDate;
private $lastLoginDate; private $lastLoginDate;
private $accessRank; private $accessRank;
public $settings;
private $banned = false; private $banned = false;
private $__passwordChanged = false; private $settings;
private $__password; private $_passwordChanged = false;
private $_password;
public function fillNew() public function fillNew()
{ {
$this->setAccessRank(new AccessRank(AccessRank::Anonymous)); $this->setAccessRank(new AccessRank(AccessRank::Anonymous));
$this->setPasswordSalt(md5(mt_rand() . uniqid())); $this->setPasswordSalt(md5(mt_rand() . uniqid()));
$this->settings = new UserSettings();
} }
public function fillFromDatabase($row) public function fillFromDatabase($row)
@ -36,9 +37,9 @@ final class UserEntity extends AbstractEntity implements IValidatable
$this->emailConfirmed = $row['email_confirmed']; $this->emailConfirmed = $row['email_confirmed'];
$this->joinDate = $row['join_date']; $this->joinDate = $row['join_date'];
$this->lastLoginDate = $row['last_login_date']; $this->lastLoginDate = $row['last_login_date'];
$this->settings = $row['settings'];
$this->banned = $row['banned']; $this->banned = $row['banned'];
$this->setAccessRank(new AccessRank($row['access_rank'])); $this->setAccessRank(new AccessRank($row['access_rank']));
$this->settings = new UserSettings($row['settings']);
} }
public function validate() public function validate()
@ -47,9 +48,7 @@ final class UserEntity extends AbstractEntity implements IValidatable
$this->validatePassword(); $this->validatePassword();
$this->validateAccessRank(); $this->validateAccessRank();
$this->validateEmails(); $this->validateEmails();
$this->settings->validate();
if (!$this->getSetting(UserModel::SETTING_SAFETY))
$this->setSetting(UserModel::SETTING_SAFETY, (new PostSafety(PostSafety::Safe))->toFlag());
if (empty($this->getAccessRank())) if (empty($this->getAccessRank()))
throw new Exception('No access rank detected'); throw new Exception('No access rank detected');
@ -88,14 +87,14 @@ final class UserEntity extends AbstractEntity implements IValidatable
if (empty($this->getPasswordHash())) if (empty($this->getPasswordHash()))
throw new Exception('Trying to save user with no password into database'); throw new Exception('Trying to save user with no password into database');
if (!$this->__passwordChanged) if (!$this->_passwordChanged)
return; return;
$config = getConfig(); $config = getConfig();
$passMinLength = intval($config->registration->passMinLength); $passMinLength = intval($config->registration->passMinLength);
$passRegex = $config->registration->passRegex; $passRegex = $config->registration->passRegex;
$password = $this->__password; $password = $this->_password;
if (strlen($password) < $passMinLength) if (strlen($password) < $passMinLength)
throw new SimpleException('Password must have at least %d characters', $passMinLength); throw new SimpleException('Password must have at least %d characters', $passMinLength);
@ -249,8 +248,8 @@ final class UserEntity extends AbstractEntity implements IValidatable
public function setPassword($password) public function setPassword($password)
{ {
$this->__passwordChanged = true; $this->_passwordChanged = true;
$this->__password = $password; $this->_password = $password;
$this->passHash = UserModel::hashPassword($password, $this->passSalt); $this->passHash = UserModel::hashPassword($password, $this->passSalt);
} }
@ -275,86 +274,9 @@ final class UserEntity extends AbstractEntity implements IValidatable
return $url; return $url;
} }
public function getSetting($key) public function getSettings()
{ {
$settings = json_decode($this->settings, true); return $this->settings;
return isset($settings[$key])
? $settings[$key]
: null;
}
public function setSetting($key, $value)
{
$settings = json_decode($this->settings, true);
$settings[$key] = $value;
$settings = json_encode($settings);
if (strlen($settings) > 200)
throw new SimpleException('Too much data');
$this->settings = $settings;
}
public function hasEnabledSafety(PostSafety $safety)
{
$all = $this->getSetting(UserModel::SETTING_SAFETY);
if (!$all)
return $safety->toInteger() == (new PostSafety(PostSafety::Safe))->toInteger();
return ($all & $safety->toFlag()) == $safety->toFlag();
}
public function enableSafety(PostSafety $safety, $enabled)
{
$all = $this->getSetting(UserModel::SETTING_SAFETY);
$new = $all;
if (!$enabled)
{
$new &= ~$safety->toFlag();
}
else
{
$new |= $safety->toFlag();
}
$this->setSetting(UserModel::SETTING_SAFETY, $new);
}
public function hasEnabledHidingDislikedPosts()
{
$ret = $this->getSetting(UserModel::SETTING_HIDE_DISLIKED_POSTS);
if ($ret === null)
$ret = !getConfig()->browsing->showDislikedPostsDefault;
return $ret;
}
public function enableHidingDislikedPosts($enabled)
{
$this->setSetting(UserModel::SETTING_HIDE_DISLIKED_POSTS, $enabled ? 1 : 0);
}
public function hasEnabledPostTagTitles()
{
$ret = $this->getSetting(UserModel::SETTING_POST_TAG_TITLES);
if ($ret === null)
$ret = getConfig()->browsing->showPostTagTitlesDefault;
return $ret;
}
public function enablePostTagTitles($enabled)
{
$this->setSetting(UserModel::SETTING_POST_TAG_TITLES, $enabled ? 1 : 0);
}
public function hasEnabledEndlessScrolling()
{
$ret = $this->getSetting(UserModel::SETTING_ENDLESS_SCROLLING);
if ($ret === null)
$ret = getConfig()->browsing->endlessScrollingDefault;
return $ret;
}
public function enableEndlessScrolling($enabled)
{
$this->setSetting(UserModel::SETTING_ENDLESS_SCROLLING, $enabled ? 1 : 0);
} }
public function confirmEmail() public function confirmEmail()

View file

@ -29,7 +29,7 @@ class PostSearchParser extends AbstractSearchParser
protected function processTeardown() protected function processTeardown()
{ {
if (Auth::getCurrentUser()->hasEnabledHidingDislikedPosts() and !$this->showDisliked) if (Auth::getCurrentUser()->getSettings()->hasEnabledHidingDislikedPosts() and !$this->showDisliked)
$this->processComplexToken('special', 'disliked', true); $this->processComplexToken('special', 'disliked', true);
if (!Access::check(new Privilege(Privilege::ListPosts, 'hidden')) or !$this->showHidden) if (!Access::check(new Privilege(Privilege::ListPosts, 'hidden')) or !$this->showHidden)

View file

@ -4,11 +4,6 @@ use \Chibi\Database as Database;
final class UserModel extends AbstractCrudModel final class UserModel extends AbstractCrudModel
{ {
const SETTING_SAFETY = 1;
const SETTING_ENDLESS_SCROLLING = 2;
const SETTING_POST_TAG_TITLES = 3;
const SETTING_HIDE_DISLIKED_POSTS = 4;
public static function getTableName() public static function getTableName()
{ {
return 'user'; return 'user';
@ -32,7 +27,7 @@ final class UserModel extends AbstractCrudModel
'join_date' => $user->getJoinTime(), 'join_date' => $user->getJoinTime(),
'last_login_date' => $user->getLastLoginTime(), 'last_login_date' => $user->getLastLoginTime(),
'access_rank' => $user->getAccessRank()->toInteger(), 'access_rank' => $user->getAccessRank()->toInteger(),
'settings' => $user->settings, 'settings' => $user->getSettings()->getAllAsSerializedString(),
'banned' => $user->isBanned(), 'banned' => $user->isBanned(),
]; ];

143
src/Models/UserSettings.php Normal file
View file

@ -0,0 +1,143 @@
<?php
class UserSettings implements IValidatable
{
const SETTING_SAFETY = 1;
const SETTING_ENDLESS_SCROLLING = 2;
const SETTING_POST_TAG_TITLES = 3;
const SETTING_HIDE_DISLIKED_POSTS = 4;
private $data;
public function __construct($serializedString = null)
{
$this->data = [];
if ($serializedString !== null)
$this->fillFromSerializedString($serializedString);
$this->attachDefaultSettings();
}
public function validate()
{
$serialized = $this->getAllAsSerializedString();
if (strlen($serialized) > 200)
throw new SimpleException('Too much data');
$this->ensureCorrectTypes();
}
public function get($key)
{
return isset($this->data[$key])
? $this->data[$key]
: null;
}
public function set($key, $value)
{
$this->data[$key] = $value;
}
public function getAllAsSerializedString()
{
return json_encode($this->data);
}
public function getAllAsArray()
{
return $this->data;
}
public function hasEnabledSafety(PostSafety $safety)
{
$all = $this->get(self::SETTING_SAFETY);
return ($all & $safety->toFlag()) == $safety->toFlag();
}
public function enableSafety(PostSafety $safety, $enabled)
{
$new = $this->get(self::SETTING_SAFETY);
if (!$enabled)
$new &= ~$safety->toFlag();
else
$new |= $safety->toFlag();
$this->set(self::SETTING_SAFETY, $new);
$this->attachDefaultSettings();
}
public function hasEnabledHidingDislikedPosts()
{
return $this->get(self::SETTING_HIDE_DISLIKED_POSTS);
}
public function enableHidingDislikedPosts($enabled)
{
$this->set(self::SETTING_HIDE_DISLIKED_POSTS, $enabled);
$this->attachDefaultSettings();
}
public function hasEnabledPostTagTitles()
{
return $this->get(self::SETTING_POST_TAG_TITLES);
}
public function enablePostTagTitles($enabled)
{
$this->set(self::SETTING_POST_TAG_TITLES, $enabled);
$this->attachDefaultSettings();
}
public function hasEnabledEndlessScrolling()
{
return $this->get(self::SETTING_ENDLESS_SCROLLING);
}
public function enableEndlessScrolling($enabled)
{
$this->set(self::SETTING_ENDLESS_SCROLLING, $enabled);
$this->attachDefaultSettings();
}
private function fillFromSerializedString($string)
{
$this->data = json_decode($string, true);
}
private function attachDefaultSettings()
{
if ($this->get(self::SETTING_SAFETY) === null or $this->get(self::SETTING_SAFETY) === 0)
$this->set(self::SETTING_SAFETY, (new PostSafety(PostSafety::Safe))->toInteger());
if ($this->get(self::SETTING_HIDE_DISLIKED_POSTS) === null)
$this->set(self::SETTING_HIDE_DISLIKED_POSTS, !(bool) getConfig()->browsing->showDislikedPostsDefault);
if ($this->get(self::SETTING_POST_TAG_TITLES) === null)
$this->set(self::SETTING_POST_TAG_TITLES, (bool) getConfig()->browsing->showPostTagTitlesDefault);
if ($this->get(self::SETTING_ENDLESS_SCROLLING) === null)
$this->set(self::SETTING_ENDLESS_SCROLLING, (bool) getConfig()->browsing->endlessScrollingDefault);
}
private function ensureCorrectTypes()
{
$makeInt = ['TextHelper', 'toIntegerOrNull'];
$makeBool = ['TextHelper', 'toBooleanOrNull'];
$types =
[
[self::SETTING_SAFETY, $makeInt],
[self::SETTING_HIDE_DISLIKED_POSTS, $makeBool],
[self::SETTING_POST_TAG_TITLES, $makeBool],
[self::SETTING_ENDLESS_SCROLLING, $makeBool],
];
foreach ($types as $item)
{
list ($setting, $func) = $item;
$this->set($setting, $func($this->get($setting)));
}
$this->attachDefaultSettings();
}
}

View file

@ -42,7 +42,7 @@ if (!function_exists('pageUrl'))
<?php if (!empty($pagesVisible)): ?> <?php if (!empty($pagesVisible)): ?>
<?php <?php
Assets::addStylesheet('paginator.css'); Assets::addStylesheet('paginator.css');
if (Auth::getCurrentUser()->hasEnabledEndlessScrolling()) if (Auth::getCurrentUser()->getSettings()->hasEnabledEndlessScrolling())
Assets::addScript('paginator-endless.js'); Assets::addScript('paginator-endless.js');
?> ?>

View file

@ -41,7 +41,7 @@ if ($masstag)
<?php endif ?> <?php endif ?>
<a class="link" <a class="link"
<?php if (Auth::getCurrentUser()->hasEnabledPostTagTitles()): ?> <?php if (Auth::getCurrentUser()->getSettings()->hasEnabledPostTagTitles()): ?>
title="<?= TextHelper::reprTags($this->context->post->getTags()) ?>" title="<?= TextHelper::reprTags($this->context->post->getTags()) ?>"
<?php endif ?> <?php endif ?>
href="<?= \Chibi\Router::linkTo(['PostController', 'genericView'], ['id' => $this->context->post->getId()]) ?>"> href="<?= \Chibi\Router::linkTo(['PostController', 'genericView'], ['id' => $this->context->post->getId()]) ?>">

View file

@ -119,13 +119,15 @@
TextCaseConverter::CAMEL_CASE, TextCaseConverter::CAMEL_CASE,
TextCaseConverter::SPINAL_CASE) ?>"> TextCaseConverter::SPINAL_CASE) ?>">
<a class="simple-action <?= Auth::getCurrentUser()->hasEnabledSafety($safety) ? 'enabled' : 'disabled' ?>" <a class="simple-action <?= Auth::getCurrentUser()->getSettings()->hasEnabledSafety($safety)
? 'enabled'
: 'disabled' ?>"
href="<?= \Chibi\Router::linkTo( href="<?= \Chibi\Router::linkTo(
['UserController', 'toggleSafetyAction'], ['UserController', 'toggleSafetyAction'],
['safety' => $safety->toInteger()]) ?>" ['safety' => $safety->toInteger()]) ?>"
title="<?= sprintf('Searching %s posts: %s', title="<?= sprintf('Searching %s posts: %s',
$safety->toDisplayString(), $safety->toDisplayString(),
Auth::getCurrentUser()->hasEnabledSafety($safety) Auth::getCurrentUser()->getSettings()->hasEnabledSafety($safety)
? 'enabled' ? 'enabled'
: 'disabled') ?>"> : 'disabled') ?>">

View file

@ -1,9 +1,6 @@
<?php <?php
Assets::setSubTitle('users'); Assets::setSubTitle('users');
Assets::addStylesheet('user-list.css'); Assets::addStylesheet('user-list.css');
Assets::addStylesheet('paginator.css');
if (Auth::getCurrentUser()->hasEnabledEndlessScrolling())
Assets::addScript('paginator-endless.js');
?> ?>
<nav class="sort-styles"> <nav class="sort-styles">

View file

@ -1,3 +1,7 @@
<?php
$settings = $this->context->transport->user->getSettings();
?>
<form <form
action="<?= \Chibi\Router::linkTo( action="<?= \Chibi\Router::linkTo(
['UserController', 'settingsAction'], ['UserController', 'settingsAction'],
@ -18,7 +22,7 @@
$attrs['type'] = 'checkbox'; $attrs['type'] = 'checkbox';
$attrs['name'] = 'safety[]'; $attrs['name'] = 'safety[]';
$attrs['value'] = $safety->toInteger(); $attrs['value'] = $safety->toInteger();
if ($this->context->transport->user->hasEnabledSafety($safety)) if ($settings->hasEnabledSafety($safety))
$attrs['checked'] = 'checked'; $attrs['checked'] = 'checked';
echo TextHelper::htmlTag('input', TextHelper::HTML_LEAF, $attrs); echo TextHelper::htmlTag('input', TextHelper::HTML_LEAF, $attrs);
@ -39,7 +43,7 @@
$attrs['type'] = 'checkbox'; $attrs['type'] = 'checkbox';
$attrs['id'] = 'endless-scrolling'; $attrs['id'] = 'endless-scrolling';
$attrs['name'] = 'endless-scrolling'; $attrs['name'] = 'endless-scrolling';
if ($this->context->transport->user->hasEnabledEndlessScrolling()) if ($settings->hasEnabledEndlessScrolling())
$attrs['checked'] = 'checked'; $attrs['checked'] = 'checked';
echo TextHelper::htmlTag('input', TextHelper::HTML_LEAF, $attrs); echo TextHelper::htmlTag('input', TextHelper::HTML_LEAF, $attrs);
@ -58,7 +62,7 @@
$attrs['type'] = 'checkbox'; $attrs['type'] = 'checkbox';
$attrs['id'] = 'post-tag-titles'; $attrs['id'] = 'post-tag-titles';
$attrs['name'] = 'post-tag-titles'; $attrs['name'] = 'post-tag-titles';
if ($this->context->transport->user->hasEnabledPostTagTitles()) if ($settings->hasEnabledPostTagTitles())
$attrs['checked'] = 'checked'; $attrs['checked'] = 'checked';
echo TextHelper::htmlTag('input', TextHelper::HTML_LEAF, $attrs); echo TextHelper::htmlTag('input', TextHelper::HTML_LEAF, $attrs);
@ -77,7 +81,7 @@
$attrs['type'] = 'checkbox'; $attrs['type'] = 'checkbox';
$attrs['id'] = 'hide-disliked-posts'; $attrs['id'] = 'hide-disliked-posts';
$attrs['name'] = 'hide-disliked-posts'; $attrs['name'] = 'hide-disliked-posts';
if ($this->context->transport->user->hasEnabledHidingDislikedPosts()) if ($settings->hasEnabledHidingDislikedPosts())
$attrs['checked'] = 'checked'; $attrs['checked'] = 'checked';
echo TextHelper::htmlTag('input', TextHelper::HTML_LEAF, $attrs); echo TextHelper::htmlTag('input', TextHelper::HTML_LEAF, $attrs);

View file

@ -41,9 +41,9 @@ class ListPostsJobTest extends AbstractTest
public function testAutomaticSafetyFilterOnlySafeEnabled() public function testAutomaticSafetyFilterOnlySafeEnabled()
{ {
$user = $this->userMocker->mockSingle(); $user = $this->userMocker->mockSingle();
$user->enableSafety(new PostSafety(PostSafety::Safe), true); $user->getSettings()->enableSafety(new PostSafety(PostSafety::Safe), true);
$user->enableSafety(new PostSafety(PostSafety::Sketchy), false); $user->getSettings()->enableSafety(new PostSafety(PostSafety::Sketchy), false);
$user->enableSafety(new PostSafety(PostSafety::Unsafe), false); $user->getSettings()->enableSafety(new PostSafety(PostSafety::Unsafe), false);
UserModel::save($user); UserModel::save($user);
$this->login($user); $this->login($user);
@ -68,9 +68,9 @@ class ListPostsJobTest extends AbstractTest
public function testAutomaticSafetyFilterAllEnabled() public function testAutomaticSafetyFilterAllEnabled()
{ {
$user = $this->userMocker->mockSingle(); $user = $this->userMocker->mockSingle();
$user->enableSafety(new PostSafety(PostSafety::Safe), true); $user->getSettings()->enableSafety(new PostSafety(PostSafety::Safe), true);
$user->enableSafety(new PostSafety(PostSafety::Sketchy), true); $user->getSettings()->enableSafety(new PostSafety(PostSafety::Sketchy), true);
$user->enableSafety(new PostSafety(PostSafety::Unsafe), true); $user->getSettings()->enableSafety(new PostSafety(PostSafety::Unsafe), true);
UserModel::save($user); UserModel::save($user);
$this->login($user); $this->login($user);
@ -97,7 +97,7 @@ class ListPostsJobTest extends AbstractTest
{ {
$this->grantAccess('listPosts'); $this->grantAccess('listPosts');
$user = $this->userMocker->mockSingle(); $user = $this->userMocker->mockSingle();
$user->enableHidingDislikedPosts(true); $user->getSettings()->enableHidingDislikedPosts(true);
$post = $this->postMocker->mockSingle(); $post = $this->postMocker->mockSingle();
$this->login($user); $this->login($user);
@ -115,7 +115,7 @@ class ListPostsJobTest extends AbstractTest
{ {
$this->grantAccess('listPosts'); $this->grantAccess('listPosts');
$user = $this->userMocker->mockSingle(); $user = $this->userMocker->mockSingle();
$user->enableHidingDislikedPosts(false); $user->getSettings()->enableHidingDislikedPosts(false);
$post = $this->postMocker->mockSingle(); $post = $this->postMocker->mockSingle();
$this->login($user); $this->login($user);
@ -133,7 +133,7 @@ class ListPostsJobTest extends AbstractTest
{ {
$this->grantAccess('listPosts'); $this->grantAccess('listPosts');
$user = $this->userMocker->mockSingle(); $user = $this->userMocker->mockSingle();
$user->enableHidingDislikedPosts(true); $user->getSettings()->enableHidingDislikedPosts(true);
$post = $this->postMocker->mockSingle(); $post = $this->postMocker->mockSingle();
$this->login($user); $this->login($user);

View file

@ -16,4 +16,44 @@ class TextHelperTest extends AbstractTest
$this->assert->areEqual($text, TextHelper::decrypt(TextHelper::encrypt($text))); $this->assert->areEqual($text, TextHelper::decrypt(TextHelper::encrypt($text)));
} }
} }
public function testToIntegerOrNulll()
{
$this->assert->areEqual(1, TextHelper::toIntegerOrNull(1));
$this->assert->areEqual(1, TextHelper::toIntegerOrNull('1'));
$this->assert->areEqual(-1, TextHelper::toIntegerOrNull(-1));
$this->assert->areEqual(-2, TextHelper::toIntegerOrNull('-2'));
$this->assert->areEqual(0, TextHelper::toIntegerOrNull(0));
$this->assert->areEqual(0, TextHelper::toIntegerOrNull('0'));
$this->assert->areEqual(null, TextHelper::toIntegerOrNull('rubbish'));
$this->assert->areEqual(null, TextHelper::toIntegerOrNull('1e1'));
$this->assert->areEqual(null, TextHelper::toIntegerOrNull('1.7'));
$this->assert->areEqual(null, TextHelper::toIntegerOrNull(true));
$this->assert->areEqual(null, TextHelper::toIntegerOrNull(false));
$this->assert->areEqual(null, TextHelper::toIntegerOrNull(null));
}
public function testToBooleanOrNull()
{
$this->assert->isTrue(TextHelper::toBooleanOrNull(1));
$this->assert->isTrue(TextHelper::toBooleanOrNull('1'));
$this->assert->isTrue(TextHelper::toBooleanOrNull('yes'));
$this->assert->isTrue(TextHelper::toBooleanOrNull('y'));
$this->assert->isTrue(TextHelper::toBooleanOrNull('on'));
$this->assert->isTrue(TextHelper::toBooleanOrNull('TrUe'));
$this->assert->isTrue(TextHelper::toBooleanOrNull('true'));
$this->assert->isTrue(TextHelper::toBooleanOrNull(true));
$this->assert->isFalse(TextHelper::toBooleanOrNull(0));
$this->assert->isFalse(TextHelper::toBooleanOrNull('0'));
$this->assert->isFalse(TextHelper::toBooleanOrNull('no'));
$this->assert->isFalse(TextHelper::toBooleanOrNull('n'));
$this->assert->isFalse(TextHelper::toBooleanOrNull('off'));
$this->assert->isFalse(TextHelper::toBooleanOrNull('FaLsE'));
$this->assert->isFalse(TextHelper::toBooleanOrNull('false'));
$this->assert->isFalse(TextHelper::toBooleanOrNull(false));
$this->assert->areEqual(null, TextHelper::toBooleanOrNull(2));
$this->assert->areEqual(null, TextHelper::toBooleanOrNull('2'));
$this->assert->areEqual(null, TextHelper::toBooleanOrNull('rubbish'));
$this->assert->areEqual(null, TextHelper::toBooleanOrNull(null));
}
} }