Download the PHP package brandonshar/presenters without Composer

On this page you can find all versions of the php package brandonshar/presenters. 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 presenters

Presenters

WARNING While this is tested, I have not used it in a production or extensively in a development environment yet. I'm pretty sure I'm going to love using it, but until I do that I feel obligated to offer a warning about its usefulness.

Installation

Install via composer and just start using it. Couldn't be easier!

So what is this?

Have you ever had a model, or combination of models, that you wanted to return with different data depending on the endpoint? Maybe you have a resource controller called VehicleOnCraigslistController and when someone hits your VehicleOnCraigslistController@show method, you want to return a set of data that represents a vehicle listed on Craigslist. This set of data is likely comprised of your Vehicle model and your CraigslistAd model, but different than what you return from your VehicleController@show method, your CraigslistAdController@show, or just a combination of the two. This is where Presenters come in to save the day!

How do I do those great sounding things?

Presenters have a number of great features. Let's see them all in action!

Basic Presenter Example

There's the minimum code for a presenter! (present is just some nice syntactic sugar that provides a more fluent way to instantiate your presenter. If it makes you nervous, you can always new it up like usual).

Ok... I can see that you're not very impressed yet. Let's do things with it!

Delegation

Delegation allow you to reach through your presenter to the models (or other objects) it contains while avoiding much of the boilerplate of typical getters.

Now we can "reach through" the presenter to access the delegates properties!

Ok, a little cooler, right? What if we need something more complicated?

Getters (Accessors)

The presenter has getters that work just like Laravel's Eloquent getters: just create a method called getPropertyNameAttribute (if your property is called "propertyName") that optionally accepts a single argument (the currently set value of the named property) :

We can now access this like any other property:

Both camelCase and snake_case work the same. If you put information into your presenter manually, it will automatically be passed as an argument to your getter.

Note: Wish you had a better way to put data on your presenter? Just hang tight for the tap method below!

getAttribute

If you need to access an attribute that isn't the attribute named in your getter, you should use the getAttribute method:

You could go directly to the attributes array property, but this requires you to make sure you match case with the json casting below (defaulting to snake_case). Because of this, the preferred way is to use the get (and corresponding set) attribute methods.

Setters (Mutators)

Technically, the presenters do have setters, but I haven't thought of a use case for them. They exist in order to aid the getters, but could be used standalone. I haven't thought of a real use case for them, but here's a quick example in case you do:

Now you can:

setAttribute

Just like getting above, the preferred way to set internal attributes is as follows:

Tap

I'm a sucker for fluid syntax and avoiding temporary variables, so if you want to add some instance variables to your presenter when it's instantiated, you can do so with the tap method:

Json

I try to exclusively develop API endpoints to be consumed by my front-end, so my use cases for this are always json. If you're like me, then you'll be happy to know that you have three great ways to get json out of your presenter: You can use the toJson method:

You can simply use the php function json_encode (presenters implement JsonSerializable)

OR if you use Laravel (and if you don't, you should strongly reconsider), you can just directly return presenters from any controller route and they'll be converted to json automatically! This is where the code really starts to get sexy:

Wait, what is converted to JSON?

Presenters automatically turn all of their delegates, any getters, and any variables that were manually set (through tap or otherwise) as JSON. Our json from the presenter we've been building will look something like this:

But hang on, I prefer my JSON to be camelCase? Is there any hope for me?

Oh come on, how on earth would we manage to make something like.. OH WAIT ONE SECOND

Happy? Valid options are camel, snake, or (if you're an irredeemable monster), studly

That's all for now! I hope to update as I begin using this in my projects more and would be irrationally thrilled to receive a pull request if you think you can improve it.


All versions of presenters with dependencies

PHP Build Version
Package Version
Requires php Version >=7
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 brandonshar/presenters contains the following files

Loading the files please wait ....