Skip to content
Snippets Groups Projects
Commit f27462fd authored by IMMACULATE X's avatar IMMACULATE X Committed by Youri van Koppen
Browse files

Issue #3452563 by megachriz, bharath-kondeti, ankitv18, bwoods, baikho: Added...

Issue #3452563 by megachriz, bharath-kondeti, ankitv18, bwoods, baikho: Added different implementation to use dynamic properties in \Drupal\feeds\Feeds\Item\BaseItem to avoid PHP 8.2 warnings.
parent 962a6043
Branches
No related tags found
1 merge request!174Update file SyndicationItem.php
Pipeline #500187 passed with warnings
......@@ -7,26 +7,87 @@ namespace Drupal\feeds\Feeds\Item;
*/
abstract class BaseItem implements ItemInterface {
/**
* Additional item data.
*
* This contains data for fields that are not defined as class properties.
*
* Subclasses MUST NOT override this property with a different type.
*
* @var array
*/
protected array $data = [];
/**
* {@inheritdoc}
*/
public function get($field) {
return $this->$field ?? NULL;
// Special case: allow "data" as a user field. When a field called 'data'
// is requested, instead of returning the contents of the $data property,
// check if the $data property contains a field called 'data'. If so, return
// that. If not, return null.
if ($field === 'data') {
return $this->data['data'] ?? NULL;
}
// Check if the requested field is defined as a property on our object. If
// this is the case, return the value of the object property.
if (property_exists($this, $field)) {
return $this->$field;
}
// If the field is *not* defined as an object property, check if the field
// exists on the data property.
if (isset($this->data[$field])) {
return $this->data[$field];
}
// The requested field was not found directly on the object, nor on the data
// property. In that case, return null.
return NULL;
}
/**
* {@inheritdoc}
*/
public function set($field, $value) {
// Special case: allow "data" as a user field.
// Store it inside the internal $data array instead of overwriting the $data
// property.
if ($field === 'data') {
$this->data['data'] = $value;
return $this;
}
// Check if the requested field is defined as a property on our object. If
// this is the case, set the value directly on that object property.
if (property_exists($this, $field)) {
$this->$field = $value;
return $this;
}
// If the field is *not* defined as an object property, set it on the data
// property.
$this->data[$field] = $value;
return $this;
}
/**
* {@inheritdoc}
*/
public function toArray() {
return get_object_vars($this);
$vars = [];
// Create an array of all object properties *except* $data.
foreach (get_object_vars($this) as $key => $value) {
if ($key !== 'data') {
$vars[$key] = $value;
}
}
// Merge with the internal $data array (which may also include a field
// called 'data').
return $vars + $this->data;
}
/**
......
......@@ -26,6 +26,13 @@ class BaseItemTest extends ItemTestBase {
$this->assertNull($this->item->get('non_existent'));
}
/**
* @covers ::get
*/
public function testGetDataFieldWhenNotSet() {
$this->assertNull($this->item->get('data'));
}
/**
* @covers ::set
* @covers ::get
......@@ -35,6 +42,14 @@ class BaseItemTest extends ItemTestBase {
$this->assertSame('bar', $this->item->get('foo'));
}
/**
* @covers ::set
*/
public function testSetDataField() {
$this->item->set('data', 'bar');
$this->assertSame($this->item, $this->item->set('data', 'bar'));
}
/**
* @covers ::toArray
*/
......@@ -43,12 +58,46 @@ class BaseItemTest extends ItemTestBase {
$this->item->set('baz', 'qux');
$expected = [
'title' => NULL,
'foo' => 'bar',
'baz' => 'qux',
];
$this->assertSame($expected, $this->item->toArray());
}
/**
* @covers ::toArray
*/
public function testToArrayWithTitleSet() {
$this->item->set('foo', 'bar');
$this->item->set('baz', 'qux');
$this->item->set('title', 'Lorem ipsum');
$expected = [
'title' => 'Lorem ipsum',
'foo' => 'bar',
'baz' => 'qux',
];
$this->assertSame($expected, $this->item->toArray());
}
/**
* @covers ::toArray
*/
public function testToArrayWithDataField() {
$this->item->set('foo', 'bar');
$this->item->set('baz', 'qux');
$this->item->set('data', 'bar');
$expected = [
'title' => NULL,
'foo' => 'bar',
'baz' => 'qux',
'data' => 'bar',
];
$this->assertSame($expected, $this->item->toArray());
}
/**
* @covers ::fromArray
*/
......@@ -62,6 +111,48 @@ class BaseItemTest extends ItemTestBase {
$this->assertSame('ipsum', $this->item->get('lorem'));
}
/**
* @covers ::fromArray
*/
public function testFromArrayWithTitle() {
$data = [
'bar' => 'foo',
'lorem' => 'ipsum',
'title' => 'Lorem Ipsum',
];
$this->assertSame($this->item, $this->item->fromArray($data));
$this->assertSame('foo', $this->item->get('bar'));
$this->assertSame('ipsum', $this->item->get('lorem'));
$this->assertSame('Lorem Ipsum', $this->item->get('title'));
}
/**
* @covers ::fromArray
*/
public function testFromWithDataField() {
$data = [
'bar' => 'foo',
'lorem' => 'ipsum',
'data' => ['some' => 'thing'],
];
$this->assertSame($this->item, $this->item->fromArray($data));
$this->assertSame('foo', $this->item->get('bar'));
$this->assertSame('ipsum', $this->item->get('lorem'));
$this->assertSame(['some' => 'thing'], $this->item->get('data'));
}
/**
* @covers ::fromArray
*/
public function testFromArrayWithEmptyArray() {
$this->assertSame($this->item, $this->item->fromArray([]));
$expected = [
'title' => NULL,
];
$this->assertSame($expected, $this->item->toArray());
}
}
/**
......@@ -69,4 +160,13 @@ class BaseItemTest extends ItemTestBase {
*
* Abstract classes cannot be mocked.
*/
class ItemMock extends BaseItem {}
class ItemMock extends BaseItem {
/**
* The title of the item.
*
* @var string
*/
protected $title;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment