From 204ef6df53385d274bdfd6d9c0e9709d8dbf1ef3 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Thu, 15 Nov 2018 21:33:38 +0200 Subject: [PATCH] Update array syntax style, broken doc blocks and namespace imports --- src/StringTagField.php | 55 ++++++++++++++++++++---------------------- src/TagField.php | 45 ++++++++++++++++------------------ 2 files changed, 47 insertions(+), 53 deletions(-) diff --git a/src/StringTagField.php b/src/StringTagField.php index 948358c..a8b301b 100644 --- a/src/StringTagField.php +++ b/src/StringTagField.php @@ -2,12 +2,15 @@ namespace SilverStripe\TagField; +use Iterator; use SilverStripe\Control\Controller; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; use SilverStripe\Core\Convert; use SilverStripe\Forms\DropdownField; +use SilverStripe\Forms\Validator; use SilverStripe\ORM\ArrayList; +use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObjectInterface; use SilverStripe\ORM\SS_List; @@ -29,7 +32,7 @@ class StringTagField extends DropdownField * @var array */ private static $allowed_actions = [ - 'suggest' + 'suggest', ]; /** @@ -67,8 +70,7 @@ class StringTagField extends DropdownField /** * @param bool $shouldLazyLoad - * - * @return static + * @return $this */ public function setShouldLazyLoad($shouldLazyLoad) { @@ -87,8 +89,7 @@ class StringTagField extends DropdownField /** * @param int $lazyLoadItemLimit - * - * @return static + * @return $this */ public function setLazyLoadItemLimit($lazyLoadItemLimit) { @@ -107,8 +108,7 @@ class StringTagField extends DropdownField /** * @param bool $isMultiple - * - * @return static + * @return $this */ public function setIsMultiple($isMultiple) { @@ -135,7 +135,6 @@ class StringTagField extends DropdownField /** * @param DataObject $record - * * @return $this */ public function setRecord(DataObject $record) @@ -148,7 +147,7 @@ class StringTagField extends DropdownField /** * {@inheritdoc} */ - public function Field($properties = array()) + public function Field($properties = []) { Requirements::css('silverstripe/tagfield:css/select2.min.css'); Requirements::css('silverstripe/tagfield:css/TagField.css'); @@ -165,9 +164,9 @@ class StringTagField extends DropdownField if ($this->getShouldLazyLoad()) { $this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL()); } else { - $properties = array_merge($properties, array( + $properties = array_merge($properties, [ 'Options' => $this->getOptions() - )); + ]); } $this->setAttribute('data-can-create', (int) $this->getCanCreate()); @@ -202,11 +201,11 @@ class StringTagField extends DropdownField foreach ($source as $value) { $options->push( - ArrayData::create(array( + ArrayData::create([ 'Title' => $value, 'Value' => $value, 'Selected' => in_array($value, $values), - )) + ]) ); } @@ -231,8 +230,8 @@ class StringTagField extends DropdownField $value = $source->column('ID'); } - if (is_null($value)) { - $value = array(); + if ($value === null) { + $value = []; } return parent::setValue(array_filter($value)); @@ -245,7 +244,7 @@ class StringTagField extends DropdownField { return array_merge( parent::getAttributes(), - array('name' => $this->getName() . '[]') + ['name' => $this->getName() . '[]'] ); } @@ -258,7 +257,7 @@ class StringTagField extends DropdownField $name = $this->getName(); - $record->$name = join(',', $this->Value()); + $record->$name = implode(',', $this->Value()); $record->write(); } @@ -271,14 +270,14 @@ class StringTagField extends DropdownField public function suggest(HTTPRequest $request) { $responseBody = json_encode( - array('items' => array()) + ['items' => []] ); - $response = new HTTPResponse; + $response = HTTPResponse::create(); $response->addHeader('Content-Type', 'application/json'); if ($record = $this->getRecord()) { - $tags = array(); + $tags = []; $term = $request->getVar('term'); if ($record->hasField($this->getName())) { @@ -286,7 +285,7 @@ class StringTagField extends DropdownField } $responseBody = json_encode( - array('items' => $tags) + ['items' => $tags] ); } @@ -299,7 +298,6 @@ class StringTagField extends DropdownField * Returns array of arrays representing tags. * * @param string $term - * * @return array */ protected function getTags($term) @@ -307,7 +305,7 @@ class StringTagField extends DropdownField $record = $this->getRecord(); if (!$record) { - return array(); + return []; } $fieldName = $this->getName(); @@ -315,7 +313,7 @@ class StringTagField extends DropdownField $term = Convert::raw2sql($term); - $query = $className::get() + $query = DataList::create($className) ->filter($fieldName . ':PartialMatch:nocase', $term) ->limit($this->getLazyLoadItemLimit()); @@ -326,10 +324,10 @@ class StringTagField extends DropdownField foreach ($tags as $i => $tag) { if (stripos($tag, $term) !== false && !in_array($tag, $items)) { - $items[] = array( + $items[] = [ 'id' => $tag, - 'text' => $tag - ); + 'text' => $tag, + ]; } } } @@ -359,8 +357,7 @@ class StringTagField extends DropdownField /** * @param bool $canCreate - * - * @return static + * @return $this */ public function setCanCreate($canCreate) { diff --git a/src/TagField.php b/src/TagField.php index ecb3a11..011d80e 100644 --- a/src/TagField.php +++ b/src/TagField.php @@ -5,9 +5,9 @@ namespace SilverStripe\TagField; use SilverStripe\Control\Controller; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; -use SilverStripe\Core\Convert; use SilverStripe\Core\Injector\Injector; use SilverStripe\Forms\DropdownField; +use SilverStripe\Forms\Validator; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataObject; @@ -27,9 +27,9 @@ class TagField extends DropdownField /** * @var array */ - private static $allowed_actions = array( - 'suggest' - ); + private static $allowed_actions = [ + 'suggest', + ]; /** * @var bool @@ -198,7 +198,7 @@ class TagField extends DropdownField /** * {@inheritdoc} */ - public function Field($properties = array()) + public function Field($properties = []) { Requirements::css('silverstripe/tagfield:client/dist/styles/bundle.css'); Requirements::javascript('silverstripe/tagfield:client/dist/js/bundle.js'); @@ -219,6 +219,7 @@ class TagField extends DropdownField } $schema['optionUrl'] = $this->getSuggestURL(); } + $this->setAttribute('data-schema', json_encode($schema)); $this->addExtraClass('ss-tag-field'); @@ -275,10 +276,10 @@ class TagField extends DropdownField foreach ($source as $object) { $options->push( - ArrayData::create(array( + ArrayData::create([ 'Title' => $object->$titleField, 'Value' => $object->Title, - )) + ]) ); } @@ -316,13 +317,12 @@ class TagField extends DropdownField $name = $this->getName(); $titleField = $this->getTitleField(); - $source = $this->getSource(); $values = $this->Value(); $relation = $record->$name(); - $ids = array(); + $ids = []; if (!$values) { - $values = array(); + $values = []; } if (empty($record) || empty($titleField)) { return; @@ -350,12 +350,11 @@ class TagField extends DropdownField * Get or create tag with the given value * * @param string $term - * @return DataObject + * @return DataObject|bool */ protected function getOrCreateTag($term) { // Check if existing record can be found - /** @var DataList $source */ $source = $this->getSourceList(); $titleField = $this->getTitleField(); $record = $source @@ -372,9 +371,9 @@ class TagField extends DropdownField $record->{$titleField} = $term; $record->write(); return $record; - } else { - return false; } + + return false; } /** @@ -387,9 +386,9 @@ class TagField extends DropdownField { $tags = $this->getTags($request->getVar('term')); - $response = new HTTPResponse(); + $response = HTTPResponse::create(); $response->addHeader('Content-Type', 'application/json'); - $response->setBody(json_encode(array('items' => $tags))); + $response->setBody(json_encode(['items' => $tags])); return $response; } @@ -402,9 +401,6 @@ class TagField extends DropdownField */ protected function getTags($term) { - /** - * @var array $source - */ $source = $this->getSourceList(); $titleField = $this->getTitleField(); @@ -415,13 +411,13 @@ class TagField extends DropdownField ->limit($this->getLazyLoadItemLimit()); // Map into a distinct list - $items = array(); + $items = []; $titleField = $this->getTitleField(); foreach ($query->map('ID', $titleField) as $id => $title) { - $items[$title] = array( + $items[$title] = [ 'id' => $title, - 'text' => $title - ); + 'text' => $title, + ]; } return array_values($items); @@ -442,10 +438,11 @@ class TagField extends DropdownField /** * Converts the field to a readonly variant. * - * @return TagField_Readonly + * @return ReadonlyTagField */ public function performReadonlyTransformation() { + /** @var ReadonlyTagField $copy */ $copy = $this->castedCopy(ReadonlyTagField::class); $copy->setSourceList($this->getSourceList()); return $copy;