Add array access semantics to ActivityPubObjects

This commit is contained in:
Jeremy Dormitzer 2018-12-12 11:04:19 -05:00
parent 590ad71081
commit bc06810432
2 changed files with 86 additions and 1 deletions

View File

@ -1,6 +1,8 @@
<?php
namespace ActivityPub\Entities;
use ArrayAccess;
use BadMethodCallException;
use DateTime;
use ActivityPub\Utils\Util;
use Doctrine\Common\Collections\ArrayCollection;
@ -9,7 +11,7 @@ use Doctrine\Common\Collections\ArrayCollection;
* Represents an ActivityPub JSON-LD object
* @Entity @Table(name="objects")
*/
class ActivityPubObject
class ActivityPubObject implements ArrayAccess
{
/**
* @var int
@ -162,6 +164,25 @@ class ActivityPubObject
return false;
}
/**
* Returns the value of the field with key $name
*
* The value is either a string, another ActivityPubObject, or null
* if no such key exists.
*
* @return string|ActivityPubObject|null The field's value, or null if
* the field is not found
*/
public function getFieldValue( string $name )
{
foreach( $this->getFields() as $field ) {
if ( $field->getName() === $name ) {
return $field->getValueOrTargetObject();
}
}
return null;
}
/**
* Adds a new field on the object
*
@ -232,5 +253,29 @@ class ActivityPubObject
{
$this->lastUpdated = $lastUpdated;
}
public function offsetExists( $offset )
{
return $this->hasField( $offset );
}
public function offsetGet( $offset )
{
return $this->getFieldValue( $offset );
}
public function offsetSet( $offset, $value )
{
throw new BadMethodCallException(
'ActivityPubObject fields cannot be directly set'
);
}
public function offsetUnset( $offset )
{
throw new BadMethodCallException(
'ActivityPubObject fields cannot be directly unset'
);
}
}
?>

View File

@ -2,6 +2,7 @@
namespace ActivityPub\Test;
use DateTime;
use BadMethodCallException;
use ActivityPub\Test\Config\SQLiteTestCase;
use ActivityPub\Test\Config\ArrayDataSet;
use ActivityPub\Entities\ActivityPubObject;
@ -1094,5 +1095,44 @@ class ObjectsServiceTest extends SQLiteTestCase
$this->assertTablesEqual( $expectedObjectsTable, $objectsQueryTable );
$this->assertTablesEqual( $expectedFieldsTable, $fieldsQueryTable );
}
public function testObjectArrayAccess()
{
$fields = array(
'id' => 'https://example.com/notes/1',
'type' => 'Note',
'content' => 'This is a note',
);
$now = $this->getTime( 'create' );
$object = $this->objectsService->createObject( $fields );
$this->assertEquals( $object['content'], 'This is a note' );
$this->assertNull( $object['attributedTo'] );
}
public function testItThrowsTryingToSetObjectFieldLikeArray()
{
$fields = array(
'id' => 'https://example.com/notes/1',
'type' => 'Note',
'content' => 'This is a note',
);
$now = $this->getTime( 'create' );
$object = $this->objectsService->createObject( $fields );
$this->expectException( BadMethodCallException::class );
$object['content'] = 'This should break';
}
public function testItThrowsTryingToUnsetObjectFieldLikeArray()
{
$fields = array(
'id' => 'https://example.com/notes/1',
'type' => 'Note',
'content' => 'This is a note',
);
$now = $this->getTime( 'create' );
$object = $this->objectsService->createObject( $fields );
$this->expectException( BadMethodCallException::class );
unset( $object['content'] );
}
}
?>