PHP code example of pre / prop-types
1. Go to this page and download the library: Download pre/prop-types 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/ */
pre / prop-types example snippets
use App\Profile;
use Pre\PropTypes;
$profile = Profile::find(1);
$definitions = [
"name" => PropTypes::string()->isRequired(),
"age" => PropTypes::int(),
"rating" => PropTypes::float(),
"permissions" => PropTypes::arrayOf(PropTypes::string()),
"thumbnail" => PropTypes::either(
PropTypes::string(), // uri
PropTypes::resource(), // file handle
),
"profile" => PropTypes::objectOf(Profile::class)->isRequired(),
"onMessage" => PropTypes::closure()->isRequired(),
"isAdmin" => PropTypes::bool()->isRequired(),
];
$properties = [
"name" => "Joe",
"profile" => $profile,
"onMessage" => function($message) use ($profile) {
$profile->notify($message);
},
"isAdmin" => false,
];
try {
PropTypes::validate($definitions, $properties);
} catch (InvalidArgumentException $e) {
// ...handle the error
}
use Pre\PropTypes;
use function Pre\Phpx\Html\render as renderHTML;
function render($type, $props = [])
{
$props = (array) $props;
if (class_exists($type)) {
if (method_exists($type, "propTypes")) {
PropTypes::validate($type::propTypes(), $props);
}
if (method_exists($type, "defaultProps")) {
$props = array_merge($type::defaultProps(), $props);
}
}
return renderHTML($type, $props);
}
render("App\\Custom\\Component");