PHP code example of cfxmarkets / php-jsonapi-objects
1. Go to this page and download the library: Download cfxmarkets/php-jsonapi-objects 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/ */
cfxmarkets / php-jsonapi-objects example snippets
// Use user info for conditional logic, say on an admin panel
if (!$user->isAtLeast('manager')) {
throw new UnauthorizedAccessException("You must be at least a manager to view this page");
}
// User user info to prefill forms
$form = '<form method="POST">
<input type="email" name="email" value="'.$user->getEmail().'">
<input type="phone" name="phone" value="'.$user->getPhone().'">
<input type="text" name="name" value="'.$user->getFullName().'">';
if ($user->likes('muffins')) {
$form .= '
<h2>Your Muffins</h2>';
foreach ($user->getMuffins(['orderBy' => 'rating:desc']) as $muffin) {
$form .= '
<p>
<input type="number" name="muffins['.$muffin->getId().'][rating]" value="'.$muffin->getRating().'">
'.$muffin->getType().'
<button data-muffin="'.$muffin->getId().'">Remove</button>
</p>';
}
$form .= '
</form>';
echo $form;
// Update user data in response to form input
$user
->setName($_POST['name'])
->setEmail($_POST['email'])
->setPhone($_POST['phone'])
;
$muffinData = $_POST['muffins'];
$muffins = $myBlog->muffins->newCollection();
forach($muffinData as $id => $info) {
$muffins[] = $myBlog->muffins->get("id=$id")
->setRating($info['rating'])
->save()
;
}
$user
->setMuffins($muffins)
->save()
;
// Update user data from jsonapi input
$user
->updateFromData($jsonapiData)
->save();
class User extends \CFX\JsonApi\AbstractResource
{
protected $attributes = [
// default to "end-user" role
'roles' => 1,
//....
];
protected static function getValidRoles()
{
return [
1 => 'end-user',
2 => 'advanced-user',
4 => 'manager',
8 => 'site-admin',
16 => 'sys-admin',
];
}
public function getRoleInteger()
{
return $this->_getAttributeValue('roles');
}
public function getRoles()
{
$roles = [];
$userRoles = $this->getRoleInteger();
foreach (static::getValidRoles() as $roleInt => $role) {
if ($userRoles & $roleInt) {
$roles[] = $role;
}
}
return $roles;
}
public function setRoles($val)
{
$roles = 0;
$validRoles = static::getValidRoles();
$invalidRoles = [];
if (is_array($val)) {
foreach ($val as $role) {
$roleInt = array_search($role, $validRoles);
if ($roleInt === false) {
$invalidRoles[] = $role;
} else {
$roles += $roleInt;
}
}
} else {
// Implement logic for validating integer role here....
}
if (count($invalidRoles) > 0) {
$this->setError('roles', 'invalid', [
"title" => 'Invalid Roles Provided',
"detail" => "The following roles are invalid: `".implode("`, `", $invalidRoles)."`",
]);
} else {
$this->clearError('roles', 'invalid');
}
return $this->_setAttribute('roles', $roles);
}
public function isAtLeast($role)
{
$roleLevel = array_search($role, static::getValidRoles(), true);
if ($roleLevel === false) {
throw new \RuntimeException("Unrecognized role `$role`. Valid roles are `".implode("`, `", static::getValidRoles())."`.");
}
return $this->getRoleInteger() >= $roleLevel);
}
}
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.