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

View File

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

View File

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

View File

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

View File

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

View File

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