PHP code example of xp-forge / partial

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

    

xp-forge / partial example snippets


namespace example;

use lang\partial\Box;
use lang\Value;

class Name implements Value {
  use Name\is\Box;

  public function personal() { return '~' === $this->value{0}; }
}

namespace example;

use lang\Value;

class Name implements Value {
  protected $value;

  public function __construct($value) { $this->value= $value; }

  public function value() { return $this->value; }

  public function personal() { return '~' === $this->value{0}; }

  public function hashCode() { /* ... */ }

  public function compareTo($value) { /* ... */ }

  public function toString() { /* ... */ }
}

namespace example;

use lang\partial\Accessors;

class Wall {
  use Wall\with\Accessors;

  private $name, $type, $posts;

  public function __construct(
    Name $name,
    Type $type,
    Posts $posts
  ) {
    $this->name= $name;
    $this->type= $type;
    $this->posts= $posts;
  }
}

namespace example;

class Wall {
  private $name, $type, $posts;

  public function __construct(
    Name $name,
    Type $type,
    Posts $posts
  ) {
    $this->name= $name;
    $this->type= $type;
    $this->posts= $posts;
  }

  public function name() { return $this->name; }

  public function type() { return $this->type; }

  public function posts() { return $this->posts; }
}

namespace example;

use lang\partial\Constructor;

class Author {
  use Author\with\Constructor;

  private $handle, $name;
}

namespace example;

class Author {
  private $handle, $name;

  public function __construct($handle, $name) {
    $this->handle= $handle;
    $this->name= $name;
  }
}

namespace example;

use lang\partial\ListOf;

class Posts implements \lang\Value, \IteratorAggregate {
  use Posts\is\ListOf;
}

namespace example;

class Posts implements \lang\Value, \IteratorAggregate {
  private $backing;

  public function __construct(...$elements) {
    $this->backing= $elements;
  }

  public function present() { return !empty($this->backing); }

  public function size() { return sizeof($this->backing); }

  public function at($offset) {
    if (isset($this->backing[$offset])) {
      return $this->backing[$offset];
    }
    throw new ElementNotFoundException(…);
  }

  public function first() {
    if (empty($this->backing)) {
      throw new ElementNotFoundException(…);
    }
    return $this->backing[0];
  }

  public function getIterator() {
    foreach ($this->backing as $element) {
      yield $element;
    }
  }

  public function compareTo($value) { /* ... */ }

  public function toString() { /* ... */ }

  public function hashCode() { /* ... */ }
}

namespace example;

use lang\partial\Accessors;
use lang\partial\Builder;
use lang\partial\Comparators;

class Post {
  use Wall\with\Accessors;
  use Wall\with\Builder;
  use Wall\with\Comparators;

  private $author, $text, $date;

  public function __construct($author, $text, Date $date) {
    $this->author= $author;
    $this->text= $text;
    $this->date= $date;
  }
}

namespace example;

use lang\partial\ListIndexedBy;

class Walls implements \IteratorAggregate {
  use Walls\is\ListIndexedBy;

  protected function index($wall) { return $wall->name()->value(); }
}

use util\data\Sequence;

$post= Post::with()->author('Timm')->text('Hello World!')->date(Date::now())->create();

$walls= new Walls(
  new Wall(new Name('one'), Type::$OPEN, new Posts()),
  new Wall(new Name('two'), Type::$CLOSED, new Posts($post))
);

$walls->present();        // TRUE, list is not empty
$walls->size();           // 2
$walls->provides('one');  // TRUE, wall named one found
$walls->provides('zero'); // FALSE, no such wall
$walls->first();          // Wall(name => Name("one"), type => OPEN)
$walls->named('two');     // Wall(name => Name("two"), type => CLOSED)
$walls->named('three');   // ***ElementNotFoundException

foreach ($walls as $wall) {
  Console::writeLine('== ', $wall->name()->value(), ' wall (', $wall->type(), ') ==');
  Sequence::of($wall->posts())->sorted(Post::byDate())->each(function($post) {
    Console::writeLine('Written by ', $post->author(), ' on ', $post->date());
    Console::writeLine($post->text());
    Console::writeLine();
  });
}