From ef5584561c13ce0a298b3f939305360fd7d55c03 Mon Sep 17 00:00:00 2001 From: Junior Grossi Date: Wed, 2 Nov 2016 19:00:52 -0200 Subject: [PATCH 01/15] Add test for field type using snake_case camelCase conversion --- tests/CorcelIntegrationTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/CorcelIntegrationTest.php b/tests/CorcelIntegrationTest.php index a674122..bb6f5f0 100644 --- a/tests/CorcelIntegrationTest.php +++ b/tests/CorcelIntegrationTest.php @@ -18,4 +18,11 @@ public function testUsageOfHelperFunctions() $post = Post::find(56); $this->assertEquals('admin', $post->acf->user('fake_user')->nickname); } + + public function testFunctionHelperWithSnakeCaseFieldType() + { + $post = Post::find(65); + $this->assertEquals('10/13/2016', $post->acf->fake_date_picker->format('m/d/Y')); + $this->assertEquals('10/13/2016', $post->acf->datePicker('fake_date_picker')->format('m/d/Y')); + } } From d7075cbbb88652a9af3af2f978305a4d42175256 Mon Sep 17 00:00:00 2001 From: Junior Grossi Date: Sun, 6 Nov 2016 20:51:55 -0200 Subject: [PATCH 02/15] Remove unnecessary method in AdvancedCustomFields class --- src/AdvancedCustomFields.php | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/AdvancedCustomFields.php b/src/AdvancedCustomFields.php index 08c914b..20dd2f0 100644 --- a/src/AdvancedCustomFields.php +++ b/src/AdvancedCustomFields.php @@ -25,16 +25,6 @@ public function __construct(Post $post) $this->post = $post; } - /** - * @return mixed - */ - public function get($fieldName) - { - $field = FieldFactory::make($fieldName, $this->post); - - return $field->get(); - } - /** * @param string $name * @@ -42,7 +32,9 @@ public function get($fieldName) */ public function __get($name) { - return $this->get($name); + $field = FieldFactory::make($name, $this->post); + + return $field->get(); } /** From fb8d778306facceb969b5f2410704a3f200a22ca Mon Sep 17 00:00:00 2001 From: Junior Grossi Date: Tue, 15 Nov 2016 12:34:18 -0200 Subject: [PATCH 03/15] Set connection name to basic field --- src/Field/BasicField.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Field/BasicField.php b/src/Field/BasicField.php index 25cca15..006ece7 100644 --- a/src/Field/BasicField.php +++ b/src/Field/BasicField.php @@ -42,6 +42,11 @@ abstract class BasicField */ protected $value; + /** + * @var string + */ + protected $connection; + /** * Constructor method. */ @@ -124,4 +129,21 @@ public function __toString() { return $this->get(); } + + /** + * @return string + */ + public function getConnection() + { + return $this->connection; + } + + /** + * @param string $connection + */ + public function setConnection($connection) + { + $this->connection = $connection; + $this->postMeta->setConnection($connection); + } } From de7ce55ca3a5a2cf3f753e4cbcfd40b10f5e1ead Mon Sep 17 00:00:00 2001 From: Junior Grossi Date: Tue, 15 Nov 2016 12:35:00 -0200 Subject: [PATCH 04/15] Set the connection name in factory class --- src/FieldFactory.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/FieldFactory.php b/src/FieldFactory.php index b3081d0..110f7c6 100644 --- a/src/FieldFactory.php +++ b/src/FieldFactory.php @@ -24,6 +24,11 @@ */ class FieldFactory { + /** + * @var null|string + */ + public static $connection = null; + private function __construct() { } @@ -104,6 +109,10 @@ public static function make($name, Post $post, $type = null) break; } + if (static::$connection) { + $field->setConnection(static::$connection); + } + $field->process($name, $post); return $field; From 56d1159ee07d7cbf047a61bf8057e6e2b7c141d8 Mon Sep 17 00:00:00 2001 From: Junior Grossi Date: Tue, 15 Nov 2016 12:37:41 -0200 Subject: [PATCH 05/15] Configure database connection according the post parameter --- src/AdvancedCustomFields.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/AdvancedCustomFields.php b/src/AdvancedCustomFields.php index 20dd2f0..4b5df52 100644 --- a/src/AdvancedCustomFields.php +++ b/src/AdvancedCustomFields.php @@ -23,6 +23,7 @@ class AdvancedCustomFields public function __construct(Post $post) { $this->post = $post; + FieldFactory::$connection = $post->getConnectionName(); } /** From 9d054fead0a6fce6dcf267b1375a42c64174321b Mon Sep 17 00:00:00 2001 From: Junior Grossi Date: Tue, 15 Nov 2016 12:44:09 -0200 Subject: [PATCH 06/15] Allow to find the field key name using the correct database connection name --- src/FieldFactory.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/FieldFactory.php b/src/FieldFactory.php index 110f7c6..4f62a5f 100644 --- a/src/FieldFactory.php +++ b/src/FieldFactory.php @@ -44,6 +44,11 @@ public static function make($name, Post $post, $type = null) { if (null === $type) { $fakeText = new Text(); + + if (static::$connection) { + $fakeText->setConnection(static::$connection); + } + $key = $fakeText->fetchFieldKey($name, $post); if ($key === null) { // Field does not exist From 8acd570f02a4b1334235138e5943eb90917c57d3 Mon Sep 17 00:00:00 2001 From: Junior Grossi Date: Tue, 15 Nov 2016 13:52:51 -0200 Subject: [PATCH 07/15] Check if the post exists with the field key --- src/Field/BasicField.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Field/BasicField.php b/src/Field/BasicField.php index 006ece7..cb4ebb9 100644 --- a/src/Field/BasicField.php +++ b/src/Field/BasicField.php @@ -59,7 +59,7 @@ public function __construct() * Get the value of a field according it's post ID. * * @param string $field - * @param Post $post + * @param Post $post * * @return array|string */ @@ -85,7 +85,7 @@ public function fetchValue($field, Post $post) /** * @param string $fieldName - * @param Post $post + * @param Post $post * * @return string */ @@ -95,7 +95,7 @@ public function fetchFieldKey($fieldName, Post $post) $this->name = $fieldName; $postMeta = $this->postMeta->where('post_id', $post->ID) - ->where('meta_key', '_'.$fieldName) + ->where('meta_key', '_' . $fieldName) ->first(); if (!$postMeta) { @@ -110,16 +110,20 @@ public function fetchFieldKey($fieldName, Post $post) /** * @param string $fieldKey * - * @return string + * @return string|null */ public function fetchFieldType($fieldKey) { $post = $this->post->where('post_name', $fieldKey)->first(); - $fieldData = unserialize($post->post_content); - $this->type = isset($fieldData['type']) ? $fieldData['type'] : 'text'; + if ($post) { + $fieldData = unserialize($post->post_content); + $this->type = isset($fieldData['type']) ? $fieldData['type'] : 'text'; + + return $this->type; + } - return $this->type; + return null; } /** From f9bf79df8f227f9d6911186b3576c4e495e8df82 Mon Sep 17 00:00:00 2001 From: Junior Grossi Date: Wed, 18 Jan 2017 19:01:49 -0200 Subject: [PATCH 08/15] Pass $post to the constructor and set the postMeta connection name --- src/Field/BasicField.php | 58 +++++++++++++++------------------------- 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/src/Field/BasicField.php b/src/Field/BasicField.php index cb4ebb9..e22f622 100644 --- a/src/Field/BasicField.php +++ b/src/Field/BasicField.php @@ -46,26 +46,31 @@ abstract class BasicField * @var string */ protected $connection; - - /** - * Constructor method. - */ - public function __construct() + + /** + * Constructor method. + * + * @param Post $post + */ + public function __construct(Post $post) { + $this->post = $post; $this->postMeta = new PostMeta(); + $this->postMeta->setConnection($post->getConnectionName()); + + dd($this->post->getConnectionName(), $this->postMeta->getConnectionName()); } /** * Get the value of a field according it's post ID. * * @param string $field - * @param Post $post * * @return array|string */ - public function fetchValue($field, Post $post) + public function fetchValue($field) { - $postMeta = $this->postMeta->where('post_id', $post->ID) + $postMeta = $this->postMeta->where('post_id', $this->post->ID) ->where('meta_key', $field) ->first(); @@ -85,20 +90,18 @@ public function fetchValue($field, Post $post) /** * @param string $fieldName - * @param Post $post * * @return string */ - public function fetchFieldKey($fieldName, Post $post) + public function fetchFieldKey($fieldName) { - $this->post = $post; - $this->name = $fieldName; - - $postMeta = $this->postMeta->where('post_id', $post->ID) + $this->name = $fieldName; + + $postMeta = $this->postMeta->where('post_id', $this->post->ID) ->where('meta_key', '_' . $fieldName) ->first(); - - if (!$postMeta) { + + if (!$postMeta) { return null; } @@ -114,9 +117,9 @@ public function fetchFieldKey($fieldName, Post $post) */ public function fetchFieldType($fieldKey) { - $post = $this->post->where('post_name', $fieldKey)->first(); - - if ($post) { + $post = $this->post->where('post_name', $fieldKey)->first(); + + if ($post) { $fieldData = unserialize($post->post_content); $this->type = isset($fieldData['type']) ? $fieldData['type'] : 'text'; @@ -133,21 +136,4 @@ public function __toString() { return $this->get(); } - - /** - * @return string - */ - public function getConnection() - { - return $this->connection; - } - - /** - * @param string $connection - */ - public function setConnection($connection) - { - $this->connection = $connection; - $this->postMeta->setConnection($connection); - } } From f748ce4289f9bbfc92689fca757967d52e7e8828 Mon Sep 17 00:00:00 2001 From: Junior Grossi Date: Wed, 18 Jan 2017 19:03:43 -0200 Subject: [PATCH 09/15] Fix field factory to set $post in field constructor --- src/AdvancedCustomFields.php | 1 - src/Field/BasicField.php | 2 -- src/FieldFactory.php | 54 +++++++++++++----------------------- 3 files changed, 20 insertions(+), 37 deletions(-) diff --git a/src/AdvancedCustomFields.php b/src/AdvancedCustomFields.php index 4b5df52..20dd2f0 100644 --- a/src/AdvancedCustomFields.php +++ b/src/AdvancedCustomFields.php @@ -23,7 +23,6 @@ class AdvancedCustomFields public function __construct(Post $post) { $this->post = $post; - FieldFactory::$connection = $post->getConnectionName(); } /** diff --git a/src/Field/BasicField.php b/src/Field/BasicField.php index e22f622..5d198c9 100644 --- a/src/Field/BasicField.php +++ b/src/Field/BasicField.php @@ -57,8 +57,6 @@ public function __construct(Post $post) $this->post = $post; $this->postMeta = new PostMeta(); $this->postMeta->setConnection($post->getConnectionName()); - - dd($this->post->getConnectionName(), $this->postMeta->getConnectionName()); } /** diff --git a/src/FieldFactory.php b/src/FieldFactory.php index 4f62a5f..dc91485 100644 --- a/src/FieldFactory.php +++ b/src/FieldFactory.php @@ -24,11 +24,6 @@ */ class FieldFactory { - /** - * @var null|string - */ - public static $connection = null; - private function __construct() { } @@ -43,22 +38,17 @@ private function __construct() public static function make($name, Post $post, $type = null) { if (null === $type) { - $fakeText = new Text(); - - if (static::$connection) { - $fakeText->setConnection(static::$connection); - } - - $key = $fakeText->fetchFieldKey($name, $post); - - if ($key === null) { // Field does not exist + $fakeText = new Text($post); + $key = $fakeText->fetchFieldKey($name); + + if ($key === null) { // Field does not exist return null; } - - $type = $fakeText->fetchFieldType($key); + + $type = $fakeText->fetchFieldType($key); } - - switch ($type) { + + switch ($type) { case 'text': case 'textarea': case 'number': @@ -73,52 +63,48 @@ public static function make($name, Post $post, $type = null) case 'select': case 'checkbox': case 'radio': - $field = new Text(); + $field = new Text($post); break; case 'image': case 'img': - $field = new Image(); + $field = new Image($post); break; case 'file': - $field = new File(); + $field = new File($post); break; case 'gallery': - $field = new Gallery(); + $field = new Gallery($post); break; case 'true_false': case 'boolean': - $field = new Boolean(); + $field = new Boolean($post); break; case 'post_object': case 'post': case 'relationship': - $field = new PostObject(); + $field = new PostObject($post); break; case 'page_link': - $field = new PageLink(); + $field = new PageLink($post); break; case 'taxonomy': case 'term': - $field = new Term(); + $field = new Term($post); break; case 'user': - $field = new User(); + $field = new User($post); break; case 'date_picker': case 'date_time_picker': case 'time_picker': - $field = new DateTime(); + $field = new DateTime($post); break; case 'repeater': - $field = new Repeater(); + $field = new Repeater($post); break; } - if (static::$connection) { - $field->setConnection(static::$connection); - } - - $field->process($name, $post); + $field->process($name); return $field; } From 90690726fe4eb255fbb91afac9c41a29f5632348 Mon Sep 17 00:00:00 2001 From: Junior Grossi Date: Wed, 18 Jan 2017 19:04:12 -0200 Subject: [PATCH 10/15] Fix field interface to remove $post from process() method --- src/FieldInterface.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/FieldInterface.php b/src/FieldInterface.php index 9d4d280..ae45dab 100644 --- a/src/FieldInterface.php +++ b/src/FieldInterface.php @@ -13,9 +13,8 @@ interface FieldInterface { /** * @param string $fieldName - * @param Post $post */ - public function process($fieldName, Post $post); + public function process($fieldName); /** * @return mixed From b318ff3b60a89ae839e027bfc0eebc4834e92a1f Mon Sep 17 00:00:00 2001 From: Junior Grossi Date: Wed, 18 Jan 2017 19:09:43 -0200 Subject: [PATCH 11/15] Update process() method removing the post object --- src/Field/DateTime.php | 5 ++--- src/Field/File.php | 7 +++---- src/Field/Gallery.php | 9 ++++----- src/Field/Image.php | 11 +++++------ src/Field/PostObject.php | 9 ++++----- src/Field/Repeater.php | 6 ++---- src/Field/Term.php | 15 +++++++++------ src/Field/Text.php | 9 ++++++--- src/Field/User.php | 15 +++++++++------ 9 files changed, 44 insertions(+), 42 deletions(-) diff --git a/src/Field/DateTime.php b/src/Field/DateTime.php index fc3aba1..f75860c 100644 --- a/src/Field/DateTime.php +++ b/src/Field/DateTime.php @@ -20,11 +20,10 @@ class DateTime extends BasicField implements FieldInterface /** * @param string $fieldName - * @param Post $post */ - public function process($fieldName, Post $post) + public function process($fieldName) { - $dateString = $this->fetchValue($fieldName, $post); + $dateString = $this->fetchValue($fieldName); $format = $this->getDateFormatFromString($dateString); $this->date = Carbon::createFromFormat($format, $dateString); } diff --git a/src/Field/File.php b/src/Field/File.php index c7ab0a9..7c03259 100644 --- a/src/Field/File.php +++ b/src/Field/File.php @@ -44,12 +44,11 @@ class File extends BasicField implements FieldInterface /** * @param string $field - * @param Post $post */ - public function process($field, Post $post) + public function process($field) { - $value = $this->fetchValue($field, $post); - $file = $post->find($value); + $value = $this->fetchValue($field); + $file = $this->post->find($value); $this->fillFields($file); } diff --git a/src/Field/Gallery.php b/src/Field/Gallery.php index 3ffb9ce..27ef697 100644 --- a/src/Field/Gallery.php +++ b/src/Field/Gallery.php @@ -25,16 +25,15 @@ class Gallery extends Image implements FieldInterface /** * @param $field - * @param Post $post */ - public function process($field, Post $post) + public function process($field) { - $ids = $this->fetchValue($field, $post); - $attachments = $post->whereIn('ID', $ids)->get(); + $ids = $this->fetchValue($field); + $attachments = $this->post->whereIn('ID', $ids)->get(); $metaDataValues = $this->fetchMultipleMetadataValues($attachments); foreach ($attachments as $attachment) { - $image = new Image(); + $image = new Image($this->post); $image->fillFields($attachment); $image->fillMetadataFields($metaDataValues[$attachment->ID]); $this->images[] = $image; diff --git a/src/Field/Image.php b/src/Field/Image.php index be19ae6..11fdfa4 100644 --- a/src/Field/Image.php +++ b/src/Field/Image.php @@ -55,13 +55,12 @@ class Image extends BasicField implements FieldInterface /** * @param string $field - * @param Post $post */ - public function process($field, Post $post) + public function process($field) { - $attachmentId = $this->fetchValue($field, $post); + $attachmentId = $this->fetchValue($field); - $attachment = $post->find(intval($attachmentId)); + $attachment = $this->post->find(intval($attachmentId)); $this->fillFields($attachment); $imageData = $this->fetchMetadataValue($attachment); @@ -107,7 +106,7 @@ public function size($size) */ protected function fillThumbnailFields(array $data) { - $size = new static(); + $size = new static($this->post); $size->filename = $data['file']; $size->width = $data['width']; $size->height = $data['height']; @@ -133,7 +132,7 @@ protected function fetchMetadataValue(Post $attachment) /** * @param Collection $attachments * - * @return Collection + * @return Collection|array */ protected function fetchMultipleMetadataValues(Collection $attachments) { diff --git a/src/Field/PostObject.php b/src/Field/PostObject.php index 523e27f..2516e26 100644 --- a/src/Field/PostObject.php +++ b/src/Field/PostObject.php @@ -19,16 +19,15 @@ class PostObject extends BasicField implements FieldInterface /** * @param string $fieldName - * @param Post $post */ - public function process($fieldName, Post $post) + public function process($fieldName) { - $postId = $this->fetchValue($fieldName, $post); + $postId = $this->fetchValue($fieldName); if (is_array($postId)) { - $this->object = $post->whereIn('ID', $postId)->get(); + $this->object = $this->post->whereIn('ID', $postId)->get(); } else { - $this->object = $post->find($postId); + $this->object = $this->post->find($postId); } } diff --git a/src/Field/Repeater.php b/src/Field/Repeater.php index fd1c0ad..ec47626 100644 --- a/src/Field/Repeater.php +++ b/src/Field/Repeater.php @@ -22,14 +22,12 @@ class Repeater extends BasicField implements FieldInterface /** * @param string $fieldName - * @param Post $post */ - public function process($fieldName, Post $post) + public function process($fieldName) { $this->name = $fieldName; - $this->post = $post; - $builder = $this->fetchPostsMeta($fieldName, $post); + $builder = $this->fetchPostsMeta($fieldName, $this->post); $fields = $this->fetchFields($fieldName, $builder); $this->fields = new Collection($fields); diff --git a/src/Field/Term.php b/src/Field/Term.php index aa40923..32566d6 100644 --- a/src/Field/Term.php +++ b/src/Field/Term.php @@ -22,20 +22,23 @@ class Term extends BasicField implements FieldInterface * @var \Corcel\Term */ protected $term; - - public function __construct() + + /** + * @param Post $post + */ + public function __construct(Post $post) { - parent::__construct(); + parent::__construct($post); $this->term = new \Corcel\Term(); + $this->term->setConnection($post->getConnectionName()); } /** * @param string $fieldName - * @param Post $post */ - public function process($fieldName, Post $post) + public function process($fieldName) { - $value = $this->fetchValue($fieldName, $post); + $value = $this->fetchValue($fieldName); if (is_array($value)) { $this->items = $this->term->whereIn('term_id', $value)->get(); // ids } else { diff --git a/src/Field/Text.php b/src/Field/Text.php index 3c9ce0d..20d60c7 100644 --- a/src/Field/Text.php +++ b/src/Field/Text.php @@ -16,10 +16,13 @@ class Text extends BasicField implements FieldInterface * @var string */ protected $value; - - public function process($field, Post $post) + + /** + * @param string $field + */ + public function process($field) { - $this->value = $this->fetchValue($field, $post); + $this->value = $this->fetchValue($field); } /** diff --git a/src/Field/User.php b/src/Field/User.php index 5d33ee5..83e8fa9 100644 --- a/src/Field/User.php +++ b/src/Field/User.php @@ -21,20 +21,23 @@ class User extends BasicField implements FieldInterface * @var \Corcel\User */ protected $value; - - public function __construct() + + /** + * @param Post $post + */ + public function __construct(Post $post) { - parent::__construct(); + parent::__construct($post); $this->user = new \Corcel\User(); + $this->user->setConnection($post->getConnectionName()); } /** * @param string $fieldName - * @param Post $post */ - public function process($fieldName, Post $post) + public function process($fieldName) { - $userId = $this->fetchValue($fieldName, $post); + $userId = $this->fetchValue($fieldName); $this->value = $this->user->find($userId); } From 41dcec788b2d4c2a8c456c321aece19de819331d Mon Sep 17 00:00:00 2001 From: Junior Grossi Date: Wed, 18 Jan 2017 19:13:30 -0200 Subject: [PATCH 12/15] Fix tests with process() change --- tests/BasicFieldsTest.php | 24 ++++++++++++------------ tests/ChoicesFieldsTest.php | 20 ++++++++++---------- tests/ContentFieldsTest.php | 20 ++++++++++---------- tests/JqueryFieldsTest.php | 16 ++++++++-------- tests/LayoutFieldsTest.php | 8 ++++---- tests/RelationalFieldsTest.php | 24 ++++++++++++------------ 6 files changed, 56 insertions(+), 56 deletions(-) diff --git a/tests/BasicFieldsTest.php b/tests/BasicFieldsTest.php index e62cae6..0b8f11b 100644 --- a/tests/BasicFieldsTest.php +++ b/tests/BasicFieldsTest.php @@ -25,48 +25,48 @@ protected function setUp() public function testTextFieldValue() { - $text = new Text(); - $text->process('fake_text', $this->post); + $text = new Text($this->post); + $text->process('fake_text'); $this->assertEquals('Proin eget tortor risus', $text->get()); } public function testTextareaFieldValue() { - $textarea = new Text(); - $textarea->process('fake_textarea', $this->post); + $textarea = new Text($this->post); + $textarea->process('fake_textarea'); $this->assertEquals('Praesent sapien massa, convallis a pellentesque nec, egestas non nisi.', $textarea->get()); } public function testNumberFieldValue() { - $number = new Text(); - $number->process('fake_number', $this->post); + $number = new Text($this->post); + $number->process('fake_number'); $this->assertEquals('1984', $number->get()); } public function testEmailFieldValue() { - $email = new Text(); - $email->process('fake_email', $this->post); + $email = new Text($this->post); + $email->process('fake_email'); $this->assertEquals('junior@corcel.org', $email->get()); } public function testUrlFieldValue() { - $url = new Text(); - $url->process('fake_url', $this->post); + $url = new Text($this->post); + $url->process('fake_url'); $this->assertEquals('https://corcel.org', $url->get()); } public function testPasswordFieldValue() { - $password = new Text(); - $password->process('fake_password', $this->post); + $password = new Text($this->post); + $password->process('fake_password'); $this->assertEquals('123change', $password->get()); } diff --git a/tests/ChoicesFieldsTest.php b/tests/ChoicesFieldsTest.php index a905bad..c383ad4 100644 --- a/tests/ChoicesFieldsTest.php +++ b/tests/ChoicesFieldsTest.php @@ -23,36 +23,36 @@ public function setUp() public function testSelectField() { - $select = new Text(); - $select->process('fake_select', $this->post); + $select = new Text($this->post); + $select->process('fake_select'); $this->assertEquals('red', $select->get()); } public function testSelectMultipleField() { - $select = new Text(); - $select->process('fake_select_multiple', $this->post); + $select = new Text($this->post); + $select->process('fake_select_multiple'); $this->assertEquals(['yellow', 'green'], $select->get()); } public function testCheckboxField() { - $check = new Text(); - $check->process('fake_checkbox', $this->post); + $check = new Text($this->post); + $check->process('fake_checkbox'); $this->assertEquals(['blue', 'yellow'], $check->get()); } public function testRadioField() { - $radio = new Text(); - $radio->process('fake_radio_button', $this->post); + $radio = new Text($this->post); + $radio->process('fake_radio_button'); $this->assertEquals('green', $radio->get()); } public function testTrueFalseField() { - $boolean = new Boolean(); - $boolean->process('fake_true_false', $this->post); + $boolean = new Boolean($this->post); + $boolean->process('fake_true_false'); $this->assertTrue($boolean->get()); } } diff --git a/tests/ContentFieldsTest.php b/tests/ContentFieldsTest.php index ffd601c..b47e38b 100644 --- a/tests/ContentFieldsTest.php +++ b/tests/ContentFieldsTest.php @@ -28,8 +28,8 @@ protected function setUp() public function testEditorFieldValue() { - $field = new Text(); - $field->process('fake_editor', $this->post); + $field = new Text($this->post); + $field->process('fake_editor'); $this->assertEquals( 'Nulla porttitor accumsan tincidunt. Sed porttitor lectus nibh.', @@ -39,16 +39,16 @@ public function testEditorFieldValue() public function testOembedFieldValue() { - $field = new Text(); - $field->process('fake_oembed', $this->post); + $field = new Text($this->post); + $field->process('fake_oembed'); $this->assertEquals('https://www.youtube.com/watch?v=LiyQ8bvLzIE', $field->get()); } public function testImageFieldValue() { - $image = new Image(); - $image->process('fake_image', $this->post); + $image = new Image($this->post); + $image->process('fake_image'); $this->assertEquals('1920', $image->width); $this->assertEquals('1080', $image->height); @@ -60,8 +60,8 @@ public function testImageFieldValue() public function testFileFieldValue() { - $file = new File(); - $file->process('fake_file', $this->post); + $file = new File($this->post); + $file->process('fake_file'); $this->assertEquals('Description here', $file->description); $this->assertEquals('Title here', $file->title); @@ -72,8 +72,8 @@ public function testFileFieldValue() public function testGalleryFieldValue() { - $gallery = new Gallery(); - $gallery->process('fake_gallery', $this->post); + $gallery = new Gallery($this->post); + $gallery->process('fake_gallery'); $this->assertEquals(7, $gallery->get()->count()); diff --git a/tests/JqueryFieldsTest.php b/tests/JqueryFieldsTest.php index 61df761..d63b8f7 100644 --- a/tests/JqueryFieldsTest.php +++ b/tests/JqueryFieldsTest.php @@ -23,29 +23,29 @@ public function testGoogleMapField() public function testDatePickerField() { - $date = new DateTime(); - $date->process('fake_date_picker', $this->post); + $date = new DateTime($this->post); + $date->process('fake_date_picker'); $this->assertEquals('10/13/2016', $date->get()->format('m/d/Y')); } public function testDateTimePickerField() { - $dateTime = new DateTime(); - $dateTime->process('fake_date_time_picker', $this->post); + $dateTime = new DateTime($this->post); + $dateTime->process('fake_date_time_picker'); $this->assertEquals('05:06:08/19-10:2016', $dateTime->get()->format('s:i:H/d-m:Y')); // 2016-10-19 08:06:05 } public function testTimePickerField() { - $time = new DateTime(); - $time->process('fake_time_picker', $this->post); + $time = new DateTime($this->post); + $time->process('fake_time_picker'); $this->assertEquals('00/17/30', $time->get()->format('s/H/i')); // 17:30:00 } public function testColorPickerField() { - $color = new Text(); - $color->process('fake_color_picker', $this->post); + $color = new Text($this->post); + $color->process('fake_color_picker'); $this->assertEquals('#7263a8', $color->get()); } } diff --git a/tests/LayoutFieldsTest.php b/tests/LayoutFieldsTest.php index 3283a5b..d5a4d9e 100644 --- a/tests/LayoutFieldsTest.php +++ b/tests/LayoutFieldsTest.php @@ -12,8 +12,8 @@ class LayoutFieldsTest extends PHPUnit_Framework_TestCase public function testRepeaterField() { $page = Post::find(73); - $repeater = new Repeater(); - $repeater->process('fake_repeater', $page); + $repeater = new Repeater($page); + $repeater->process('fake_repeater'); $fields = $repeater->get()->toArray(); $this->assertEquals('First text', $fields[0]['repeater_text']); @@ -25,8 +25,8 @@ public function testRepeaterField() public function testComplexRepeaterField() { $page = Post::find(73); - $repeater = new Repeater(); - $repeater->process('fake_repeater_2', $page); + $repeater = new Repeater($page); + $repeater->process('fake_repeater_2'); $fields = $repeater->get()->toArray(); $this->assertEquals('admin', $fields[0]['fake_user']->nickname); diff --git a/tests/RelationalFieldsTest.php b/tests/RelationalFieldsTest.php index baef2b1..9e07ac1 100644 --- a/tests/RelationalFieldsTest.php +++ b/tests/RelationalFieldsTest.php @@ -25,41 +25,41 @@ public function setUp() public function testPostObjectField() { - $object = new PostObject(); - $object->process('fake_post_object', $this->post); + $object = new PostObject($this->post); + $object->process('fake_post_object'); $this->assertEquals('ACF Basic Fields', $object->get()->post_title); } public function testPageLinkField() { - $page = new PageLink(); - $page->process('fake_page_link', $this->post); + $page = new PageLink($this->post); + $page->process('fake_page_link'); $this->assertEquals('http://wordpress.corcel.dev/acf-content-fields/', $page->get()); } public function testRelationshipField() { - $relation = new PostObject(); - $relation->process('fake_relationship', $this->post); + $relation = new PostObject($this->post); + $relation->process('fake_relationship'); $posts = $relation->get(); $this->assertEquals([44, 56], $posts->pluck('ID')->toArray()); } public function testTaxonomyField() { - $relation = new Term(); - - $relation->process('fake_taxonomy', $this->post); // multiple (Collection) + $relation = new Term($this->post); + + $relation->process('fake_taxonomy'); // multiple (Collection) $this->assertEquals('uncategorized', $relation->get()->first()->slug); - $relation->process('fake_taxonomy_single', $this->post); // single (Corcel\Term) + $relation->process('fake_taxonomy_single'); // single (Corcel\Term) $this->assertEquals('uncategorized', $relation->get()->slug); } public function testUserField() { - $user = new User(); - $user->process('fake_user', $this->post); + $user = new User($this->post); + $user->process('fake_user'); $this->assertEquals('admin', $user->get()->user_login); $this->assertEquals('admin', $user->get()->nickname); } From 4b509e89877350a6b8d5d85368e1f649ba304bc0 Mon Sep 17 00:00:00 2001 From: Junior Grossi Date: Wed, 18 Jan 2017 19:19:27 -0200 Subject: [PATCH 13/15] Format code --- src/Field/BasicField.php | 226 +++++++++++++++++++-------------------- src/FieldFactory.php | 166 ++++++++++++++-------------- 2 files changed, 196 insertions(+), 196 deletions(-) diff --git a/src/Field/BasicField.php b/src/Field/BasicField.php index 5d198c9..fed2489 100644 --- a/src/Field/BasicField.php +++ b/src/Field/BasicField.php @@ -12,126 +12,126 @@ */ abstract class BasicField { - /** - * @var Post - */ - protected $post; - - /** - * @var PostMeta - */ - protected $postMeta; - - /** - * @var string - */ - protected $name; - - /** - * @var string - */ - protected $key; - - /** - * @var string - */ - protected $type; - - /** - * @var mixed - */ - protected $value; - - /** - * @var string - */ - protected $connection; + /** + * @var Post + */ + protected $post; + + /** + * @var PostMeta + */ + protected $postMeta; + + /** + * @var string + */ + protected $name; + + /** + * @var string + */ + protected $key; + + /** + * @var string + */ + protected $type; + + /** + * @var mixed + */ + protected $value; + + /** + * @var string + */ + protected $connection; /** * Constructor method. * * @param Post $post */ - public function __construct(Post $post) - { - $this->post = $post; - $this->postMeta = new PostMeta(); - $this->postMeta->setConnection($post->getConnectionName()); - } - - /** - * Get the value of a field according it's post ID. - * - * @param string $field - * - * @return array|string - */ - public function fetchValue($field) - { - $postMeta = $this->postMeta->where('post_id', $this->post->ID) - ->where('meta_key', $field) - ->first(); - - if (isset($postMeta->meta_value) and $postMeta->meta_value) { - $value = $postMeta->meta_value; - if ($array = @unserialize($value) and is_array($array)) { - $this->value = $array; - - return $array; - } else { - $this->value = $value; - - return $value; - } - } - } - - /** - * @param string $fieldName - * - * @return string - */ - public function fetchFieldKey($fieldName) - { - $this->name = $fieldName; + public function __construct(Post $post) + { + $this->post = $post; + $this->postMeta = new PostMeta(); + $this->postMeta->setConnection($post->getConnectionName()); + } - $postMeta = $this->postMeta->where('post_id', $this->post->ID) - ->where('meta_key', '_' . $fieldName) - ->first(); + /** + * Get the value of a field according it's post ID. + * + * @param string $field + * + * @return array|string + */ + public function fetchValue($field) + { + $postMeta = $this->postMeta->where('post_id', $this->post->ID) + ->where('meta_key', $field) + ->first(); + + if (isset($postMeta->meta_value) and $postMeta->meta_value) { + $value = $postMeta->meta_value; + if ($array = @unserialize($value) and is_array($array)) { + $this->value = $array; + + return $array; + } else { + $this->value = $value; + + return $value; + } + } + } - if (!$postMeta) { - return null; - } - - $this->key = $postMeta->meta_value; - - return $this->key; - } - - /** - * @param string $fieldKey - * - * @return string|null - */ - public function fetchFieldType($fieldKey) - { - $post = $this->post->where('post_name', $fieldKey)->first(); + /** + * @param string $fieldName + * + * @return string + */ + public function fetchFieldKey($fieldName) + { + $this->name = $fieldName; + + $postMeta = $this->postMeta->where('post_id', $this->post->ID) + ->where('meta_key', '_'.$fieldName) + ->first(); + + if (!$postMeta) { + return null; + } + + $this->key = $postMeta->meta_value; + + return $this->key; + } - if ($post) { - $fieldData = unserialize($post->post_content); - $this->type = isset($fieldData['type']) ? $fieldData['type'] : 'text'; - - return $this->type; - } - - return null; - } - - /** - * @return mixed - */ - public function __toString() - { - return $this->get(); - } + /** + * @param string $fieldKey + * + * @return string|null + */ + public function fetchFieldType($fieldKey) + { + $post = $this->post->where('post_name', $fieldKey)->first(); + + if ($post) { + $fieldData = unserialize($post->post_content); + $this->type = isset($fieldData['type']) ? $fieldData['type'] : 'text'; + + return $this->type; + } + + return null; + } + + /** + * @return mixed + */ + public function __toString() + { + return $this->get(); + } } diff --git a/src/FieldFactory.php b/src/FieldFactory.php index dc91485..b751062 100644 --- a/src/FieldFactory.php +++ b/src/FieldFactory.php @@ -24,88 +24,88 @@ */ class FieldFactory { - private function __construct() - { - } - - /** - * @param string $name - * @param Post $post - * @param null|string $type - * - * @return FieldInterface|Collection|string - */ - public static function make($name, Post $post, $type = null) - { - if (null === $type) { - $fakeText = new Text($post); - $key = $fakeText->fetchFieldKey($name); - - if ($key === null) { // Field does not exist - return null; - } - - $type = $fakeText->fetchFieldType($key); - } + private function __construct() + { + } - switch ($type) { - case 'text': - case 'textarea': - case 'number': - case 'email': - case 'url': - case 'password': - case 'wysiwyg': - case 'editor': - case 'oembed': - case 'embed': - case 'color_picker': - case 'select': - case 'checkbox': - case 'radio': - $field = new Text($post); - break; - case 'image': - case 'img': - $field = new Image($post); - break; - case 'file': - $field = new File($post); - break; - case 'gallery': - $field = new Gallery($post); - break; - case 'true_false': - case 'boolean': - $field = new Boolean($post); - break; - case 'post_object': - case 'post': - case 'relationship': - $field = new PostObject($post); - break; - case 'page_link': - $field = new PageLink($post); - break; - case 'taxonomy': - case 'term': - $field = new Term($post); - break; - case 'user': - $field = new User($post); - break; - case 'date_picker': - case 'date_time_picker': - case 'time_picker': - $field = new DateTime($post); - break; - case 'repeater': - $field = new Repeater($post); - break; - } - - $field->process($name); - - return $field; - } + /** + * @param string $name + * @param Post $post + * @param null|string $type + * + * @return FieldInterface|Collection|string + */ + public static function make($name, Post $post, $type = null) + { + if (null === $type) { + $fakeText = new Text($post); + $key = $fakeText->fetchFieldKey($name); + + if ($key === null) { // Field does not exist + return null; + } + + $type = $fakeText->fetchFieldType($key); + } + + switch ($type) { + case 'text': + case 'textarea': + case 'number': + case 'email': + case 'url': + case 'password': + case 'wysiwyg': + case 'editor': + case 'oembed': + case 'embed': + case 'color_picker': + case 'select': + case 'checkbox': + case 'radio': + $field = new Text($post); + break; + case 'image': + case 'img': + $field = new Image($post); + break; + case 'file': + $field = new File($post); + break; + case 'gallery': + $field = new Gallery($post); + break; + case 'true_false': + case 'boolean': + $field = new Boolean($post); + break; + case 'post_object': + case 'post': + case 'relationship': + $field = new PostObject($post); + break; + case 'page_link': + $field = new PageLink($post); + break; + case 'taxonomy': + case 'term': + $field = new Term($post); + break; + case 'user': + $field = new User($post); + break; + case 'date_picker': + case 'date_time_picker': + case 'time_picker': + $field = new DateTime($post); + break; + case 'repeater': + $field = new Repeater($post); + break; + } + + $field->process($name); + + return $field; + } } From fc74735d5c4448b2ea41c02862a52c728efca36d Mon Sep 17 00:00:00 2001 From: Junior Grossi Date: Wed, 18 Jan 2017 19:21:46 -0200 Subject: [PATCH 14/15] Fix StyleCI --- src/Field/BasicField.php | 244 ++++++++++++++++----------------- src/FieldFactory.php | 168 +++++++++++------------ tests/RelationalFieldsTest.php | 2 +- 3 files changed, 207 insertions(+), 207 deletions(-) diff --git a/src/Field/BasicField.php b/src/Field/BasicField.php index fed2489..a75655d 100644 --- a/src/Field/BasicField.php +++ b/src/Field/BasicField.php @@ -12,126 +12,126 @@ */ abstract class BasicField { - /** - * @var Post - */ - protected $post; - - /** - * @var PostMeta - */ - protected $postMeta; - - /** - * @var string - */ - protected $name; - - /** - * @var string - */ - protected $key; - - /** - * @var string - */ - protected $type; - - /** - * @var mixed - */ - protected $value; - - /** - * @var string - */ - protected $connection; - - /** - * Constructor method. - * - * @param Post $post - */ - public function __construct(Post $post) - { - $this->post = $post; - $this->postMeta = new PostMeta(); - $this->postMeta->setConnection($post->getConnectionName()); - } - - /** - * Get the value of a field according it's post ID. - * - * @param string $field - * - * @return array|string - */ - public function fetchValue($field) - { - $postMeta = $this->postMeta->where('post_id', $this->post->ID) - ->where('meta_key', $field) - ->first(); - - if (isset($postMeta->meta_value) and $postMeta->meta_value) { - $value = $postMeta->meta_value; - if ($array = @unserialize($value) and is_array($array)) { - $this->value = $array; - - return $array; - } else { - $this->value = $value; - - return $value; - } - } - } - - /** - * @param string $fieldName - * - * @return string - */ - public function fetchFieldKey($fieldName) - { - $this->name = $fieldName; - - $postMeta = $this->postMeta->where('post_id', $this->post->ID) - ->where('meta_key', '_'.$fieldName) - ->first(); - - if (!$postMeta) { - return null; - } - - $this->key = $postMeta->meta_value; - - return $this->key; - } - - /** - * @param string $fieldKey - * - * @return string|null - */ - public function fetchFieldType($fieldKey) - { - $post = $this->post->where('post_name', $fieldKey)->first(); - - if ($post) { - $fieldData = unserialize($post->post_content); - $this->type = isset($fieldData['type']) ? $fieldData['type'] : 'text'; - - return $this->type; - } - - return null; - } - - /** - * @return mixed - */ - public function __toString() - { - return $this->get(); - } + /** + * @var Post + */ + protected $post; + + /** + * @var PostMeta + */ + protected $postMeta; + + /** + * @var string + */ + protected $name; + + /** + * @var string + */ + protected $key; + + /** + * @var string + */ + protected $type; + + /** + * @var mixed + */ + protected $value; + + /** + * @var string + */ + protected $connection; + + /** + * Constructor method. + * + * @param Post $post + */ + public function __construct(Post $post) + { + $this->post = $post; + $this->postMeta = new PostMeta(); + $this->postMeta->setConnection($post->getConnectionName()); + } + + /** + * Get the value of a field according it's post ID. + * + * @param string $field + * + * @return array|string + */ + public function fetchValue($field) + { + $postMeta = $this->postMeta->where('post_id', $this->post->ID) + ->where('meta_key', $field) + ->first(); + + if (isset($postMeta->meta_value) and $postMeta->meta_value) { + $value = $postMeta->meta_value; + if ($array = @unserialize($value) and is_array($array)) { + $this->value = $array; + + return $array; + } else { + $this->value = $value; + + return $value; + } + } + } + + /** + * @param string $fieldName + * + * @return string + */ + public function fetchFieldKey($fieldName) + { + $this->name = $fieldName; + + $postMeta = $this->postMeta->where('post_id', $this->post->ID) + ->where('meta_key', '_' . $fieldName) + ->first(); + + if (!$postMeta) { + return null; + } + + $this->key = $postMeta->meta_value; + + return $this->key; + } + + /** + * @param string $fieldKey + * + * @return string|null + */ + public function fetchFieldType($fieldKey) + { + $post = $this->post->where('post_name', $fieldKey)->first(); + + if ($post) { + $fieldData = unserialize($post->post_content); + $this->type = isset($fieldData['type']) ? $fieldData['type'] : 'text'; + + return $this->type; + } + + return null; + } + + /** + * @return mixed + */ + public function __toString() + { + return $this->get(); + } } diff --git a/src/FieldFactory.php b/src/FieldFactory.php index b751062..b423ab6 100644 --- a/src/FieldFactory.php +++ b/src/FieldFactory.php @@ -24,88 +24,88 @@ */ class FieldFactory { - private function __construct() - { - } - - /** - * @param string $name - * @param Post $post - * @param null|string $type - * - * @return FieldInterface|Collection|string - */ - public static function make($name, Post $post, $type = null) - { - if (null === $type) { - $fakeText = new Text($post); - $key = $fakeText->fetchFieldKey($name); - - if ($key === null) { // Field does not exist - return null; - } - - $type = $fakeText->fetchFieldType($key); - } - - switch ($type) { - case 'text': - case 'textarea': - case 'number': - case 'email': - case 'url': - case 'password': - case 'wysiwyg': - case 'editor': - case 'oembed': - case 'embed': - case 'color_picker': - case 'select': - case 'checkbox': - case 'radio': - $field = new Text($post); - break; - case 'image': - case 'img': - $field = new Image($post); - break; - case 'file': - $field = new File($post); - break; - case 'gallery': - $field = new Gallery($post); - break; - case 'true_false': - case 'boolean': - $field = new Boolean($post); - break; - case 'post_object': - case 'post': - case 'relationship': - $field = new PostObject($post); - break; - case 'page_link': - $field = new PageLink($post); - break; - case 'taxonomy': - case 'term': - $field = new Term($post); - break; - case 'user': - $field = new User($post); - break; - case 'date_picker': - case 'date_time_picker': - case 'time_picker': - $field = new DateTime($post); - break; - case 'repeater': - $field = new Repeater($post); - break; - } - - $field->process($name); - - return $field; - } + private function __construct() + { + } + + /** + * @param string $name + * @param Post $post + * @param null|string $type + * + * @return FieldInterface|Collection|string + */ + public static function make($name, Post $post, $type = null) + { + if (null === $type) { + $fakeText = new Text($post); + $key = $fakeText->fetchFieldKey($name); + + if ($key === null) { // Field does not exist + return null; + } + + $type = $fakeText->fetchFieldType($key); + } + + switch ($type) { + case 'text': + case 'textarea': + case 'number': + case 'email': + case 'url': + case 'password': + case 'wysiwyg': + case 'editor': + case 'oembed': + case 'embed': + case 'color_picker': + case 'select': + case 'checkbox': + case 'radio': + $field = new Text($post); + break; + case 'image': + case 'img': + $field = new Image($post); + break; + case 'file': + $field = new File($post); + break; + case 'gallery': + $field = new Gallery($post); + break; + case 'true_false': + case 'boolean': + $field = new Boolean($post); + break; + case 'post_object': + case 'post': + case 'relationship': + $field = new PostObject($post); + break; + case 'page_link': + $field = new PageLink($post); + break; + case 'taxonomy': + case 'term': + $field = new Term($post); + break; + case 'user': + $field = new User($post); + break; + case 'date_picker': + case 'date_time_picker': + case 'time_picker': + $field = new DateTime($post); + break; + case 'repeater': + $field = new Repeater($post); + break; + } + + $field->process($name); + + return $field; + } } diff --git a/tests/RelationalFieldsTest.php b/tests/RelationalFieldsTest.php index 9e07ac1..9c9b66c 100644 --- a/tests/RelationalFieldsTest.php +++ b/tests/RelationalFieldsTest.php @@ -48,7 +48,7 @@ public function testRelationshipField() public function testTaxonomyField() { $relation = new Term($this->post); - + $relation->process('fake_taxonomy'); // multiple (Collection) $this->assertEquals('uncategorized', $relation->get()->first()->slug); From f47fbfb46572ea398ae65ea087e68e0c7fbf107e Mon Sep 17 00:00:00 2001 From: Junior Grossi Date: Wed, 18 Jan 2017 19:23:02 -0200 Subject: [PATCH 15/15] More fix for StyleCI --- src/Field/Term.php | 8 ++++---- src/Field/Text.php | 8 ++++---- src/Field/User.php | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Field/Term.php b/src/Field/Term.php index 32566d6..531547f 100644 --- a/src/Field/Term.php +++ b/src/Field/Term.php @@ -22,10 +22,10 @@ class Term extends BasicField implements FieldInterface * @var \Corcel\Term */ protected $term; - - /** - * @param Post $post - */ + + /** + * @param Post $post + */ public function __construct(Post $post) { parent::__construct($post); diff --git a/src/Field/Text.php b/src/Field/Text.php index 20d60c7..2b3f50c 100644 --- a/src/Field/Text.php +++ b/src/Field/Text.php @@ -16,10 +16,10 @@ class Text extends BasicField implements FieldInterface * @var string */ protected $value; - - /** - * @param string $field - */ + + /** + * @param string $field + */ public function process($field) { $this->value = $this->fetchValue($field); diff --git a/src/Field/User.php b/src/Field/User.php index 83e8fa9..09aada2 100644 --- a/src/Field/User.php +++ b/src/Field/User.php @@ -21,10 +21,10 @@ class User extends BasicField implements FieldInterface * @var \Corcel\User */ protected $value; - - /** - * @param Post $post - */ + + /** + * @param Post $post + */ public function __construct(Post $post) { parent::__construct($post);