Passed
Push — master ( 904963...c192c4 )
by
unknown
14:57 queued 05:56
created

SearchEngineFieldValue::setFieldId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Entity;
8
9
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
10
use ApiPlatform\Metadata\ApiFilter;
11
use ApiPlatform\Metadata\ApiProperty;
12
use ApiPlatform\Metadata\ApiResource;
13
use ApiPlatform\Metadata\Get;
14
use ApiPlatform\Metadata\GetCollection;
15
use Doctrine\ORM\Mapping as ORM;
16
use Symfony\Component\Serializer\Annotation\Groups;
17
18
/**
19
 * SearchEngineFieldValue.
20
 *
21
 * Stores a value for a specific search field (prefix) for a given ResourceNode.
22
 * This is typically used by the Xapian search layer.
23
 */
24
#[ORM\Table(name: 'search_engine_field_value')]
25
#[ORM\UniqueConstraint(name: 'uniq_search_field_value_node_field', columns: ['resource_node_id', 'field_id'])]
26
#[ORM\Index(name: 'idx_sefv_resource_node', columns: ['resource_node_id'])]
27
#[ORM\Index(name: 'idx_sefv_field', columns: ['field_id'])]
28
#[ORM\Entity]
29
#[ApiResource(
30
    operations: [
31
        new GetCollection(
32
            normalizationContext: ['groups' => ['sefv:read']],
33
            paginationEnabled: false
34
        ),
35
        new Get(
36
            normalizationContext: ['groups' => ['sefv:read']]
37
        ),
38
    ],
39
    normalizationContext: ['groups' => ['sefv:read']],
40
    security: "is_granted('ROLE_TEACHER') or is_granted('ROLE_ADMIN')"
41
)]
42
#[ApiFilter(SearchFilter::class, properties: [
43
    // Allows: ?resourceNode=/api/resource_nodes/20228
44
    'resourceNode' => 'exact',
45
    // Optional: allows filtering by field too: ?field=/api/search_engine_fields/1
46
    'field' => 'exact',
47
])]
48
class SearchEngineFieldValue
49
{
50
    #[ORM\Column(name: 'id', type: 'integer')]
51
    #[ORM\Id]
52
    #[ORM\GeneratedValue(strategy: 'IDENTITY')]
53
    #[Groups(['sefv:read'])]
54
    protected ?int $id = null;
55
56
    #[ORM\ManyToOne(targetEntity: ResourceNode::class)]
57
    #[ORM\JoinColumn(name: 'resource_node_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
58
    #[Groups(['sefv:read'])]
59
    #[ApiProperty(readableLink: false)]
60
    protected ResourceNode $resourceNode;
61
62
    #[ORM\ManyToOne(targetEntity: SearchEngineField::class)]
63
    #[ORM\JoinColumn(name: 'field_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
64
    #[Groups(['sefv:read'])]
65
    #[ApiProperty(readableLink: false)]
66
    protected SearchEngineField $field;
67
68
    #[ORM\Column(name: 'value', type: 'string', length: 200, nullable: false)]
69
    #[Groups(['sefv:read'])]
70
    protected string $value = '';
71
72
    public function getId(): ?int
73
    {
74
        return $this->id;
75
    }
76
77
    public function getResourceNode(): ResourceNode
78
    {
79
        return $this->resourceNode;
80
    }
81
82
    public function setResourceNode(ResourceNode $resourceNode): self
83
    {
84
        $this->resourceNode = $resourceNode;
85
86
        return $this;
87
    }
88
89
    public function getField(): SearchEngineField
90
    {
91
        return $this->field;
92
    }
93
94
    public function setField(SearchEngineField $field): self
95
    {
96
        $this->field = $field;
97
98
        return $this;
99
    }
100
101
    public function setValue(string $value): self
102
    {
103
        $value = trim($value);
104
105
        // Avoid DB errors if someone passes a very long string.
106
        if (strlen($value) > 200) {
107
            $value = substr($value, 0, 200);
108
        }
109
110
        $this->value = $value;
111
112
        return $this;
113
    }
114
115
    public function getValue(): string
116
    {
117
        return $this->value;
118
    }
119
}
120