Skip to content

Commit

Permalink
Release 1.0.2 - adjusted readme
Browse files Browse the repository at this point in the history
  • Loading branch information
vanengers committed Oct 16, 2023
1 parent 1674841 commit 8844fca
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
113 changes: 113 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,115 @@
# php-json-object-library
Base object for api

## Installation
``` composer require vanengers/php-json-object-library```

## Usage
```
use Vanengers\PhpJsonObjectLibrary\PhpJsonObject;
class SamplePhpObject extends PhpJsonObject
{
public string $property = 'value';
}
```

### Use array to create object
```
$object = new SamplePhpObject(['property' => 'new value']);
```

### Or pass Json directly
```
$object = new SamplePhpObject("{\"property\":\"new value\"}");
```

### Use setters
```
class SamplePhpObject extends PhpJsonObject
{
public string $property = 'value';
public function setProperty(string $data): self
{
$this->property = $value;
// do stuff here like parsing data, transformation or validations
return $this;
}
}
```

### Translate json/array data to another object property
```
class SamplePhpObject extends PhpJsonObject
{
public $mappers = [
'property_other_remote_name' => 'property'
];
public string $property = 'value';
...
}
```

```
$object = new SamplePhpObject("{\"property_other_remote_name\":\"new value\"}");
$object->getProperty(); // returns: "new value"
```

### serialize to json / create array
```
$object = new SamplePhpObject("{\"property\":\"new value\"}");
$array = $object->toArray();
$json = $object->toJson();
```

### skip certain fields in object from being serialized
```
$object = new SamplePhpObject("{\"property\":\"new value\"}");
$array = $object->toArray(['skip' => ['property']]);
$json = $object->toJson(['skip' => ['property']]);
```

### skip certain fields in object from being serialized
```
$object = new SamplePhpObject("{\"property\":\"new value\"}");
$array = $object->toArray(['skip' => ['property']]);
$json = $object->toJson(['skip' => ['property']]);
```

### skip certain fields in object from being serialized only when value is null
```
$object = new SamplePhpObject("{\"property\":\"new value\"}");
$array = $object->toArray(['skip_null' => ['property']]);
$json = $object->toJson(['skip_null' => ['property']]);
```

### skip certain fields in object from being serialized only when value is empty()
```
$object = new SamplePhpObject("{\"property\":\"new value\"}");
$array = $object->toArray(['skip_empty' => ['property']]);
$json = $object->toJson(['skip_empty' => ['property']]);
```

### exclude properties from serialization
```
class SamplePhpObject extends PhpJsonObject
{
public $exclude_from_array = [
'property'
];
public string $proper;;[ty = 'value';
public string $property2 = 'value';
...
}
```
Which results in
```
{
"property2": "value"
}
```
2 changes: 1 addition & 1 deletion src/PhpJsonObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ abstract class PhpJsonObject
public $mappers = [];

/** @var array $exclude_from_array */
private $exclude_from_array = [];
public $exclude_from_array = [];

/** @var array $hard_exclude_from_array */
private $hard_exclude_from_array = [
Expand Down

0 comments on commit 8844fca

Please sign in to comment.