Merge pull request #31 from helpfulrobot/convert-to-psr-2

Converted to PSR-2
This commit is contained in:
Daniel Hensby 2015-11-25 15:31:30 +00:00
commit 965389ded9
6 changed files with 393 additions and 367 deletions

View File

@ -6,10 +6,9 @@
*
* @package spamprotection
*/
if(class_exists('EditableFormField')) {
class EditableSpamProtectionField extends EditableFormField {
if (class_exists('EditableFormField')) {
class EditableSpamProtectionField extends EditableFormField
{
private static $singular_name = 'Spam Protection Field';
private static $plural_name = 'Spam Protection Fields';
@ -25,14 +24,17 @@ 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();
foreach($this->getCandidateFields() as $otherField) {
foreach ($this->getCandidateFields() as $otherField) {
$mapSetting = "Map-{$otherField->Name}";
$spamField = $this->getSetting($mapSetting);
$fieldMapping[$otherField->Name] = $spamField;
@ -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,15 +47,16 @@ 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'])) {
if (isset($options['protector'])) {
$protector = $options['protector'];
} else {
$protector = Config::inst()->get('FormSpamProtectionExtension', 'default_spam_protector');
}
if($protector && class_exists($protector)) {
if ($protector && class_exists($protector)) {
return Injector::inst()->create($protector);
} else {
return null;
@ -67,17 +68,18 @@ 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'])) {
if (isset($options['name'])) {
$name = $options['name'];
} else {
$name = 'Captcha';
}
// captcha field title
if(isset($options['title'])) {
if (isset($options['title'])) {
$title = $options['title'];
} else {
$title = '';
@ -86,21 +88,21 @@ class FormSpamProtectionExtension extends Extension {
// set custom mapping on this form
$protector = self::get_protector($options);
if(isset($options['mapping'])) {
if (isset($options['mapping'])) {
$protector->setFieldMapping($options['mapping']);
}
if($protector) {
if ($protector) {
// add the form field
if($field = $protector->getFormField($name, $title)) {
if ($field = $protector->getFormField($name, $title)) {
$field->setForm($this->owner);
// Add before field specified by insertBefore
$inserted = false;
if(!empty($options['insertBefore'])) {
if (!empty($options['insertBefore'])) {
$inserted = $this->owner->Fields()->insertBefore($field, $options['insertBefore']);
}
if(!$inserted) {
if (!$inserted) {
// Add field to end if not added already
$this->owner->Fields()->push($field);
}

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)
{
}
}