Add created and last updated timestamps to objects and fields
This commit is contained in:
parent
c23604206c
commit
4c24dd3806
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace ActivityPub\Entities;
|
||||
|
||||
use DateTime;
|
||||
use ActivityPub\Utils\Util;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
|
||||
@ -32,9 +33,25 @@ class ActivityPubObject
|
||||
*/
|
||||
protected $referencingFields;
|
||||
|
||||
/**
|
||||
* The object's creation timestamp
|
||||
* @Column(type="datetime")
|
||||
* @var DateTime
|
||||
*/
|
||||
protected $created;
|
||||
|
||||
/**
|
||||
* The object's last update timestamp
|
||||
* @Column(type="datetime")
|
||||
* @var DateTime
|
||||
*/
|
||||
protected $lastUpdated;
|
||||
|
||||
public function __construct() {
|
||||
$this->fields = new ArrayCollection();
|
||||
$this->referencingFields = new ArrayCollection();
|
||||
$this->created = new DateTime("now");
|
||||
$this->lastUpdated = new DateTime("now");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -104,11 +121,52 @@ class ActivityPubObject
|
||||
return $this->referencingFields;
|
||||
}
|
||||
|
||||
public function addField(Field $field) {
|
||||
$this->fields[] = $field;
|
||||
/**
|
||||
* Returns the object's creation timestamp
|
||||
*
|
||||
* @return DateTime
|
||||
*/
|
||||
public function getCreated()
|
||||
{
|
||||
return $this->created;
|
||||
}
|
||||
|
||||
public function addReferencingField(Field $field) {
|
||||
/**
|
||||
* Returns the object's last updated timestamp
|
||||
*
|
||||
* @return DateTime
|
||||
*/
|
||||
public function getLastUpdated()
|
||||
{
|
||||
return $this->lastUpdated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new field on the object
|
||||
*
|
||||
* Don't call this directly; instead, use one of the
|
||||
* Field constructors and pass in this object as the
|
||||
* $object.
|
||||
*
|
||||
* @param Field $field
|
||||
*/
|
||||
public function addField(Field $field)
|
||||
{
|
||||
$this->fields[] = $field;
|
||||
$this->lastUpdated = new DateTime( "now" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new field that references this object
|
||||
*
|
||||
* Don't call this directly; instead, use one of the
|
||||
* Field constructors and pass in this object as the
|
||||
* $targetObject.
|
||||
*
|
||||
* @param Field $field
|
||||
*/
|
||||
public function addReferencingField(Field $field)
|
||||
{
|
||||
$this->referencingFields[] = $field;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
<?php
|
||||
namespace ActivityPub\Entities;
|
||||
|
||||
use DateTime;
|
||||
|
||||
/**
|
||||
* The field table hold the JSON-LD object graph.
|
||||
*
|
||||
@ -51,6 +53,20 @@ class Field
|
||||
*/
|
||||
protected $targetObject;
|
||||
|
||||
/**
|
||||
* The field's creation timestamp
|
||||
* @Column(type="datetime")
|
||||
* @var DateTime The creation timestamp
|
||||
*/
|
||||
protected $created;
|
||||
|
||||
/**
|
||||
* The field's last updated timestamp
|
||||
* @Column(type="datetime")
|
||||
* @var DateTime The last updated timestamp
|
||||
*/
|
||||
protected $lastUpdated;
|
||||
|
||||
/**
|
||||
* Create a new field with a string value
|
||||
*
|
||||
@ -59,11 +75,15 @@ class Field
|
||||
* @param string $value The value of the field
|
||||
* @return Field The new field
|
||||
*/
|
||||
public static function withValue(ActivityPubObject $object, string $name, string $value) {
|
||||
public static function withValue( ActivityPubObject $object, string $name, string $value )
|
||||
{
|
||||
$field = new Field();
|
||||
$field->setObject( $object );
|
||||
$field->setName( $name );
|
||||
$field->setValue( $value );
|
||||
$now = new DateTime( "now" );
|
||||
$field->setCreated( $now );
|
||||
$field->setLastUpdated( $now );
|
||||
return $field;
|
||||
}
|
||||
|
||||
@ -75,30 +95,52 @@ class Field
|
||||
* @param ActivityPubObject $targetObject The object that this field holds
|
||||
* @return Field The new field
|
||||
*/
|
||||
public static function withObject(ActivityPubObject $object, string $name, Object $targetObject) {
|
||||
public static function withObject( ActivityPubObject $object, string $name, Object $targetObject )
|
||||
{
|
||||
$field = new Field();
|
||||
$field->setObject( $object);
|
||||
$field->setName( $name );
|
||||
$field->setTargetObject( $targetObject );
|
||||
$now = new DateTime( "now" );
|
||||
$field->setCreated( $now );
|
||||
$field->setLastUpdated( $now );
|
||||
return $field;
|
||||
}
|
||||
|
||||
protected function setObject(ActivityPubObject $object) {
|
||||
protected function setObject( ActivityPubObject $object )
|
||||
{
|
||||
$object->addField( $this );
|
||||
$this->object= $object;
|
||||
}
|
||||
|
||||
protected function setTargetObject(ActivityPubObject $targetObject) {
|
||||
protected function setTargetObject( ActivityPubObject $targetObject )
|
||||
{
|
||||
$this->value = null;
|
||||
$targetObject->addReferencingField( $this );
|
||||
$this->targetObject = $targetObject;
|
||||
$this->lastUpdated = new DateTime( "now" );
|
||||
}
|
||||
|
||||
protected function setName(string $name) {
|
||||
protected function setName( string $name )
|
||||
{
|
||||
$this->name= $name;
|
||||
}
|
||||
|
||||
protected function setValue(string $value) {
|
||||
public function setValue( string $value )
|
||||
{
|
||||
$this->targetObject = null;
|
||||
$this->value = $value;
|
||||
$this->lastUpdated = new DateTime( "now" );
|
||||
}
|
||||
|
||||
protected function setCreated( DateTime $timestamp )
|
||||
{
|
||||
$this->created = $timestamp;
|
||||
}
|
||||
|
||||
protected function setLastUpdated( DateTime $timestamp )
|
||||
{
|
||||
$this->lastUpdated = $timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -106,7 +148,8 @@ class Field
|
||||
*
|
||||
* @return ActivityPubObject
|
||||
*/
|
||||
public function getObject() {
|
||||
public function getObject()
|
||||
{
|
||||
return $this->object;
|
||||
}
|
||||
|
||||
@ -115,7 +158,8 @@ class Field
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName() {
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
@ -124,12 +168,33 @@ class Field
|
||||
*
|
||||
* @return string|ActivityPubObject
|
||||
*/
|
||||
public function getValueOrTargetObject() {
|
||||
public function getValueOrTargetObject()
|
||||
{
|
||||
if ( ! is_null( $this->targetObject) ) {
|
||||
return $this->targetObject;
|
||||
} else {
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the field's creation timestamp
|
||||
*
|
||||
* @return DateTime
|
||||
*/
|
||||
public function getCreated()
|
||||
{
|
||||
return $this->created;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the field's last updated timestamp
|
||||
*
|
||||
* @return DateTime
|
||||
*/
|
||||
public function getLastUpdated()
|
||||
{
|
||||
return $this->lastUpdated;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
@ -69,5 +69,20 @@ class ObjectsService
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries for an object with certain field values
|
||||
*
|
||||
* @param array $queryTerms An associative array where the keys are field
|
||||
* names and the values are the values to query for. The value for a key
|
||||
* can also be another associative array, which represents a field
|
||||
* containing a target object that matches the given nested query.
|
||||
*
|
||||
* @return ActivityPubObject[] The objects that match the query, if any
|
||||
*/
|
||||
public function query( $queryTerms )
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace ActivityPub\Test;
|
||||
|
||||
use DateTime;
|
||||
use ActivityPub\Test\Config\SQLiteTestCase;
|
||||
use ActivityPub\Test\Config\ArrayDataSet;
|
||||
use ActivityPub\Entities\Field;
|
||||
@ -37,6 +38,10 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
$this->objectsService = new ObjectsService( $this->entityManager );
|
||||
}
|
||||
|
||||
private static function getNow() {
|
||||
return date("Y-m-d H:i:s");
|
||||
}
|
||||
|
||||
public function testItCreatesObject()
|
||||
{
|
||||
$fields = array(
|
||||
@ -44,10 +49,11 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'type' => 'Note',
|
||||
'content' => 'This is a note',
|
||||
);
|
||||
$now = self::getNow();
|
||||
$object = $this->objectsService->createObject( $fields );
|
||||
$expected = new ArrayDataSet( array(
|
||||
'objects' => array(
|
||||
array( 'id' => 1 )
|
||||
array( 'id' => 1, 'created' => $now, 'lastUpdated' => $now )
|
||||
),
|
||||
'fields' => array(
|
||||
array(
|
||||
@ -55,6 +61,8 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 1,
|
||||
'name' => 'id',
|
||||
'value' => 'https://example.com/notes/1',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
array(
|
||||
@ -62,6 +70,8 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 1,
|
||||
'name' => 'type',
|
||||
'value' => 'Note',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
array(
|
||||
@ -69,6 +79,8 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 1,
|
||||
'name' => 'content',
|
||||
'value' => 'This is a note',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
),
|
||||
@ -125,11 +137,12 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'type' => 'Person',
|
||||
),
|
||||
);
|
||||
$now = self::getNow();
|
||||
$object = $this->objectsService->createObject( $fields );
|
||||
$expected = new ArrayDataSet( array(
|
||||
'objects' => array(
|
||||
array( 'id' => 1 ),
|
||||
array( 'id' => 2 ),
|
||||
array( 'id' => 1, 'created' => $now, 'lastUpdated' => $now ),
|
||||
array( 'id' => 2, 'created' => $now, 'lastUpdated' => $now ),
|
||||
),
|
||||
'fields' => array(
|
||||
array(
|
||||
@ -137,6 +150,8 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 1,
|
||||
'name' => 'id',
|
||||
'value' => 'https://example.com/notes/1',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
array(
|
||||
@ -144,6 +159,8 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 1,
|
||||
'name' => 'type',
|
||||
'value' => 'Note',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
array(
|
||||
@ -151,6 +168,8 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 1,
|
||||
'name' => 'content',
|
||||
'value' => 'This is a note',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
array(
|
||||
@ -158,6 +177,8 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 2,
|
||||
'name' => 'id',
|
||||
'value' => 'https://example.com/actors/1',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
array(
|
||||
@ -165,6 +186,8 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 2,
|
||||
'name' => 'type',
|
||||
'value' => 'Person',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
array(
|
||||
@ -172,6 +195,8 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 1,
|
||||
'name' => 'attributedTo',
|
||||
'value' => null,
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => 2,
|
||||
),
|
||||
),
|
||||
@ -213,10 +238,11 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
"https://example.com/notes/2",
|
||||
),
|
||||
);
|
||||
$now = self::getNow();
|
||||
$object = $this->objectsService->createObject( $fields );
|
||||
$expected = new ArrayDataSet( array(
|
||||
'objects' => array(
|
||||
array( 'id' => 1 ),
|
||||
array( 'id' => 1, 'created' => $now, 'lastUpdated' => $now ),
|
||||
),
|
||||
'fields' => array(
|
||||
array(
|
||||
@ -224,6 +250,8 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 1,
|
||||
'name' => 'id',
|
||||
'value' => 'https://example.com/collections/1',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
array(
|
||||
@ -231,6 +259,8 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 1,
|
||||
'name' => 'type',
|
||||
'value' => 'Collection',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
array(
|
||||
@ -238,6 +268,8 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 1,
|
||||
'name' => 'items',
|
||||
'value' => 'https://example.com/notes/1',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
array(
|
||||
@ -245,6 +277,8 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 1,
|
||||
'name' => 'items',
|
||||
'value' => 'https://example.com/notes/2',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
),
|
||||
@ -293,12 +327,13 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
),
|
||||
),
|
||||
);
|
||||
$now = self::getNow();
|
||||
$object = $this->objectsService->createObject( $fields );
|
||||
$expected = new ArrayDataSet( array(
|
||||
'objects' => array(
|
||||
array( 'id' => 1 ),
|
||||
array( 'id' => 2 ),
|
||||
array( 'id' => 3 ),
|
||||
array( 'id' => 1, 'created' => $now, 'lastUpdated' => $now ),
|
||||
array( 'id' => 2, 'created' => $now, 'lastUpdated' => $now ),
|
||||
array( 'id' => 3, 'created' => $now, 'lastUpdated' => $now ),
|
||||
),
|
||||
'fields' => array(
|
||||
array(
|
||||
@ -306,6 +341,8 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 1,
|
||||
'name' => 'id',
|
||||
'value' => 'https://example.com/collections/1',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
array(
|
||||
@ -313,6 +350,8 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 1,
|
||||
'name' => 'type',
|
||||
'value' => 'Collection',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
array(
|
||||
@ -320,6 +359,8 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 2,
|
||||
'name' => 'id',
|
||||
'value' => 'https://example.com/notes/1',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
array(
|
||||
@ -327,6 +368,8 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 2,
|
||||
'name' => 'type',
|
||||
'value' => 'Note',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
array(
|
||||
@ -334,6 +377,8 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 2,
|
||||
'name' => 'content',
|
||||
'value' => 'This is a note',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
array(
|
||||
@ -341,13 +386,17 @@ class ObjectsServiceTest extends SQLiteTestCase
|
||||
'object_id' => 1,
|
||||
'name' => 'items',
|
||||
'value' => null,
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => 2,
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'id' => 7,
|
||||
'object_id' => 3,
|
||||
'name' => 'id',
|
||||
'value' => 'https://example.com/notes/2',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
array(
|
||||
@ -355,6 +404,8 @@ array(
|
||||
'object_id' => 3,
|
||||
'name' => 'type',
|
||||
'value' => 'Note',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
array(
|
||||
@ -362,6 +413,8 @@ array(
|
||||
'object_id' => 3,
|
||||
'name' => 'content',
|
||||
'value' => 'This is another note',
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => null,
|
||||
),
|
||||
array(
|
||||
@ -369,6 +422,8 @@ array(
|
||||
'object_id' => 1,
|
||||
'name' => 'items',
|
||||
'value' => null,
|
||||
'created' => $now,
|
||||
'lastUpdated' => $now,
|
||||
'targetObject_id' => 3,
|
||||
),
|
||||
),
|
||||
@ -405,7 +460,6 @@ array(
|
||||
);
|
||||
$object = $this->objectsService->createObject( $fields );
|
||||
$arr = $object->asArray();
|
||||
xdebug_break();
|
||||
$this->assertEquals( $fields, $arr );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user