Update array syntax style, broken doc blocks and namespace imports

This commit is contained in:
Robbie Averill 2018-11-15 21:33:38 +02:00
parent 3ce41f60ec
commit 204ef6df53
2 changed files with 47 additions and 53 deletions

View File

@ -2,12 +2,15 @@
namespace SilverStripe\TagField; namespace SilverStripe\TagField;
use Iterator;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse; use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Convert; use SilverStripe\Core\Convert;
use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\Validator;
use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataObjectInterface; use SilverStripe\ORM\DataObjectInterface;
use SilverStripe\ORM\SS_List; use SilverStripe\ORM\SS_List;
@ -29,7 +32,7 @@ class StringTagField extends DropdownField
* @var array * @var array
*/ */
private static $allowed_actions = [ private static $allowed_actions = [
'suggest' 'suggest',
]; ];
/** /**
@ -67,8 +70,7 @@ class StringTagField extends DropdownField
/** /**
* @param bool $shouldLazyLoad * @param bool $shouldLazyLoad
* * @return $this
* @return static
*/ */
public function setShouldLazyLoad($shouldLazyLoad) public function setShouldLazyLoad($shouldLazyLoad)
{ {
@ -87,8 +89,7 @@ class StringTagField extends DropdownField
/** /**
* @param int $lazyLoadItemLimit * @param int $lazyLoadItemLimit
* * @return $this
* @return static
*/ */
public function setLazyLoadItemLimit($lazyLoadItemLimit) public function setLazyLoadItemLimit($lazyLoadItemLimit)
{ {
@ -107,8 +108,7 @@ class StringTagField extends DropdownField
/** /**
* @param bool $isMultiple * @param bool $isMultiple
* * @return $this
* @return static
*/ */
public function setIsMultiple($isMultiple) public function setIsMultiple($isMultiple)
{ {
@ -135,7 +135,6 @@ class StringTagField extends DropdownField
/** /**
* @param DataObject $record * @param DataObject $record
*
* @return $this * @return $this
*/ */
public function setRecord(DataObject $record) public function setRecord(DataObject $record)
@ -148,7 +147,7 @@ class StringTagField extends DropdownField
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function Field($properties = array()) public function Field($properties = [])
{ {
Requirements::css('silverstripe/tagfield:css/select2.min.css'); Requirements::css('silverstripe/tagfield:css/select2.min.css');
Requirements::css('silverstripe/tagfield:css/TagField.css'); Requirements::css('silverstripe/tagfield:css/TagField.css');
@ -165,9 +164,9 @@ class StringTagField extends DropdownField
if ($this->getShouldLazyLoad()) { if ($this->getShouldLazyLoad()) {
$this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL()); $this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL());
} else { } else {
$properties = array_merge($properties, array( $properties = array_merge($properties, [
'Options' => $this->getOptions() 'Options' => $this->getOptions()
)); ]);
} }
$this->setAttribute('data-can-create', (int) $this->getCanCreate()); $this->setAttribute('data-can-create', (int) $this->getCanCreate());
@ -202,11 +201,11 @@ class StringTagField extends DropdownField
foreach ($source as $value) { foreach ($source as $value) {
$options->push( $options->push(
ArrayData::create(array( ArrayData::create([
'Title' => $value, 'Title' => $value,
'Value' => $value, 'Value' => $value,
'Selected' => in_array($value, $values), 'Selected' => in_array($value, $values),
)) ])
); );
} }
@ -231,8 +230,8 @@ class StringTagField extends DropdownField
$value = $source->column('ID'); $value = $source->column('ID');
} }
if (is_null($value)) { if ($value === null) {
$value = array(); $value = [];
} }
return parent::setValue(array_filter($value)); return parent::setValue(array_filter($value));
@ -245,7 +244,7 @@ class StringTagField extends DropdownField
{ {
return array_merge( return array_merge(
parent::getAttributes(), parent::getAttributes(),
array('name' => $this->getName() . '[]') ['name' => $this->getName() . '[]']
); );
} }
@ -258,7 +257,7 @@ class StringTagField extends DropdownField
$name = $this->getName(); $name = $this->getName();
$record->$name = join(',', $this->Value()); $record->$name = implode(',', $this->Value());
$record->write(); $record->write();
} }
@ -271,14 +270,14 @@ class StringTagField extends DropdownField
public function suggest(HTTPRequest $request) public function suggest(HTTPRequest $request)
{ {
$responseBody = json_encode( $responseBody = json_encode(
array('items' => array()) ['items' => []]
); );
$response = new HTTPResponse; $response = HTTPResponse::create();
$response->addHeader('Content-Type', 'application/json'); $response->addHeader('Content-Type', 'application/json');
if ($record = $this->getRecord()) { if ($record = $this->getRecord()) {
$tags = array(); $tags = [];
$term = $request->getVar('term'); $term = $request->getVar('term');
if ($record->hasField($this->getName())) { if ($record->hasField($this->getName())) {
@ -286,7 +285,7 @@ class StringTagField extends DropdownField
} }
$responseBody = json_encode( $responseBody = json_encode(
array('items' => $tags) ['items' => $tags]
); );
} }
@ -299,7 +298,6 @@ class StringTagField extends DropdownField
* Returns array of arrays representing tags. * Returns array of arrays representing tags.
* *
* @param string $term * @param string $term
*
* @return array * @return array
*/ */
protected function getTags($term) protected function getTags($term)
@ -307,7 +305,7 @@ class StringTagField extends DropdownField
$record = $this->getRecord(); $record = $this->getRecord();
if (!$record) { if (!$record) {
return array(); return [];
} }
$fieldName = $this->getName(); $fieldName = $this->getName();
@ -315,7 +313,7 @@ class StringTagField extends DropdownField
$term = Convert::raw2sql($term); $term = Convert::raw2sql($term);
$query = $className::get() $query = DataList::create($className)
->filter($fieldName . ':PartialMatch:nocase', $term) ->filter($fieldName . ':PartialMatch:nocase', $term)
->limit($this->getLazyLoadItemLimit()); ->limit($this->getLazyLoadItemLimit());
@ -326,10 +324,10 @@ class StringTagField extends DropdownField
foreach ($tags as $i => $tag) { foreach ($tags as $i => $tag) {
if (stripos($tag, $term) !== false && !in_array($tag, $items)) { if (stripos($tag, $term) !== false && !in_array($tag, $items)) {
$items[] = array( $items[] = [
'id' => $tag, 'id' => $tag,
'text' => $tag 'text' => $tag,
); ];
} }
} }
} }
@ -359,8 +357,7 @@ class StringTagField extends DropdownField
/** /**
* @param bool $canCreate * @param bool $canCreate
* * @return $this
* @return static
*/ */
public function setCanCreate($canCreate) public function setCanCreate($canCreate)
{ {

View File

@ -5,9 +5,9 @@ namespace SilverStripe\TagField;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse; use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\Validator;
use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
@ -27,9 +27,9 @@ class TagField extends DropdownField
/** /**
* @var array * @var array
*/ */
private static $allowed_actions = array( private static $allowed_actions = [
'suggest' 'suggest',
); ];
/** /**
* @var bool * @var bool
@ -198,7 +198,7 @@ class TagField extends DropdownField
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function Field($properties = array()) public function Field($properties = [])
{ {
Requirements::css('silverstripe/tagfield:client/dist/styles/bundle.css'); Requirements::css('silverstripe/tagfield:client/dist/styles/bundle.css');
Requirements::javascript('silverstripe/tagfield:client/dist/js/bundle.js'); Requirements::javascript('silverstripe/tagfield:client/dist/js/bundle.js');
@ -219,6 +219,7 @@ class TagField extends DropdownField
} }
$schema['optionUrl'] = $this->getSuggestURL(); $schema['optionUrl'] = $this->getSuggestURL();
} }
$this->setAttribute('data-schema', json_encode($schema)); $this->setAttribute('data-schema', json_encode($schema));
$this->addExtraClass('ss-tag-field'); $this->addExtraClass('ss-tag-field');
@ -275,10 +276,10 @@ class TagField extends DropdownField
foreach ($source as $object) { foreach ($source as $object) {
$options->push( $options->push(
ArrayData::create(array( ArrayData::create([
'Title' => $object->$titleField, 'Title' => $object->$titleField,
'Value' => $object->Title, 'Value' => $object->Title,
)) ])
); );
} }
@ -316,13 +317,12 @@ class TagField extends DropdownField
$name = $this->getName(); $name = $this->getName();
$titleField = $this->getTitleField(); $titleField = $this->getTitleField();
$source = $this->getSource();
$values = $this->Value(); $values = $this->Value();
$relation = $record->$name(); $relation = $record->$name();
$ids = array(); $ids = [];
if (!$values) { if (!$values) {
$values = array(); $values = [];
} }
if (empty($record) || empty($titleField)) { if (empty($record) || empty($titleField)) {
return; return;
@ -350,12 +350,11 @@ class TagField extends DropdownField
* Get or create tag with the given value * Get or create tag with the given value
* *
* @param string $term * @param string $term
* @return DataObject * @return DataObject|bool
*/ */
protected function getOrCreateTag($term) protected function getOrCreateTag($term)
{ {
// Check if existing record can be found // Check if existing record can be found
/** @var DataList $source */
$source = $this->getSourceList(); $source = $this->getSourceList();
$titleField = $this->getTitleField(); $titleField = $this->getTitleField();
$record = $source $record = $source
@ -372,9 +371,9 @@ class TagField extends DropdownField
$record->{$titleField} = $term; $record->{$titleField} = $term;
$record->write(); $record->write();
return $record; return $record;
} else {
return false;
} }
return false;
} }
/** /**
@ -387,9 +386,9 @@ class TagField extends DropdownField
{ {
$tags = $this->getTags($request->getVar('term')); $tags = $this->getTags($request->getVar('term'));
$response = new HTTPResponse(); $response = HTTPResponse::create();
$response->addHeader('Content-Type', 'application/json'); $response->addHeader('Content-Type', 'application/json');
$response->setBody(json_encode(array('items' => $tags))); $response->setBody(json_encode(['items' => $tags]));
return $response; return $response;
} }
@ -402,9 +401,6 @@ class TagField extends DropdownField
*/ */
protected function getTags($term) protected function getTags($term)
{ {
/**
* @var array $source
*/
$source = $this->getSourceList(); $source = $this->getSourceList();
$titleField = $this->getTitleField(); $titleField = $this->getTitleField();
@ -415,13 +411,13 @@ class TagField extends DropdownField
->limit($this->getLazyLoadItemLimit()); ->limit($this->getLazyLoadItemLimit());
// Map into a distinct list // Map into a distinct list
$items = array(); $items = [];
$titleField = $this->getTitleField(); $titleField = $this->getTitleField();
foreach ($query->map('ID', $titleField) as $id => $title) { foreach ($query->map('ID', $titleField) as $id => $title) {
$items[$title] = array( $items[$title] = [
'id' => $title, 'id' => $title,
'text' => $title 'text' => $title,
); ];
} }
return array_values($items); return array_values($items);
@ -442,10 +438,11 @@ class TagField extends DropdownField
/** /**
* Converts the field to a readonly variant. * Converts the field to a readonly variant.
* *
* @return TagField_Readonly * @return ReadonlyTagField
*/ */
public function performReadonlyTransformation() public function performReadonlyTransformation()
{ {
/** @var ReadonlyTagField $copy */
$copy = $this->castedCopy(ReadonlyTagField::class); $copy = $this->castedCopy(ReadonlyTagField::class);
$copy->setSourceList($this->getSourceList()); $copy->setSourceList($this->getSourceList());
return $copy; return $copy;