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

Converted to PSR-2
This commit is contained in:
Daniel Hensby 2015-11-19 17:37:10 +00:00
commit 10b84002b3
4 changed files with 1045 additions and 977 deletions

View File

@ -9,7 +9,8 @@
* @package forms
* @subpackage fields
*/
class StringTagField extends DropdownField {
class StringTagField extends DropdownField
{
/**
* @var array
*/
@ -43,14 +44,16 @@ class StringTagField extends DropdownField {
* @param array|SS_List $source
* @param array|SS_List $value
*/
public function __construct($name, $title = '', $source = array(), $value = array()) {
public function __construct($name, $title = '', $source = array(), $value = array())
{
parent::__construct($name, $title, $source, $value);
}
/**
* @return bool
*/
public function getShouldLazyLoad() {
public function getShouldLazyLoad()
{
return $this->shouldLazyLoad;
}
@ -59,7 +62,8 @@ class StringTagField extends DropdownField {
*
* @return static
*/
public function setShouldLazyLoad($shouldLazyLoad) {
public function setShouldLazyLoad($shouldLazyLoad)
{
$this->shouldLazyLoad = $shouldLazyLoad;
return $this;
@ -68,7 +72,8 @@ class StringTagField extends DropdownField {
/**
* @return int
*/
public function getLazyLoadItemLimit() {
public function getLazyLoadItemLimit()
{
return $this->lazyLoadItemLimit;
}
@ -77,7 +82,8 @@ class StringTagField extends DropdownField {
*
* @return static
*/
public function setLazyLoadItemLimit($lazyLoadItemLimit) {
public function setLazyLoadItemLimit($lazyLoadItemLimit)
{
$this->lazyLoadItemLimit = $lazyLoadItemLimit;
return $this;
@ -86,7 +92,8 @@ class StringTagField extends DropdownField {
/**
* @return bool
*/
public function getIsMultiple() {
public function getIsMultiple()
{
return $this->isMultiple;
}
@ -95,7 +102,8 @@ class StringTagField extends DropdownField {
*
* @return static
*/
public function setIsMultiple($isMultiple) {
public function setIsMultiple($isMultiple)
{
$this->isMultiple = $isMultiple;
return $this;
@ -104,12 +112,13 @@ class StringTagField extends DropdownField {
/**
* @return null|DataObject
*/
public function getRecord() {
if($this->record) {
public function getRecord()
{
if ($this->record) {
return $this->record;
}
if($form = $this->getForm()) {
if ($form = $this->getForm()) {
return $form->getRecord();
}
@ -121,7 +130,8 @@ class StringTagField extends DropdownField {
*
* @return $this
*/
public function setRecord(DataObject $record) {
public function setRecord(DataObject $record)
{
$this->record = $record;
return $this;
@ -130,7 +140,8 @@ class StringTagField extends DropdownField {
/**
* {@inheritdoc}
*/
public function Field($properties = array()) {
public function Field($properties = array())
{
Requirements::css(TAG_FIELD_DIR . '/css/select2.min.css');
Requirements::css(TAG_FIELD_DIR . '/css/TagField.css');
@ -145,7 +156,7 @@ class StringTagField extends DropdownField {
$this->setAttribute('multiple', 'multiple');
}
if($this->getShouldLazyLoad()) {
if ($this->getShouldLazyLoad()) {
$this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL());
} else {
$properties = array_merge($properties, array(
@ -161,25 +172,27 @@ class StringTagField extends DropdownField {
/**
* @return string
*/
protected function getSuggestURL() {
protected function getSuggestURL()
{
return Controller::join_links($this->Link(), 'suggest');
}
/**
* @return ArrayList
*/
protected function getOptions() {
protected function getOptions()
{
$options = ArrayList::create();
$source = $this->getSource();
if($source instanceof Iterator) {
if ($source instanceof Iterator) {
$source = iterator_to_array($source);
}
$values = $this->Value();
foreach($source as $value) {
foreach ($source as $value) {
$options->push(
ArrayData::create(array(
'Title' => $value,
@ -195,17 +208,18 @@ class StringTagField extends DropdownField {
/**
* {@inheritdoc}
*/
public function setValue($value, $source = null) {
if(is_string($value)) {
public function setValue($value, $source = null)
{
if (is_string($value)) {
$value = explode(',', $value);
}
if($source instanceof DataObject) {
if ($source instanceof DataObject) {
$name = $this->getName();
$value = explode(',', $source->$name);
}
if($source instanceof SS_List) {
if ($source instanceof SS_List) {
$value = $source->column('ID');
}
@ -215,7 +229,8 @@ class StringTagField extends DropdownField {
/**
* {@inheritdoc}
*/
public function getAttributes() {
public function getAttributes()
{
return array_merge(
parent::getAttributes(),
array('name' => $this->getName() . '[]')
@ -225,7 +240,8 @@ class StringTagField extends DropdownField {
/**
* {@inheritdoc}
*/
public function saveInto(DataObjectInterface $record) {
public function saveInto(DataObjectInterface $record)
{
parent::saveInto($record);
$name = $this->getName();
@ -241,7 +257,8 @@ class StringTagField extends DropdownField {
*
* @return SS_HTTPResponse
*/
public function suggest(SS_HTTPRequest $request) {
public function suggest(SS_HTTPRequest $request)
{
$responseBody = Convert::raw2json(
array('items' => array())
);
@ -249,11 +266,11 @@ class StringTagField extends DropdownField {
$response = new SS_HTTPResponse();
$response->addHeader('Content-Type', 'application/json');
if($record = $this->getRecord()) {
if ($record = $this->getRecord()) {
$tags = array();
$term = $request->getVar('term');
if($record->hasField($this->getName())) {
if ($record->hasField($this->getName())) {
$tags = $this->getTags($term);
}
@ -274,10 +291,11 @@ class StringTagField extends DropdownField {
*
* @return array
*/
protected function getTags($term) {
protected function getTags($term)
{
$record = $this->getRecord();
if(!$record) {
if (!$record) {
return array();
}
@ -292,11 +310,11 @@ class StringTagField extends DropdownField {
$items = array();
foreach($query->column($fieldName) as $tags) {
foreach ($query->column($fieldName) as $tags) {
$tags = explode(',', $tags);
foreach($tags as $i => $tag) {
if(stripos($tag, $term) !== false && !in_array($tag, $items)) {
foreach ($tags as $i => $tag) {
if (stripos($tag, $term) !== false && !in_array($tag, $items)) {
$items[] = array(
'id' => $tag,
'text' => $tag
@ -315,8 +333,8 @@ class StringTagField extends DropdownField {
* @param Validator $validator
* @return bool
*/
public function validate($validator) {
public function validate($validator)
{
return true;
}
}

View File

@ -6,7 +6,8 @@
* @package forms
* @subpackage fields
*/
class TagField extends DropdownField {
class TagField extends DropdownField
{
/**
* @var array
*/
@ -45,14 +46,16 @@ class TagField extends DropdownField {
* @param null|DataList $source
* @param null|DataList $value
*/
public function __construct($name, $title = '', $source = null, $value = null) {
public function __construct($name, $title = '', $source = null, $value = null)
{
parent::__construct($name, $title, $source, $value);
}
/**
* @return bool
*/
public function getShouldLazyLoad() {
public function getShouldLazyLoad()
{
return $this->shouldLazyLoad;
}
@ -61,7 +64,8 @@ class TagField extends DropdownField {
*
* @return static
*/
public function setShouldLazyLoad($shouldLazyLoad) {
public function setShouldLazyLoad($shouldLazyLoad)
{
$this->shouldLazyLoad = $shouldLazyLoad;
return $this;
@ -70,7 +74,8 @@ class TagField extends DropdownField {
/**
* @return int
*/
public function getLazyLoadItemLimit() {
public function getLazyLoadItemLimit()
{
return $this->lazyLoadItemLimit;
}
@ -79,7 +84,8 @@ class TagField extends DropdownField {
*
* @return static
*/
public function setLazyLoadItemLimit($lazyLoadItemLimit) {
public function setLazyLoadItemLimit($lazyLoadItemLimit)
{
$this->lazyLoadItemLimit = $lazyLoadItemLimit;
return $this;
@ -88,7 +94,8 @@ class TagField extends DropdownField {
/**
* @return bool
*/
public function getIsMultiple() {
public function getIsMultiple()
{
return $this->isMultiple;
}
@ -97,7 +104,8 @@ class TagField extends DropdownField {
*
* @return static
*/
public function setIsMultiple($isMultiple) {
public function setIsMultiple($isMultiple)
{
$this->isMultiple = $isMultiple;
return $this;
@ -106,7 +114,8 @@ class TagField extends DropdownField {
/**
* @return bool
*/
public function getCanCreate() {
public function getCanCreate()
{
return $this->canCreate;
}
@ -115,7 +124,8 @@ class TagField extends DropdownField {
*
* @return static
*/
public function setCanCreate($canCreate) {
public function setCanCreate($canCreate)
{
$this->canCreate = $canCreate;
return $this;
@ -124,7 +134,8 @@ class TagField extends DropdownField {
/**
* @return string
*/
public function getTitleField() {
public function getTitleField()
{
return $this->titleField;
}
@ -133,7 +144,8 @@ class TagField extends DropdownField {
*
* @return $this
*/
public function setTitleField($titleField) {
public function setTitleField($titleField)
{
$this->titleField = $titleField;
return $this;
@ -142,7 +154,8 @@ class TagField extends DropdownField {
/**
* {@inheritdoc}
*/
public function Field($properties = array()) {
public function Field($properties = array())
{
Requirements::css(TAG_FIELD_DIR . '/css/select2.min.css');
Requirements::css(TAG_FIELD_DIR . '/css/TagField.css');
@ -157,7 +170,7 @@ class TagField extends DropdownField {
$this->setAttribute('multiple', 'multiple');
}
if($this->shouldLazyLoad) {
if ($this->shouldLazyLoad) {
$this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL());
} else {
$properties = array_merge($properties, array(
@ -173,19 +186,21 @@ class TagField extends DropdownField {
/**
* @return string
*/
protected function getSuggestURL() {
protected function getSuggestURL()
{
return Controller::join_links($this->Link(), 'suggest');
}
/**
* @return ArrayList
*/
protected function getOptions() {
protected function getOptions()
{
$options = ArrayList::create();
$source = $this->getSource();
if(!$source) {
if (!$source) {
$source = new ArrayList();
}
@ -193,11 +208,11 @@ class TagField extends DropdownField {
$values = $this->Value();
if(!$values) {
if (!$values) {
return $options;
}
if(is_array($values)) {
if (is_array($values)) {
$values = DataList::create($dataClass)->filter('ID', $values);
}
@ -205,7 +220,7 @@ class TagField extends DropdownField {
$titleField = $this->getTitleField();
foreach($source as $object) {
foreach ($source as $object) {
$options->push(
ArrayData::create(array(
'Title' => $object->$titleField,
@ -221,18 +236,19 @@ class TagField extends DropdownField {
/**
* {@inheritdoc}
*/
public function setValue($value, $source = null) {
if($source instanceof DataObject) {
public function setValue($value, $source = null)
{
if ($source instanceof DataObject) {
$name = $this->getName();
if($source->hasMethod($name)) {
if ($source->hasMethod($name)) {
$value = $source->$name()->getIDList();
}
} elseif($value instanceof SS_List) {
} elseif ($value instanceof SS_List) {
$value = $value->column('ID');
}
if(!is_array($value)) {
if (!is_array($value)) {
return parent::setValue($value);
}
@ -242,7 +258,8 @@ class TagField extends DropdownField {
/**
* {@inheritdoc}
*/
public function getAttributes() {
public function getAttributes()
{
return array_merge(
parent::getAttributes(),
array('name' => $this->getName() . '[]')
@ -252,7 +269,8 @@ class TagField extends DropdownField {
/**
* {@inheritdoc}
*/
public function saveInto(DataObjectInterface $record) {
public function saveInto(DataObjectInterface $record)
{
parent::saveInto($record);
$name = $this->getName();
@ -262,15 +280,15 @@ class TagField extends DropdownField {
$values = $this->Value();
if(!$values) {
if (!$values) {
$values = array();
}
if(empty($record) || empty($source) || empty($titleField)) {
if (empty($record) || empty($source) || empty($titleField)) {
return;
}
if(!$record->hasMethod($name)) {
if (!$record->hasMethod($name)) {
throw new Exception(
sprintf("%s does not have a %s method", get_class($record), $name)
);
@ -278,9 +296,9 @@ class TagField extends DropdownField {
$relation = $record->$name();
foreach($values as $i => $value) {
if(!is_numeric($value)) {
if(!$this->getCanCreate()) {
foreach ($values as $i => $value) {
if (!is_numeric($value)) {
if (!$this->getCanCreate()) {
unset($values[$i]);
continue;
}
@ -291,7 +309,7 @@ class TagField extends DropdownField {
}
}
if($values instanceof SS_List) {
if ($values instanceof SS_List) {
$values = iterator_to_array($values);
}
@ -304,14 +322,15 @@ class TagField extends DropdownField {
* @param string $term
* @return DataObject
*/
protected function getOrCreateTag($term) {
protected function getOrCreateTag($term)
{
// Check if existing record can be found
$source = $this->getSource();
$titleField = $this->getTitleField();
$record = $source
->filter($titleField, $term)
->first();
if($record) {
if ($record) {
return $record;
}
@ -330,7 +349,8 @@ class TagField extends DropdownField {
*
* @return SS_HTTPResponse
*/
public function suggest(SS_HTTPRequest $request) {
public function suggest(SS_HTTPRequest $request)
{
$tags = $this->getTags($request->getVar('term'));
$response = new SS_HTTPResponse();
@ -347,7 +367,8 @@ class TagField extends DropdownField {
*
* @return array
*/
protected function getTags($term) {
protected function getTags($term)
{
/**
* @var DataList $source
*/
@ -363,7 +384,7 @@ class TagField extends DropdownField {
// Map into a distinct list
$items = array();
$titleField = $this->getTitleField();
foreach($query->map('ID', $titleField) as $id => $title) {
foreach ($query->map('ID', $titleField) as $id => $title) {
$items[$title] = array(
'id' => $id,
'text' => $title
@ -380,8 +401,8 @@ class TagField extends DropdownField {
* @param Validator $validator
* @return bool
*/
public function validate($validator) {
public function validate($validator)
{
return true;
}
}

View File

@ -3,7 +3,8 @@
/**
* @mixin PHPUnit_Framework_TestCase
*/
class StringTagFieldTest extends SapphireTest {
class StringTagFieldTest extends SapphireTest
{
/**
* @var string
*/
@ -16,7 +17,8 @@ class StringTagFieldTest extends SapphireTest {
'StringTagFieldTestBlogPost',
);
function testItSavesTagsOnNewRecords() {
public function testItSavesTagsOnNewRecords()
{
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
$field = new StringTagField('Tags');
@ -33,14 +35,16 @@ class StringTagFieldTest extends SapphireTest {
*
* @return StringTagFieldTestBlogPost
*/
protected function getNewStringTagFieldTestBlogPost($name) {
protected function getNewStringTagFieldTestBlogPost($name)
{
return $this->objFromFixture(
'StringTagFieldTestBlogPost',
$name
);
}
function testItSavesTagsOnExistingRecords() {
public function testItSavesTagsOnExistingRecords()
{
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
$record->write();
@ -51,7 +55,8 @@ class StringTagFieldTest extends SapphireTest {
$this->assertEquals('Tag1,Tag2', $record->Tags);
}
function testItSuggestsTags() {
public function testItSuggestsTags()
{
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost2');
$field = new StringTagField('Tags');
@ -100,7 +105,8 @@ class StringTagFieldTest extends SapphireTest {
*
* @return SS_HTTPRequest
*/
protected function getNewRequest(array $parameters) {
protected function getNewRequest(array $parameters)
{
return new SS_HTTPRequest(
'get',
'StringTagFieldTestController/StringTagFieldTestForm/fields/Tags/suggest',
@ -112,7 +118,8 @@ class StringTagFieldTest extends SapphireTest {
/**
* @property string $Tags
*/
class StringTagFieldTestBlogPost extends DataObject implements TestOnly {
class StringTagFieldTestBlogPost extends DataObject implements TestOnly
{
/**
* @var array
*/
@ -123,11 +130,13 @@ class StringTagFieldTestBlogPost extends DataObject implements TestOnly {
);
}
class StringTagFieldTestController extends Controller implements TestOnly {
class StringTagFieldTestController extends Controller implements TestOnly
{
/**
* @return Form
*/
public function StringTagFieldTestForm() {
public function StringTagFieldTestForm()
{
$fields = new FieldList(
$tagField = new StringTagField('Tags')
);
@ -143,7 +152,8 @@ class StringTagFieldTestController extends Controller implements TestOnly {
* @param DataObject $dataObject
* @param Form $form
*/
public function StringTagFieldTestFormSubmit(DataObject $dataObject, Form $form) {
public function StringTagFieldTestFormSubmit(DataObject $dataObject, Form $form)
{
$form->saveInto($dataObject);
}
}

View File

@ -3,7 +3,8 @@
/**
* @mixin PHPUnit_Framework_TestCase
*/
class TagFieldTest extends SapphireTest {
class TagFieldTest extends SapphireTest
{
/**
* @var string
*/
@ -17,7 +18,8 @@ class TagFieldTest extends SapphireTest {
'TagFieldTestBlogPost',
);
function testItSavesLinksToNewTagsOnNewRecords() {
public function testItSavesLinksToNewTagsOnNewRecords()
{
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
@ -37,7 +39,8 @@ class TagFieldTest extends SapphireTest {
*
* @return TagFieldTestBlogPost
*/
protected function getNewTagFieldTestBlogPost($name) {
protected function getNewTagFieldTestBlogPost($name)
{
return $this->objFromFixture(
'TagFieldTestBlogPost',
$name
@ -48,7 +51,8 @@ class TagFieldTest extends SapphireTest {
* @param array $expected
* @param TagFieldTestBlogPost $record
*/
protected function compareExpectedAndActualTags(array $expected, TagFieldTestBlogPost $record) {
protected function compareExpectedAndActualTags(array $expected, TagFieldTestBlogPost $record)
{
$this->compareTagLists($expected, $record->Tags());
}
@ -58,7 +62,8 @@ class TagFieldTest extends SapphireTest {
* @param array $expected
* @param DataList $actualSource
*/
protected function compareTagLists(array $expected, DataList $actualSource) {
protected function compareTagLists(array $expected, DataList $actualSource)
{
$actual = array_values($actualSource->map('ID', 'Title')->toArray());
sort($expected);
@ -70,7 +75,8 @@ class TagFieldTest extends SapphireTest {
);
}
public function testItSavesLinksToNewTagsOnExistingRecords() {
public function testItSavesLinksToNewTagsOnExistingRecords()
{
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
$record->write();
@ -84,7 +90,8 @@ class TagFieldTest extends SapphireTest {
);
}
public function testItSavesLinksToExistingTagsOnNewRecords() {
public function testItSavesLinksToExistingTagsOnNewRecords()
{
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
@ -99,7 +106,8 @@ class TagFieldTest extends SapphireTest {
);
}
public function testItSavesLinksToExistingTagsOnExistingRecords() {
public function testItSavesLinksToExistingTagsOnExistingRecords()
{
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
$record->write();
@ -116,7 +124,8 @@ class TagFieldTest extends SapphireTest {
/**
* Ensure that {@see TagField::saveInto} respects existing tags
*/
public function testSaveDuplicateTags() {
public function testSaveDuplicateTags()
{
$record = $this->getNewTagFieldTestBlogPost('BlogPost2');
$record->write();
$tag2ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag2');
@ -151,7 +160,8 @@ class TagFieldTest extends SapphireTest {
$this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID'));
}
function testItSuggestsTags() {
public function testItSuggestsTags()
{
$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
/**
@ -198,7 +208,8 @@ class TagFieldTest extends SapphireTest {
/**
* Tests that TagField supports pre-filtered data sources
*/
public function testRestrictedSuggestions() {
public function testRestrictedSuggestions()
{
$source = TagFieldTestBlogTag::get()->exclude('Title', 'Tag2');
$field = new TagField('Tags', '', $source);
@ -238,7 +249,8 @@ class TagFieldTest extends SapphireTest {
*
* @return SS_HTTPRequest
*/
protected function getNewRequest(array $parameters) {
protected function getNewRequest(array $parameters)
{
return new SS_HTTPRequest(
'get',
'TagFieldTestController/TagFieldTestForm/fields/Tags/suggest',
@ -246,7 +258,8 @@ class TagFieldTest extends SapphireTest {
);
}
function testItDisplaysValuesFromRelations() {
public function testItDisplaysValuesFromRelations()
{
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
$record->write();
@ -266,7 +279,8 @@ class TagFieldTest extends SapphireTest {
$this->assertEquals($field->Value(), array(1 => 1, 2 => 2));
}
function testItIgnoresNewTagsIfCannotCreate() {
public function testItIgnoresNewTagsIfCannotCreate()
{
$record = new TagFieldTestBlogPost();
$record->write();
@ -288,7 +302,8 @@ class TagFieldTest extends SapphireTest {
}
}
class TagFieldTestBlogTag extends DataObject implements TestOnly {
class TagFieldTestBlogTag extends DataObject implements TestOnly
{
/**
* @var string
*/
@ -312,7 +327,8 @@ class TagFieldTestBlogTag extends DataObject implements TestOnly {
/**
* @method ManyManyList Tags()
*/
class TagFieldTestBlogPost extends DataObject implements TestOnly {
class TagFieldTestBlogPost extends DataObject implements TestOnly
{
/**
* @var array
*/
@ -329,11 +345,13 @@ class TagFieldTestBlogPost extends DataObject implements TestOnly {
);
}
class TagFieldTestController extends Controller implements TestOnly {
class TagFieldTestController extends Controller implements TestOnly
{
/**
* @return Form
*/
public function TagFieldTestForm() {
public function TagFieldTestForm()
{
$fields = new FieldList(
$tagField = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'))
);
@ -349,7 +367,8 @@ class TagFieldTestController extends Controller implements TestOnly {
* @param DataObject $dataObject
* @param Form $form
*/
public function TagFieldTestFormSubmit(DataObject $dataObject, Form $form) {
public function TagFieldTestFormSubmit(DataObject $dataObject, Form $form)
{
$form->saveInto($dataObject);
}
}