Skip to content

Commit

Permalink
Organizing documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Dec 10, 2024
1 parent 4d5e54d commit 22700b2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 35 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
[![GitHub license](https://img.shields.io/github/license/byjg/serializer.svg)](https://opensource.byjg.com/opensource/licensing.html)
[![GitHub release](https://img.shields.io/github/release/byjg/serializer.svg)](https://github.com/byjg/serializer/releases/)

The Serializer is a library is a versatile tool that allows you convert any object, array or stdClass
to JSON, XML, YAML, and Array, and apply some filter to the properties. During the conversion you can
parse attributes and apply some transformation to the property values on the fly.
The Serializer library is a versatile tool that allows you to convert any object, array, or `stdClass`
into JSON, XML, YAML, or an array. It also enables you to apply filters to properties during
the conversion process. Additionally, you can parse attributes and apply transformations to property
values on the fly.

Also allow you to copy contents from an object to another, even if they have different properties.
The library also allows you to copy content from one object to another, even if their properties differ.

For more information, please check:

Expand Down
26 changes: 15 additions & 11 deletions docs/objectcopy.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
---
sidebar_position: 2
---

# ObjectCopy class

The class ObjectCopy is used to copy the contents from one object to another.
The `ObjectCopy` class is used to copy the contents from one object to another.

The target object doesn't need to have the same properties as the source object
as you can apply transformations that allow you match source and target.
The target object doesn't need to have the same properties as the source object, as you can apply transformations
that allow you to match the source and target.

```php
Object::copy(
Expand All @@ -16,7 +20,7 @@ Object::copy(

## Examples

### Copy contents from an object to another
### Copy contents from one object to another

```php
$soruce = [ "idModel" => 1 , "clientName" => "John", "age" => 30 ];
Expand All @@ -32,7 +36,7 @@ $target = new Target();
ObjectCopy::copy($source, $target);
```

### Copy from CamelCase properties to another with snake_case properties
### Copy from CamelCase properties to snake_case properties

```php
class Source
Expand All @@ -57,7 +61,7 @@ $source->age = 30;
ObjectCopy::copy($source, $target, new CamelToSnakeCase());
```

### Copy from snake_case properties to another with CamelCase properties
### Copy from snake_case properties to CamelCase properties

```php
class Source
Expand All @@ -82,7 +86,7 @@ $source->age = 30;
ObjectCopy::copy($source, $target, new SnakeToCamelCase());
```

### Copy contents and use a map to match the properties
### Copy contents and use a map to match properties

```php
class Source
Expand Down Expand Up @@ -119,13 +123,13 @@ ObjectCopy::copy(

```php
$propertyPattern = function ($propertyName) {
// Execute the logic to match the property name in the target
// ex: change case, change name, different setter, etc
// Execute logic to match the property name in the target
// ex: change case, change name, different setter, etc.
};

$changeValue = function ($sourceName, $targetName, $valueFound) {
// Execute the logic to change the value before set in the target
// ex: change the date format, change the value, etc
// Execute logic to change the value before setting it in the target
// ex: change the date format, modify the value, etc.
};

Object::copy(
Expand Down
8 changes: 6 additions & 2 deletions docs/objectcopyinterface.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
---
sidebar_position: 3
---

# ObjectCopy Interface

ObjectCopyInterface is an interface that exposes the methods `copyFrom` and `copyTo` that allows set property contents from/to another object.
`ObjectCopyInterface` is an interface that exposes the methods `copyFrom` and `copyTo`, which allow you to set property contents from or to another object.

You can either implement this interface or extend the class `ObjectCopy`
You can either implement this interface or extend the class `ObjectCopy`.

```php
<?php
Expand Down
38 changes: 20 additions & 18 deletions docs/serialize.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
---
sidebar_position: 1
---

# Serialize class

Using the class `Serialize` you can convert any object to an array or other formats.
Using the `Serialize` class, you can convert any object into an array or other formats.

During the process you can apply some modifiers to customize the serialization.
During the process, you can apply modifiers to customize the serialization.

Here is the how `Serialize` works:
Here is how `Serialize` works:

```mermaid
block-beta
Expand Down Expand Up @@ -41,19 +45,19 @@ columns 1

## Examples

### Converting any object/content into array
### Converting any object/content into an array

Just use the Serializer class with any kind of object, stdClass or array;
Just use the `Serialize` class with any kind of object, `stdClass`, or array:

```php
<?php
$result = \ByJG\Serializer\Serialize::from($data)->toArray();
$result2 = \ByJG\Serializer\Serialize::fromPhpSerialize($anyPhpSerializedString)->toArray();
$result2 = \ByJG\Serializer\Serialize::fromJson($anyJsonString)->toArray();
$result3 = \ByJG\Serializer\Serialize::fromYaml($anyYamlString)->toArray();
$result3 = \ByJG\Serializer\Serialize::fromJson($anyJsonString)->toArray();
$result4 = \ByJG\Serializer\Serialize::fromYaml($anyYamlString)->toArray();
```

In the examples above `$result`, `$result2` and `$result3` will be an associative array.
In the examples above, `$result`, `$result2`, `$result3`, and `$result4` will be associative arrays.

### Formatting an array into JSON, YAML or ZML

Expand All @@ -67,7 +71,7 @@ echo (new YamlFormatter())->process($data);
echo (new PlainTextFormatter())->process($data);
```

or you call directly from the Serializer:
Alternatively, you can call directly from the `Serialize` class:

```php
<?php
Expand All @@ -82,7 +86,7 @@ echo Serialize::from($data)->parseAttributes($attributeClass, $flags, fn($instan

### Customizing the Serialization

These are the possible modifier for parse:
These are the possible modifiers for parsing:

| Method | Description |
|--------------------------|-------------------------------------------------|
Expand All @@ -95,7 +99,7 @@ These are the possible modifier for parse:

#### Ignore null elements: `withDoNotParseNullValues()`

The SerializerObject brings all properties by default. For example:
By default, the `Serialize` class includes all properties. For example:

```php
<?php
Expand All @@ -114,7 +118,7 @@ print_r($result);
// )
```

But you can setup for ignore the null elements:
To ignore null elements:

```php
<?php
Expand All @@ -131,11 +135,9 @@ print_r($result);

```

#### Do not parse some classes: `withDoNotParse([object])`

Sometimes we want to serialize the object but ignore some class types.
#### Do not parse specific classes: `withDoNotParse([object])`

Setting this option below the whole classes defined in the setDoNotParse will be ignored and not parsed:
To serialize an object but ignore specific class types:

```php
<?php
Expand All @@ -148,7 +150,7 @@ $result = \ByJG\Serializer\Serialize::from($myclass)

#### Return only string elements: `withOnlyString()`

Sometimes we want to serialize the object and return only the string elements.
To serialize an object and return only string elements:

```php
<?php
Expand Down Expand Up @@ -176,7 +178,7 @@ $result = \ByJG\Serializer\Serialize::from($myclass)
// )
```

#### Use the pattern to convert method into property: `withMethodPattern($pattern, $replace)`
#### Use the pattern to convert method into properties: `withMethodPattern($pattern, $replace)`

In the class we might have the name `property` name different from the getter method.

Expand Down

0 comments on commit 22700b2

Please sign in to comment.