locomotivemtl / charcoal-object
Object (Content and UserData) definition and object behaviors and tools.
Installs: 14 841
Dependents: 7
Suggesters: 0
Security: 0
Stars: 0
Watchers: 13
Forks: 0
Open Issues: 0
Requires
- php: ^7.3 || ^8.0
- locomotivemtl/charcoal-config: ~0.10
- locomotivemtl/charcoal-core: ~0.5
- locomotivemtl/charcoal-factory: ~0.4
- locomotivemtl/charcoal-translator: ~0.3
- psr/log: ^1.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^9.0
- pimple/pimple: ^3.0
- seld/jsonlint: ^1.8
- squizlabs/php_codesniffer: ^3.5
- tedivm/stash: ~0.16
- dev-master / 0.8.x-dev
- 0.8.1
- 0.8.0.1
- 0.8.0
- 0.7.4.1
- 0.7.4
- 0.7.3.1
- 0.7.3
- 0.7.2
- 0.7.1.5
- 0.7.1.4
- 0.7.1.3
- 0.7.1.2
- 0.7.1.1
- 0.7.1
- 0.7.0
- 0.6.2.1
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.1
- 0.5.0
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4
- 0.3.0.1
- 0.3
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1.1
- 0.2.1
- 0.2
- 0.1.3.1
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1
- dev-feature/camelizePropertiesAndUseArrayAccess
- dev-mducharme-camelcase
- dev-mcaskill-feature-softdeletable
This package is auto-updated.
Last update: 2024-10-30 01:31:50 UTC
README
Object definition (Content and UserData), behaviors and tools.
Table of content
How to install
The preferred (and only supported) way of installing charcoal-object is with composer:
★ composer require locomotivemtl/charcoal-object
Dependencies
- PHP 5.6+
- This is the last supported version of PHP.
PHP 7
is also supported (meaning green on travis…).
The Charcoal Object
The \Charcoal\Object
namespace provides a bunch of basic classes, helpers as well as object behaviors (interfaces + traits).
Basic classes
All charcoal project object classes should extend one of the 2 base classes, \Charcoal\Object\Content
, for data created and managed by administrators or \Charcoal\Object\UserData
, for data created from clients / users.
Content
The Content base class should be used for all objects which can be "managed". Typically by an administrator, via the charcoal-admin
module. It adds the "active" flag to objects as well as creation and modification informations.
API
setActive($active)
active()
setPosition($position)
position()
setCreated($created)
created()
setCreatedBy($createdBy)
createdBy()
setLastModified($lastModified)
lastModified()
setLastModifiedBy($lastModifiedBy)
lastModifiedBy()
The
Content
class extends\Charcoal\Model\AbstractModel
from thecharcoal-core
module, which means that it also inherits its API as well as theDescribableInterface
(metadata()
,setMetadata()
andloadMetadata()
, amongst others) and theStorableInterface
(id()
,key()
,save()
,update()
,delete()
,load()
,loadFrom()
,loadFromQuery()
,source()
andsetSource()
, amongst others).The
AbstractModel
class extends\Charcoal\Config\AbstractEntity
which also defines basic data-access methods (setData()
,data()
,keys()
,has()
,get()
,set()
, plus theArrayAccess
,JsonSerializable
andSerializable
interfaces).
Properties (metadata)
[1] Auto-generated upon save()
[2] Auto-generated upon update()
Default metadata is defined in
metadata/charcoal/object/content.json
UserData
The UserData class should be used for all objects that are expected to be entered from the project's "client" or "end user".
API
setIp($ip)
ip()
setTs($ts)
ts()
setLang($lang)
lang()
The
Content
class extends\Charcoal\Model\AbstractModel
from thecharcoal-core
module, which means that it also inherits its API as well as theDescribableInterface
(metadata()
,setMetadata()
andloadMetadata()
, amongst others) and theStorableInterface
(id()
,key()
,save()
,update()
,delete()
,load()
,loadFrom()
,loadFromQuery()
,source()
andsetSource()
, amongst others).The
AbstractModel
class extends\Charcoal\Config\AbstractEntity
which also defines basic data-access methods (setData()
,data()
,keys()
,has()
,get()
,set()
, plus theArrayAccess
,JsonSerializable
andSerializable
interfaces).
Properties (metadata)
[1] Auto-generated upon save()
and update()
Default metadata is defined in
metadata/charcoal/object/user-data.json
Object behaviors
Archivable
The archivable behavior is not yet documented. It is still under heavy development.
Categorizable
API
setCategory($category)
category()
setCategoryType($type)
categoryType()
Properties (metadata)
[1] The category obj_type
must be explicitely set in implementation's metadata.
Default metadata is defined in
metadata/charcoal/object/catgorizable-interface.json
Category
API
setCategoryItemType($type)
categoryItemType()
numCategoryItems()
hasCategoryItems()
categoryItems()
Properties (metadata)
Default metadata is defined in
metadata/charcoal/object/catgory-interface.json
Hierarchical
API
hasMaster()
isTopLevel()
isLastLevel()
hierarchyLevel()
master()
toplevelMaster()
hierarchy()
invertedHierarchy()
isMasterOf($child)
recursiveIsMasterOf($child)
hasChildren()
numChildren()
recursiveNumChildren()
children()
isChildOf($master)
recursiveIsChildOf($master)
hasSiblings()
numSiblings()
siblings()
isSiblingOf($sibling)
Properties (metadata)
Default metadata is defined in
metadata/charcoal/object/hierarchical-interface.json
.
Publishable
setPublishDate($publishDate)
publishDate()
setExpiryDate($expiryDate)
expiryDate()
setPublishStatus($status)
publishStatus()
isPublished()
Properties (metadata)
Default metadata is defined in
metadata/charcoal/object/publishable-interface.json
.
Revisionable
Revisionable objects implement \Charcoal\Object\Revision\RevisionableInterface
, which can be easily implemented by using \Charcoal\Object\Revision\RevisionableTrait
.
Revisionable objects create revisions which logs the changes between an object's versions, as diffs.
API
setRevisionEnabled(bool$enabled)
revisionEnabled()
revisionObject()
generateRevision()
latestRevision()
revisionNum(integer $revNum)
allRevisions(callable $callback = null)
revertToRevision(integer $revNum)
Properties (metadata)
The revisionable behavior does not implement any properties as all logic & data is self-contained in the revisions.
Routable
The routable behavior is not yet documented. It is still under heavy development.
Helpers
ObjectDraft
…
ObjectRevision
Upon every update
in storage, a revisionable object creates a new revision (a \Charcoal\Object\ObjectRevision
instance) which holds logs the changes (diff) between versions of an object:
Revision properties
Revision methods
createFromObject(RevisionableInterface $obj)
createDiff(array $dataPrev, array $dataObj)
lastObjectRevision(RevisionableInterface $obj)
objectRevisionNum(RevisionableInterface $obj, integer $revNum)
ObjetSchedule
It is possible, (typically from the charcoal admin backend), to create schedule (a \Charcaol\Object\ObjectSchedule
instance) which associate a set of changes to be applied automatically to an object:
Schedule properties
Schedule methods (API)
process([callable $callback, callable $successCallback,callable $failureCallback])
Scheduled actions should be run with a timely cron job. The charcoal-admin module contains a script to run schedules automatically:
★ ./vendor/bin/charcoal admin/object/process-schedules`
Development
To install the development environment:
★ composer install --prefer-source
To run the scripts (phplint, phpcs and phpunit):
★ composer test
API documentation
- The auto-generated
phpDocumentor
API documentation is available at https://locomotivemtl.github.io/charcoal-object/docs/master/ - The auto-generated
apigen
API documentation is available at https://codedoc.pub/locomotivemtl/charcoal-object/master/
Development dependencies
phpunit/phpunit
squizlabs/php_codesniffer
satooshi/php-coveralls
Continuous Integration
Coding Style
The charcoal-object module follows the Charcoal coding-style:
- PSR-1
- PSR-2
- PSR-4, autoloading is therefore provided by Composer.
- phpDocumentor comments.
- Read the phpcs.xml file for all the details on code style.
Coding style validation / enforcement can be performed with
composer phpcs
. An auto-fixer is also available withcomposer phpcbf
.
Authors
- Mathieu Ducharme, mat@locomotive.ca
- Chauncey McAskill
- Locomotive Inc.
License
Charcoal is licensed under the MIT license. See LICENSE for details.