Download the PHP package happycode/blueprint without Composer

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

Blueprint

A PHP Library for Defining, validating, reading and transforming 3rd Party data.

Problem

[disclaimer] - This is JSON only for now. Everything else is coming soon!

Ok, so it's a common thing. Your app talks to an API somewhere.

You get some JSON back from a 3rd party request, it seems to have everything you need in it (somewhere) but now you have to work out what to do with it.

You have 2 options:

oh no, no, no, NO!!

Blueprint

Tldr;

Blueprint allows us to DEFINE the data we're expecting, like a table in a DB. We tell it the SCHEMA (the shape and types) of the JSON, We tell it how to validate it, our rules not theirs, we filter out the stuff we won't want, and how to change the bits we won't like.

Then we give it the data.

Validation is implicit, with detailed errors on what went wrong.

It gives us back smart Objects, Safe data, Structured and Formatted the way our apps need it.

Lets have a look...

Install

Usage

let's say we get this from a 3rd party API - it's in a variable called $json

Let's try to define it.

Now we can add some data

by the way - that was where the validation happened! and so now...

will give you what you might imagine.

nice! - lets go deeper.

More Help

Non-Primitives Types Meta-data Collections
Virtual Fields Enum DateTime Shorthand
IDE help ArrayOf Exceptions

Types

when we specify a field...

its actually shorthand for

the Type Class gives us access to make more complex configuration.

The available Primitive types are

Meta-Data

All Types have some associated meta-data, specifically the following booleans

The default values are ( x denotes a property that cannot be set )

isNullable isRequired isHidden
String false true false
Boolean x (false) true false
Float false true false
Int false true false
DateTime false true false
Enum x (false) true false
ArrayOf false true false
Collection false true false

Enum

Enumerated values - will only accept values that match the specified set example

DateTime

Dates and times are a common use case for inline transformations and so have we're able to create PHP date format specifications for reading and rendering. example

the defaults are

ArrayOf

Suppose we're looking at this kind of json

Because it can be described as 'an array of primitive types' we can model it like this...

Non-Primitives (Custom Objects)

Yep nested structures can be typed too... Here's an example

We can create a custom (sub) Model on the fly

although you could always make it more reusable should the schema need to repeat the object

like...

Collections

ok - lets combine Custom Objects and Arrays - in Blueprint a set of Custom Objects (Model Schemas) is called a Collection

Using the $geoLocationSchema from the custom object example (above)

allows for json like

RootCollections

Sometimes json will have a root level array instead of an Object - this is valid, annoying and quite common.

easily done...

Virtual Fields (Transformations)

Suppose you want a field that isn't there, and you can construct it from the data you already have. for example:

Don't you just wish you had a fullName field in there? well...

The function passed to the Type::Virtual() method will have an assoc array with all the decoded properties (including hidden) and values from the input json.

The only requirement is that any fields being used are present in the schema (obviously).

By the way, this is why we might want to hide fields using isHidden - when we get to rendering the hydrated models, we may not want them visible.

Shorthand

As long as you aren't messing with the default values, Primitive types all have shorthand notation, in case you find that more readable.

Adapting (Hydrating)

Hoo wee Tiger!!! - You have a schema! Nice work!

Lets

  1. The first thing that happens here is Validation - Blueprint knows what it needs from the json so it makes sure it's there.
  2. The second thing is Filtering - If there's data in the Json, but your schema doesn't define it anywhere, it's disregarded.
  3. and the last thing that happens is it returns a model representing your data the way you need it,

watch this...

Rendering

now this

Well that's pointless right?? - or is it?

notice how the rendered json does not include the hidden fields first and last

Exceptions

During run-time blueprint will throw various Exceptions, In all cases they will extend the HappyCode\Blueprint\Error\BlueprintError Exception

Wrapping the adapt method like the following is guaranteed to encapsulate all cases:

for more fine grain control

IDE's and Type Help

When blueprint has parsed the input json ($schema->adapt($json))) - you may notice a problem with the model.

Your IDE (PHPStorm / Visual Studio / Whatever) - is unable to provide any intellisense, this IS a problem! Due to the fact that the data models are created at runtime, your editor is unable to perform any static analysis on them. This means you'll probably get some squiggly lines when you write perfectly legit code like $model->getId().

To fix this we have a solution - It's not necessary but we all like a squiggle free file right!

On your Schema definition add ->setHelperNamespace(<NS>) and provide a namespace for some TypeHelpers. on the first run the adapter will generate a file for each model in your project.

The namespace you provide will map to a file path located in your project directory

If your composer.json file has any psr-4 mappings - it will obey these - otherwise the mapping will be from the project root. (Where the composer file lives)

Example:

let's say you composer.json autoload settings look like.

in your code

After the first time you run this - you'll notice one or more new files in your project

you can use this in your code to tell your IDE about the model Types.

Important Note:

Once these files have been generated they will not be recreated - even if you update the schema, yuo should delete the helper files and allow them to be regenerated.

Copyright and License

The happycode/blueprint library is copyright © Paul Rooney and licensed for use under the terms of the MIT License (MIT). Please see LICENSE for more information.


All versions of blueprint with dependencies

PHP Build Version
Package Version
No informations.
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 happycode/blueprint contains the following files

Loading the files please wait ....