Download the PHP package jasny/entity without Composer

On this page you can find all versions of the php package jasny/entity. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package entity

Jasny Entity

Build Status Scrutinizer Code Quality Code Coverage Packagist Stable Version Packagist License

An entity is a "thing" you want to represent in a database or other data stores. It can be a new article on your blog, a user in your message board or a permission in your rights management system.

The properties of an entity object is a representation of persisted data.

Installation

composer require jasny/entity

Usage

A quick and dirty script to create and output the JSON of a User entity could be

In this example you could just as well json serialize the data directly. The layer helps in adding abstraction to applications that are beyond simple scripts.

Documentation

Basic entity

The Entity interface defines the methods all entities need to implement. To implement an entity you may extend the AbstractBasicEntity base class. Alternatively you can use the traits of this library.

It's recommended to define properties as public, however only use them to get values and not to set. For setting values use the set() method. This isn't enforced at runtime, but may be checked by a static code analyser like PHPStan.

Identifiable entity

If an entity has a unique identifier, the class should implement IdentifiableEntity.

It's assumed you're using property id as a surrogate key. If you're using a differently property, make sure to overwrite the static getIdProperty() method.

Dynamic entity

By default entities should only have the properties that are specified by the class. The set() method will ignore all values that don't correspond with any property. If for some reason additional properties are added, the toAssoc() and jsonSerialize() methods, also ignore properties that aren't defined in the class.

In some cases an entity might be dynamic; it can have properties that are added at runtime. Some data stores like MongoDB have dynamic schemas, which don't have to be defined at forehand, to support this.

To indicate that an entity may have dynamic properties it should implement the DynamicEntity interface.

New entity

Using the new keyword is reserved for creating a new entity.

If you set the identified (id property) of a new entity, it will either overwrite it or throw an duplicate id error, depending on the data storage implementation. However it will (or rather should) not update the existing record.

The isNew() method will tell if it's a new user or if it's loaded from data.

Existing entity

When the data of an entity is fetched, the static fromData() method is used to create the entity.

The fromData() method sets the properties of the entity object, before calling the constructor.

var_export

The __set_state() method is set as alias of fromData(), allowing entities to be serialized via var_export and stored as PHP script. Other libraries like Jasny Typecast, rely on the __set_state() method as well.

Set values

The set() method is a a helper function for setting all the properties from an array.

It can be use as fluent interface.

The set() method triggers 2 events; AfterSet.

Same entities

Check if two entities are the same using the is() method which returns a boolean. The method always returns true in case the objects are the same object (similar to ===).

For identifiable objects, the method will also return true is the entity class and the id value are the same. The value of other properties are disregarded.

Cast to associative array

Cast an entity to an associative array with the toAssoc() method. By default this method will return the values of all public properties.

The ToAssoc event is available to modify the result of this method. The library comes with the ToAssocRecursive event listener, which will also turn child entities into associative arrays.

The toAssoc() method can be used in farious places. It's not recommended to create event listeners to handle a specific use case. Instead create a new type of event for that specific use.

Cast to JSON

Entities must implement JsonSerializable, meaning they can be casted to JSON via json_encode(). By default, the result is an object with all the public properties of the entity.

The jsonSerialize method can be overwritten in the entity class. Alternatively the ToJson event can be used to modify the result before it's serialized to a json string.

The library contains the JsonCast event listener that will convert DateTime objects to a date string and will convert any (child) object that implements JsonSerializable.

Persisting entities

This library does not have any methods for saving entities into persistent storage (like a database).

It recommended to implement data gateway services for this (and not adopt Active Record pattern).

The example always does a REPLACE query, but you could do an UPDATE query if isNew() returns false instead.

After an entity is saved, the gateway should call the markAsPersisted method, which will trigger an event and mark the entity as no longer being new (for isNew()).

If you're using a auto-generated identifier, you should retrieve it from the db layer and directly set the id property prior to calling marktAsPersisted().

Events

Entities may support events through a PSR-14 compatible event dispatcher. This allows additional abstraction for different services and is important when

Before you can add event listener, you need to register an event dispatcher. The entity doesn't create one itself.

Typically the event dispatcher is added to an entity by the gateway. This means that the gateway should also be used when creating a new entity.

To add an event listener to an existing entity use the addEventListener() method of the entity.

Note that since adding event listeners isn't defined by the PSR-14 standard, the addEventListener() method only works with Jasny Event Dispatcher.

The dispatchEvent() method takes an event and dispatches it to the listeners. It will return the passed event object, which may be modified by the event listeners.

Event objects

An event can be any object. The event lister are filtered on the object class.

Event classes of this library take the $entity and $payload as constructor arguments. The getEntity() method will return the emitting entity. You can get the payload using getPayload() and update it with setPayload(). The modified event is passed to subsequent listeners and used by the method triggering the event.

Entity events

The library has the following events

Event listeners


All versions of entity with dependencies

PHP Build Version
Package Version
Requires php Version >=7.2.0
improved/improved Version ^1.0
jasny/php-functions Version ^4.1
psr/event-dispatcher Version ^1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package jasny/entity contains the following files

Loading the files please wait ....