PHP code example of happycode / blueprint
1. Go to this page and download the library: Download happycode/blueprint library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
happycode / blueprint example snippets
$userSchema = Model::Define('User', [
"fieldName" => 'string',
])
$userSchema = Model::Define('User', [
"fieldName" => Type::String(),
])
$userSchema = Model::Define('User', [
"fieldName" => Type::String(isNullable: true, isRequired: false, isHidden: false),
])
$userSchema = Model::Define('User', [
"status" => Type::Enum(values: ["PENDING", "ACTIVE", "DISABLED"]),
])
$userSchema = Model::Define('User', [
"status" => Type::DateTime(inputFormat: 'd-m-Y', outputFormat: 'm/d/Y'),
])
$userSchema = Model::Define('User', [
"lotteryNumbers" => Type::ArrayOf(Type::Int()),
])
// or with shorthand
$userSchema = Model::Define('User', [
"lotteryNumbers" => 'int[]'
])
$mapPinSchema = Model::Define('MapPin', [
"geoLocation" => Type::Model(
Model::Define('GeoLocation', [
"lat" => Type::String(),
"long" => Type::String()
])
),
])
$geoLocationSchema = Model::Define('GeoLocation', [
"lat" => Type::String(),
"long" => Type::String()
]);
$deliverySchema = Model::Define('Delivery', [
"pickupLocation" => Type::Model($geoLocationSchema),
"dropLocation" => Type::Model($geoLocationSchema),
])
$geoLocationSchema = Model::Define('GeoLocation', [
"lat" => Type::String(),
"long" => Type::String()
]);
$deliverySchema = Model::Define('Delivery', [
"journeyTracking" => Type::Collection($geoLocationSchema),
])
$rabbitSchema = Model::Define('Rabbit', [
"name" => Type::String()
]);
$loadsOfRabbitsSchema = Model::CollectionOf($rabbitSchema)
$rabbitSchema = Model::Define('Rabbit', [
"first" => Type::String(isHidden: true),
"last" => Type::String(isHidden: true),
"fullName" => Type::Virtual(function($rabbit) {
return $rabbit['first'] . ' ' . $rabbit['last'];
}),
]);
$schema = Model::Define('Thing', [
"name" => Type::String(),
"weight" => Type::Int(),
"lotteryNumbers" => Type::ArrayOf(Type::Int())
]);
// is equivalent to
$schema = Model::Thing([
"name" => 'string', // or 'text'
"rich" => 'bool', // or 'boolean'
"weight" => 'float', // 'double' or 'decimal' also work
"lotteryNumbers" => 'int[]' // works for all primitive types
]);
$model = $schema->adapt($jsonString);
$user = (Model::Define('User', [ 'name' => 'string' ])->adapt('{ "name": "Roger" }'));
echo $user->getName(); // Roger
$user = (Model::Define('User', ['name' => 'string']))->adapt('{ "name": "Roger" }');
echo $user->json(); // { "name": "Roger" }
$spy = (Model::Define('Spy', [
"first" => Type::String(isHidden: true),
"last" => Type::String(isHidden: true),
"fullName" => Type::Virtual(function($who) {
return sprintf("%s, %s %s!", $who['last'], $who['first'], $who['last']);
}),
])->adapt('{ "first": "James", "last": "Bond" }'));
echo $spy->json(); // A string = {"fullName":"Bond, James Bond!"}
try {
$user = $userSchema->adapt('{ "name": "Roger" }');
} catch (\HappyCode\Blueprint\Error\BlueprintError $e) {
// handle this
}
try {
$user = $userSchema->adapt('{ "name": "Roger" }');
} catch (\HappyCode\Blueprint\Error\BuildError $e) {
// Type Help code generator errors
} catch (\HappyCode\Blueprint\Error\ValidationError $e) {
// Validation based issues while parsing the data
// $e->getMessage() - will be helpful
}
$userSchema = Model::Define('User', [
"name" => 'string',
])->setHelperNamespace("App\TypeHintThing");
$user = $userSchema->adapt('{ "name": "Roger" }');
/** @var \App\TypeHintThing\UserModel $user */
$user = $userSchema->adapt('{ "name": "Roger" }');
php
$user = $userSchema->adapt($json);
php
echo $user->getFirstName();
- /<project_root>
- /src
- index.php
- /lib
- helper.php
- composer.json
- /<project_root>
- /src
- index.php
- /TypeHintThing
- ...?
- UserModel.php
- /lib
- helper.php
- composer.json