PHP code example of socialengine / unum

1. Go to this page and download the library: Download socialengine/unum 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/ */

    

socialengine / unum example snippets


class MyEntity extends \SocialEngine\Unum\Entity
{
  protected $name;
  protected $email;
}

$entity = new MyEntity(['name' => 'Duke', 'email' => '[email protected]']);

$entity = new MyEntity(['name' => 'Duke', 'email' => '[email protected]']);
echo $entity->name; // Duke
echo $entity['email']; // [email protected]

class GetMethodEntity extends \SocialEngine\Unum\Entity
{
    protected $name;
    
    protected function getName()
    {
        return $name . ' From Method!';
    }
}

$entity = new GetMethodEntity(['name' => 'Hello']);
var_dump($entity->name); // string(18) "Hello From Method!"

$entity = new MyEntity();
$entity->name = 'My Name';
$entity['email'] = '[email protected]';
var_dump($entity->toArray());
/*
array(2) {
  'name' =>
  string(4) "Duke"
  'email' =>
  string(24) "[email protected]"
}
*/

class SetMethodEntity extends \SocialEngine\Unum\Entity
{
    protected $name;
    
    protected function setName($value)
    {
        $this->name = $name . ' Set By Method!';
    }
}

$entity = new SetMethodEntity(['name' => 'Hello']);
var_dump($entity->name); // string(20) "Hello Set By Method!"

$entity = new MyEntity();
$entity->name = 'My Name';
var_dump($entity->toArray(true));
/*
array(1) {
  'name' =>
  string(7) "My Name"
}
*/

$entity = new MyEntity();
$entity->name = 'My Name';
$entity->email = 'whatever';
$entity->clean();
var_dump($entity->toArray(true));
/*
array(0) {
}
*/



 MyEntity extends \SocialEngine\Unum\Entity
{
  protected $category_id;
  protected $first_name;
  protected $last_name;

  protected function getCategoryName()
  {
    $categoryNamesMap = [
      1 => 'Awesome'
    ];

    return $categoryNamesMap[$this->getProp('category_id')];
  }

  /**
   * You can also set meta properties
   */
  protected function setName($name)
  {
    list($firstName, $lastName) = explode(' ', $name, 2);

    $this->fromArray([
      'first_name' => $firstName,
      'last_name' => $lastName
    ]);
  }
}

$entity = new MyEntity(['category_id' => 1]);
var_dump($entity->categoryName);
/*
string(7) "Awesome"
*/

$entity['name'] = 'Duke Orcino';
var_dump($entity->toArray(true));
/*
array(2) {
  'first_name' =>
  string(4) "Duke"
  'last_name' =>
  string(6) "Orcino"
}
*/