AbstractCatalogIdentifiable::arrayToEntries()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogos\Common;
6
7
abstract class AbstractCatalogIdentifiable implements CatalogIdentifiable
8
{
9
    use BaseCatalogTrait;
10
11
    /**
12
     * @param array<string, scalar> $data
13
     * @return EntryIdentifiable
14
     */
15
    abstract public function create(array $data): EntryIdentifiable;
16
17
    abstract protected function catalogName(): string;
18
19 62
    public function obtain(string $id): EntryIdentifiable
20
    {
21 62
        $data = $this->repository()->queryById($this->catalogName(), $id);
22 60
        return $this->create($data);
23
    }
24
25 3
    public function exists(string $id): bool
26
    {
27 3
        return $this->repository()->existsId($this->catalogName(), $id);
28
    }
29
30
    /**
31
     * @param string[] $ids
32
     * @return EntryIdentifiable[]
33
     */
34 1
    public function obtainByIds(array $ids): array
35
    {
36 1
        return $this->arrayToEntries(
37 1
            $this->repository()->queryByIds($this->catalogName(), $ids),
38 1
        );
39
    }
40
41 2
    public function searchByField(string $fieldName, string $search, int $limit = 0): array
42
    {
43 2
        $results = $this->repository()->queryRowsByFields(
44 2
            $this->catalogName(),
45 2
            [$fieldName => $search],
46 2
            $limit,
47 2
            false,
48 2
        );
49
50 2
        return $this->arrayToEntries($results);
51
    }
52
53 2
    public function searchByText(string $search, int $limit = 0): array
54
    {
55 2
        return $this->searchByField('texto', $search, $limit);
56
    }
57
58
    /**
59
     * @param array<array<string, scalar>> $entries
60
     * @return EntryIdentifiable[]
61
     */
62 3
    private function arrayToEntries(array $entries): array
63
    {
64 3
        return array_map(function (array $data): EntryIdentifiable {
65 3
            return $this->create($data);
66 3
        }, $entries);
67
    }
68
}
69