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

laravie / query-filter / 4360374084

pending completion
4360374084

push

github

Mior Muhammad Zaki
wip

27 of 27 new or added lines in 4 files covered. (100.0%)

310 of 317 relevant lines covered (97.79%)

14.32 hits per line

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

78.13
/src/Field.php
1
<?php
2

3
namespace Laravie\QueryFilter;
4

5
use Laravie\QueryFilter\Concerns\ConditionallySearchingWildcard;
6
use Laravie\QueryFilter\Contracts\Field as FieldContract;
7

8
class Field extends Column implements FieldContract
9
{
10
    use ConditionallySearchingWildcard;
11

12
    /**
13
     * Searchable instance.
14
     *
15
     * @var \Laravie\QueryFilter\Searchable
16
     */
17
    protected $searchable;
18

19
    /**
20
     * Make a new Field value object.
21
     *
22
     * @param  static|\Illuminate\Contracts\Database\Query\Expression|string  $name
23
     * @return static
24
     */
25
    public static function make($name)
26
    {
27
        if ($name instanceof static) {
24✔
28
            return tap(new static($name->getValue()), static function ($field) use ($name) {
1✔
29
                $field->wildcardSearching = $name->wildcardSearching;
1✔
30
            });
1✔
31
        }
32

33
        return new static($name);
24✔
34
    }
35

36
    /**
37
     * Validate column.
38
     */
39
    public function validate(): bool
40
    {
41
        if ($this->isRelationSelector()) {
28✔
42
            return $this->validateRelationColumn();
×
43
        } elseif ($this->isJsonPathSelector()) {
28✔
44
            return $this->validateJsonPath();
7✔
45
        }
46

47
        return parent::validate();
21✔
48
    }
49

50
    /**
51
     * Validate Relation  field + path.
52
     */
53
    protected function validateRelationColumn(): bool
54
    {
55
        if ($this->isExpression()) {
×
56
            return false;
×
57
        }
58

59
        /** @var string $name */
60
        $name = $this->name;
×
61

62
        [, $field] = explode('.', $name, 2);
×
63

64
        return Column::make($field)->validate();
×
65
    }
66

67
    /**
68
     * Validate JSON column + path.
69
     */
70
    protected function validateJsonPath(): bool
71
    {
72
        if ($this->isExpression()) {
7✔
73
            return false;
×
74
        }
75

76
        /** @var string $name */
77
        $name = $this->name;
7✔
78

79
        $parts = explode('->', $name, 2);
7✔
80

81
        $field = $parts[0];
7✔
82
        $path = \count($parts) > 1 ? $this->wrapJsonPath($parts[1], '->') : '';
7✔
83

84
        return Column::make($field)->validate() && Column::make(str_replace('.', '', $path))->validate();
7✔
85
    }
86

87
    /**
88
     * Is relation selector.
89
     */
90
    public function isRelationSelector(): bool
91
    {
92
        if ($this->isExpression()) {
29✔
93
            return false;
5✔
94
        }
95

96
        /** @var string $name */
97
        $name = $this->name;
24✔
98

99
        return strpos($name, '.') !== false;
24✔
100
    }
101

102
    /**
103
     * Is JSON path selector.
104
     */
105
    public function isJsonPathSelector(): bool
106
    {
107
        if ($this->isExpression()) {
28✔
108
            return false;
5✔
109
        }
110

111
        /** @var string $name */
112
        $name = $this->name;
23✔
113

114
        return strpos($name, '->') !== false;
23✔
115
    }
116

117
    /**
118
     * Wrap the given JSON path.
119
     */
120
    protected function wrapJsonPath(string $value, string $delimiter = '->'): string
121
    {
122
        /** @var string $value */
123
        $value = preg_replace("/([\\\\]+)?\\'/", "\\'", $value);
7✔
124

125
        return str_replace($delimiter, '.', $value);
7✔
126
    }
127
}
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

© 2025 Coveralls, Inc