Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanak-michal authored Jul 20, 2021
1 parent 8322ac0 commit d4d7003
Showing 1 changed file with 82 additions and 4 deletions.
86 changes: 82 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,87 @@
# deepr-php

### WIP

PHP API library following Deepr specification
PHP API library following Deepr specification

https://github.com/deeprjs/deepr

https://refactoring.guru/design-patterns/composite

### Example

```php

function applyColumns(IComponent $object, array $row)
{
foreach ($row as $column => $value) {
if (property_exists($object, $column))
$object->$column = $value;
}
}

class Movies extends Collection implements ILoadable
{
public $count;

public function __construct()
{
$this->count = DB::getMoviesCount();
}

public function load(): array
{
$items = [];
foreach (DB::getMovies() as $row) {
$movie = new Movie();
applyColumns($movie, $row);
$items[] = $movie;
}
return $items;
}

public function getByTitle(string $title): Collection
{
$collection = new self();

$movie = new Movie();
$row = DB::getMovieByTitle($title);
applyColumns($movie, $row);
$collection->add($movie);

return $collection;
}
}

class Movie extends Collection
{
public $_id;
public $title;
public $released;
public $tagline;
}

class Root extends Collection
{
public $movies = Movies::class;
}

$deepr = new Deepr();
$json = '{"movies": {"count": true, "[]": [0, 2],"title": true, "released": true}}';
$collection = new Root();
$deepr->invokeQuery($collection, json_decode($j, true));
echo '<pre>' . json_encode($collection->execute(), JSON_PRETTY_PRINT) . '</pre>' . PHP_EOL;

/*
{
"movies": {
"count": 38,
"0": {
"title": "The Matrix",
"released": 1999
},
"1": {
"title": "The Matrix Reloaded",
"released": 2003
}
}
}
*/
```

0 comments on commit d4d7003

Please sign in to comment.