• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

liqueurdetoile / cakephp-orm-json / 3816907494

pending completion
3816907494

Pull #11

github

GitHub
Merge 1beca5b39 into 6364490a4
Pull Request #11: ci: Fix phinx update issue

895 of 1015 relevant lines covered (88.18%)

43.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.5
/src/Database/Driver/DatFieldMysql.php
1
<?php
2
declare(strict_types=1);
3

4
namespace Lqdt\OrmJson\Database\Driver;
5

6
use Cake\Database\Driver\Mysql;
7
use Cake\Database\Expression\ComparisonExpression;
8
use Cake\Database\Expression\FunctionExpression;
9
use Cake\Database\ExpressionInterface;
10
use Cake\Database\Query;
11
use Lqdt\OrmJson\Database\DatFieldDriverInterface;
12
use Lqdt\OrmJson\Database\Expression\DatFieldExpression;
13
use Lqdt\OrmJson\Database\JsonTypeMap;
14
use Lqdt\OrmJson\DatField\Exception\UnparsableDatFieldException;
15

16
class DatFieldMysql extends Mysql implements DatFieldDriverInterface
17
{
18
    use DatFieldSqlDialectTrait;
19

20
    /**
21
     * @inheritDoc
22
     */
23
    public function translateDatField($datfield, bool $unquote = false, $repository = null)
24
    {
25
        try {
26
            if (is_array($datfield)) {
137✔
27
                return array_map(function ($field) use ($unquote, $repository) {
×
28
                    return $this->translateDatField($field, $unquote, $repository);
×
29
                }, $datfield);
×
30
            }
31

32
            if ($datfield instanceof DatFieldExpression) {
137✔
33
                return $datfield;
2✔
34
            }
35

36
            if ($datfield instanceof ExpressionInterface) {
137✔
37
                return $datfield;
×
38
            }
39

40
            ['doc' => $doc, 'path' => $path] = $this->_extractJsonParts($datfield, $repository);
137✔
41

42
            $expr = new FunctionExpression('JSON_EXTRACT', [$doc => 'identifier', $path => 'literal']);
137✔
43

44
            $expr = $unquote ? new FunctionExpression('JSON_UNQUOTE', [$expr]) : $expr;
137✔
45
            $expr = new DatFieldExpression([$expr]);
137✔
46

47
            return $expr->setDatField($datfield);
137✔
48
        } catch (UnparsableDatFieldException $err) {
2✔
49
            return $datfield;
2✔
50
        }
51
    }
52

53
    /**
54
     * @inheritDoc
55
     */
56
    public function translateSetDatField(
57
        ComparisonExpression $expr,
58
        Query $query,
59
        JsonTypeMap $map
60
    ): ComparisonExpression {
61
        $datfield = $expr->getField();
3✔
62

63
        if (!is_string($datfield)) {
3✔
64
            return $expr;
×
65
        }
66

67
        if (!$this->isDatField($datfield)) {
3✔
68
            // We still need to apply JSON types if field is a JSON field
69
            $casters = $map->getCasters($query);
2✔
70
            $row = [$datfield => $expr->getValue()];
2✔
71
            $row = $this->_castRow($row, $casters, $query);
2✔
72
            $expr->setValue($row[$datfield]);
2✔
73

74
            return $expr;
2✔
75
        }
76

77
        $field = $this->getDatFieldPart('field', $datfield);
2✔
78
        ['doc' => $doc, 'path' => $path] = $this->_extractJsonParts($datfield);
2✔
79
        $caster = $map->getCaster($datfield, $query);
2✔
80
        $value = $this->_castValue($expr->getValue(), $query, $caster);
2✔
81
        $expr->setField($field);
2✔
82
        $expr->setValue(new FunctionExpression('JSON_SET', [$doc => 'identifier', $path => 'literal', $value]));
2✔
83

84
        return $expr;
2✔
85
    }
86

87
    /**
88
     * Utility function to parse needed parts for JSON_EXTRACT or JSON_SET functions
89
     *
90
     * @param  string $datfield Datfield
91
     * @param  string|null|false $repository Repository. IF set to false, existing model will be removed
92
     * @return array            Parts as doc and path
93
     */
94
    protected function _extractJsonParts(string $datfield, $repository = null): array
95
    {
96
        ['model' => $model, 'field' => $field, 'path' => $path] =
137✔
97
          $this->parseDatField($datfield, $repository ? $repository : null);
137✔
98
        $field = $repository !== false && $model ? implode('.', [$model, $field]) : $field;
137✔
99
        // Avoid adding a starting dot in path if querying an array or using joker
100
        $path = in_array($path[0], ['[', '*']) ? '$' . $path : implode('.', ['$', $path]);
137✔
101
        $path = "'" . $path . "'";
137✔
102

103
        return [
137✔
104
          'doc' => $field,
137✔
105
          'path' => $path,
137✔
106
        ];
137✔
107
    }
108
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc