1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the jquery-datatables-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2019 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\JQuery\DataTablesBundle\Helper; |
13
|
|
|
|
14
|
|
|
use JsonSerializable; |
15
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
16
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
17
|
|
|
use Symfony\Component\Serializer\Serializer; |
18
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Entity\DataTablesEntityInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* DataTables entity helper. |
22
|
|
|
* |
23
|
|
|
* @author webeweb <https://github.com/webeweb/> |
24
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\Helper |
25
|
|
|
*/ |
26
|
|
|
class DataTablesEntityHelper { |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Determines if an entity is compatible. |
30
|
|
|
* |
31
|
|
|
* @param DataTablesEntityInterface|object $entity The entity. |
32
|
|
|
* @return bool Returns true in case of success, false otherwise. |
33
|
|
|
*/ |
34
|
|
|
public static function isCompatible($entity) { |
35
|
|
|
|
36
|
|
|
if (true === ($entity instanceof DataTablesEntityInterface)) { |
37
|
|
|
return true; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (true === is_object($entity) && true === method_exists($entity, "getId")) { |
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Creates a serializer. |
49
|
|
|
* |
50
|
|
|
* @return Serializer Returns the serializer. |
51
|
|
|
*/ |
52
|
|
|
public static function newSerializer() { |
53
|
|
|
return new Serializer([new ObjectNormalizer()], [new JsonEncoder()]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Serialize an entity. |
58
|
|
|
* |
59
|
|
|
* @param object $entity The entity. |
60
|
|
|
* @return string Returns the serialized entity. |
61
|
|
|
*/ |
62
|
|
|
public static function jsonSerialize($entity) { |
63
|
|
|
|
64
|
|
|
$data = true === ($entity instanceof JsonSerializable) ? $entity->jsonSerialize() : $entity; |
65
|
|
|
|
66
|
|
|
return static::newSerializer()->serialize($data, "json"); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|