Converted to PSR-2

This commit is contained in:
helpfulrobot 2015-11-21 19:15:15 +13:00
parent 8e4f03a409
commit 7ee2dcea0f
6 changed files with 393 additions and 367 deletions

View File

@ -7,9 +7,8 @@
* @package spamprotection
*/
if (class_exists('EditableFormField')) {
class EditableSpamProtectionField extends EditableFormField {
class EditableSpamProtectionField extends EditableFormField
{
private static $singular_name = 'Spam Protection Field';
private static $plural_name = 'Spam Protection Fields';
@ -25,10 +24,13 @@ if(class_exists('EditableFormField')) {
'EditableNumericField'
);
public function getFormField() {
public function getFormField()
{
// Get protector
$protector = FormSpamProtectionExtension::get_protector();
if(!$protector) return false;
if (!$protector) {
return false;
}
// Extract saved field mappings and update this field.
$fieldMapping = array();
@ -48,7 +50,8 @@ if(class_exists('EditableFormField')) {
*
* @return DataList
*/
protected function getCandidateFields() {
protected function getCandidateFields()
{
// Get list of all configured classes available for spam detection
$types = self::config()->check_fields;
@ -66,12 +69,15 @@ if(class_exists('EditableFormField')) {
->exclude('Title', ''); // Ignore this field and those without titles
}
public function getFieldConfiguration() {
public function getFieldConfiguration()
{
$fields = parent::getFieldConfiguration();
// Get protector
$protector = FormSpamProtectionExtension::get_protector();
if (!$protector) return $fields;
if (!$protector) {
return $fields;
}
if ($this->Parent()->Fields() instanceof UnsavedRelationList) {
return $fields;
@ -104,26 +110,31 @@ if(class_exists('EditableFormField')) {
return $fields;
}
public function validateField($data, $form) {
public function validateField($data, $form)
{
$formField = $this->getFormField();
if (!$formField->validate($form->getValidator())) {
$form->addErrorMessage($this->Name, $this->getErrorMessage()->HTML(), 'error', false);
}
}
public function getFieldValidationOptions() {
public function getFieldValidationOptions()
{
return new FieldList();
}
public function getRequired() {
public function getRequired()
{
return false;
}
public function getIcon() {
public function getIcon()
{
return 'spamprotection/images/' . strtolower($this->class) . '.png';
}
public function showInReports() {
public function showInReports()
{
return false;
}
}

View File

@ -6,11 +6,12 @@
* @deprecated 1.0
*/
class SpamProtectorManager {
class SpamProtectorManager
{
private static $spam_protector = null;
public static function set_spam_protector($protector) {
public static function set_spam_protector($protector)
{
Deprecation::notice('1.1',
'SpamProtectorManager::set_spam_protector() is deprecated. '.
'Use the new config system. FormSpamProtectorExtension.default_spam_protector'
@ -19,7 +20,8 @@ class SpamProtectorManager {
self::$spam_protector = $protector;
}
public static function get_spam_protector() {
public static function get_spam_protector()
{
Deprecation::notice('1.1',
'SpamProtectorManager::get_spam_protector() is deprecated'.
'Use the new config system. FormSpamProtectorExtension.default_spam_protector');
@ -27,7 +29,8 @@ class SpamProtectorManager {
return self::$spam_protector;
}
public static function update_form($form, $before = null, $fieldsToSpamServiceMapping = array(), $title = null, $rightTitle = null) {
public static function update_form($form, $before = null, $fieldsToSpamServiceMapping = array(), $title = null, $rightTitle = null)
{
Deprecation::notice('1.1',
'SpamProtectorManager::update_form is deprecated'.
'Please use $form->enableSpamProtection() for adding spamprotection'

View File

@ -6,9 +6,10 @@
* @package spamprotection
*/
class CommentSpamProtection extends Extension {
public function alterCommentForm(&$form) {
class CommentSpamProtection extends Extension
{
public function alterCommentForm(&$form)
{
$form->enableSpamProtection(array(
'name' => 'IsSpam',
'mapping' => array(

View File

@ -7,8 +7,8 @@
* @package spamprotection
*/
class FormSpamProtectionExtension extends Extension {
class FormSpamProtectionExtension extends Extension
{
/**
* @config
*
@ -47,7 +47,8 @@ class FormSpamProtectionExtension extends Extension {
* @param array $options Configuration options
* @return SpamProtector
*/
public static function get_protector($options = null) {
public static function get_protector($options = null)
{
// generate the spam protector
if (isset($options['protector'])) {
$protector = $options['protector'];
@ -67,7 +68,8 @@ class FormSpamProtectionExtension extends Extension {
*
* @param array $options
*/
public function enableSpamProtection($options = array()) {
public function enableSpamProtection($options = array())
{
// captcha form field name (must be unique)
if (isset($options['name'])) {

View File

@ -12,8 +12,8 @@
* @package spamprotection
*/
interface SpamProtector {
interface SpamProtector
{
/**
* Return the {@link FormField} associated with this protector.
*

View File

@ -3,8 +3,8 @@
/**
* @package spamprotection
*/
class FormSpamProtectionExtensionTest extends SapphireTest {
class FormSpamProtectionExtensionTest extends SapphireTest
{
protected $usesDatabase = false;
/**
@ -12,7 +12,8 @@ class FormSpamProtectionExtensionTest extends SapphireTest {
*/
protected $form = null;
public function setUp() {
public function setUp()
{
parent::setUp();
$this->form = new Form($this, 'Form', new FieldList(
@ -24,7 +25,8 @@ class FormSpamProtectionExtensionTest extends SapphireTest {
$this->form->disableSecurityToken();
}
public function testEnableSpamProtection() {
public function testEnableSpamProtection()
{
Config::inst()->update(
'FormSpamProtectionExtension', 'default_spam_protector',
'FormSpamProtectionExtensionTest_FooProtector'
@ -33,10 +35,10 @@ class FormSpamProtectionExtensionTest extends SapphireTest {
$form = $this->form->enableSpamProtection();
$this->assertEquals('Foo', $form->Fields()->fieldByName('Captcha')->Title());
}
public function testEnableSpamProtectionCustomProtector() {
public function testEnableSpamProtectionCustomProtector()
{
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_BarProtector'
));
@ -44,7 +46,8 @@ class FormSpamProtectionExtensionTest extends SapphireTest {
$this->assertEquals('Bar', $form->Fields()->fieldByName('Captcha')->Title());
}
public function testEnableSpamProtectionCustomTitle() {
public function testEnableSpamProtectionCustomTitle()
{
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_BarProtector',
'title' => 'Baz',
@ -53,7 +56,8 @@ class FormSpamProtectionExtensionTest extends SapphireTest {
$this->assertEquals('Baz', $form->Fields()->fieldByName('Captcha')->Title());
}
public function testCustomOptions() {
public function testCustomOptions()
{
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_BazProtector',
'title' => 'Qux',
@ -63,8 +67,8 @@ class FormSpamProtectionExtensionTest extends SapphireTest {
$this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title());
}
public function testInsertBefore() {
public function testInsertBefore()
{
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_FooProtector',
'insertBefore' => 'URL'
@ -77,8 +81,8 @@ class FormSpamProtectionExtensionTest extends SapphireTest {
$this->assertEquals('URL', $fields[3]->Title());
}
public function testInsertBeforeMissing() {
public function testInsertBeforeMissing()
{
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_FooProtector',
'insertBefore' => 'NotAField'
@ -91,45 +95,50 @@ class FormSpamProtectionExtensionTest extends SapphireTest {
$this->assertEquals('URL', $fields[2]->Title());
$this->assertEquals('Foo', $fields[3]->Title());
}
}
/**
* @package spamprotection
*/
class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, TestOnly {
public function getFormField($name = null, $title = null, $value = null) {
class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, TestOnly
{
public function getFormField($name = null, $title = null, $value = null)
{
return new TextField($name, $title, $value);
}
public function setFieldMapping($fieldMapping) {}
public function setFieldMapping($fieldMapping)
{
}
}
/**
* @package spamprotection
*/
class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, TestOnly {
public function getFormField($name = null, $title = null, $value = null) {
class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, TestOnly
{
public function getFormField($name = null, $title = null, $value = null)
{
$title = $title ?: 'Bar';
return new TextField($name, $title, $value);
}
public function setFieldMapping($fieldMapping) {}
public function setFieldMapping($fieldMapping)
{
}
}
/**
* @package spamprotection
*/
class FormSpamProtectionExtensionTest_FooProtector implements SpamProtector, TestOnly {
public function getFormField($name = null, $title = null, $value = null) {
class FormSpamProtectionExtensionTest_FooProtector implements SpamProtector, TestOnly
{
public function getFormField($name = null, $title = null, $value = null)
{
return new TextField($name, 'Foo', $value);
}
public function setFieldMapping($fieldMapping) {}
public function setFieldMapping($fieldMapping)
{
}
}