@@ -32,170 +32,170 @@ |
||
| 32 | 32 | * Tools for transforming search queries into database queries |
| 33 | 33 | */ |
| 34 | 34 | class QuerySearchHelper { |
| 35 | - static protected $searchOperatorMap = [ |
|
| 36 | - ISearchComparison::COMPARE_LIKE => 'iLike', |
|
| 37 | - ISearchComparison::COMPARE_EQUAL => 'eq', |
|
| 38 | - ISearchComparison::COMPARE_GREATER_THAN => 'gt', |
|
| 39 | - ISearchComparison::COMPARE_GREATER_THAN_EQUAL => 'gte', |
|
| 40 | - ISearchComparison::COMPARE_LESS_THAN => 'lt', |
|
| 41 | - ISearchComparison::COMPARE_LESS_THAN_EQUAL => 'lte' |
|
| 42 | - ]; |
|
| 43 | - |
|
| 44 | - static protected $searchOperatorNegativeMap = [ |
|
| 45 | - ISearchComparison::COMPARE_LIKE => 'notLike', |
|
| 46 | - ISearchComparison::COMPARE_EQUAL => 'neq', |
|
| 47 | - ISearchComparison::COMPARE_GREATER_THAN => 'lte', |
|
| 48 | - ISearchComparison::COMPARE_GREATER_THAN_EQUAL => 'lt', |
|
| 49 | - ISearchComparison::COMPARE_LESS_THAN => 'gte', |
|
| 50 | - ISearchComparison::COMPARE_LESS_THAN_EQUAL => 'lt' |
|
| 51 | - ]; |
|
| 52 | - |
|
| 53 | - const TAG_FAVORITE = '_$!<Favorite>!$_'; |
|
| 54 | - |
|
| 55 | - /** @var IMimeTypeLoader */ |
|
| 56 | - private $mimetypeLoader; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * QuerySearchUtil constructor. |
|
| 60 | - * |
|
| 61 | - * @param IMimeTypeLoader $mimetypeLoader |
|
| 62 | - */ |
|
| 63 | - public function __construct(IMimeTypeLoader $mimetypeLoader) { |
|
| 64 | - $this->mimetypeLoader = $mimetypeLoader; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Whether or not the tag tables should be joined to complete the search |
|
| 69 | - * |
|
| 70 | - * @param ISearchOperator $operator |
|
| 71 | - * @return boolean |
|
| 72 | - */ |
|
| 73 | - public function shouldJoinTags(ISearchOperator $operator) { |
|
| 74 | - if ($operator instanceof ISearchBinaryOperator) { |
|
| 75 | - return array_reduce($operator->getArguments(), function ($shouldJoin, ISearchOperator $operator) { |
|
| 76 | - return $shouldJoin || $this->shouldJoinTags($operator); |
|
| 77 | - }, false); |
|
| 78 | - } else if ($operator instanceof ISearchComparison) { |
|
| 79 | - return $operator->getField() === 'tagname' || $operator->getField() === 'favorite'; |
|
| 80 | - } |
|
| 81 | - return false; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - public function searchOperatorToDBExpr(IQueryBuilder $builder, ISearchOperator $operator) { |
|
| 85 | - $expr = $builder->expr(); |
|
| 86 | - if ($operator instanceof ISearchBinaryOperator) { |
|
| 87 | - switch ($operator->getType()) { |
|
| 88 | - case ISearchBinaryOperator::OPERATOR_NOT: |
|
| 89 | - $negativeOperator = $operator->getArguments()[0]; |
|
| 90 | - if ($negativeOperator instanceof ISearchComparison) { |
|
| 91 | - return $this->searchComparisonToDBExpr($builder, $negativeOperator, self::$searchOperatorNegativeMap); |
|
| 92 | - } else { |
|
| 93 | - throw new \InvalidArgumentException('Binary operators inside "not" is not supported'); |
|
| 94 | - } |
|
| 95 | - case ISearchBinaryOperator::OPERATOR_AND: |
|
| 96 | - return $expr->andX($this->searchOperatorToDBExpr($builder, $operator->getArguments()[0]), $this->searchOperatorToDBExpr($builder, $operator->getArguments()[1])); |
|
| 97 | - case ISearchBinaryOperator::OPERATOR_OR: |
|
| 98 | - return $expr->orX($this->searchOperatorToDBExpr($builder, $operator->getArguments()[0]), $this->searchOperatorToDBExpr($builder, $operator->getArguments()[1])); |
|
| 99 | - default: |
|
| 100 | - throw new \InvalidArgumentException('Invalid operator type: ' . $operator->getType()); |
|
| 101 | - } |
|
| 102 | - } else if ($operator instanceof ISearchComparison) { |
|
| 103 | - return $this->searchComparisonToDBExpr($builder, $operator, self::$searchOperatorMap); |
|
| 104 | - } else { |
|
| 105 | - throw new \InvalidArgumentException('Invalid operator type: ' . get_class($operator)); |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - private function searchComparisonToDBExpr(IQueryBuilder $builder, ISearchComparison $comparison, array $operatorMap) { |
|
| 110 | - $this->validateComparison($comparison); |
|
| 111 | - |
|
| 112 | - list($field, $value, $type) = $this->getOperatorFieldAndValue($comparison); |
|
| 113 | - if (isset($operatorMap[$type])) { |
|
| 114 | - $queryOperator = $operatorMap[$type]; |
|
| 115 | - return $builder->expr()->$queryOperator($field, $this->getParameterForValue($builder, $value)); |
|
| 116 | - } else { |
|
| 117 | - throw new \InvalidArgumentException('Invalid operator type: ' . $comparison->getType()); |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - private function getOperatorFieldAndValue(ISearchComparison $operator) { |
|
| 122 | - $field = $operator->getField(); |
|
| 123 | - $value = $operator->getValue(); |
|
| 124 | - $type = $operator->getType(); |
|
| 125 | - if ($field === 'mimetype') { |
|
| 126 | - if ($operator->getType() === ISearchComparison::COMPARE_EQUAL) { |
|
| 127 | - $value = $this->mimetypeLoader->getId($value); |
|
| 128 | - } else if ($operator->getType() === ISearchComparison::COMPARE_LIKE) { |
|
| 129 | - // transform "mimetype='foo/%'" to "mimepart='foo'" |
|
| 130 | - if (preg_match('|(.+)/%|', $value, $matches)) { |
|
| 131 | - $field = 'mimepart'; |
|
| 132 | - $value = $this->mimetypeLoader->getId($matches[1]); |
|
| 133 | - $type = ISearchComparison::COMPARE_EQUAL; |
|
| 134 | - } |
|
| 135 | - if (strpos($value, '%') !== false) { |
|
| 136 | - throw new \InvalidArgumentException('Unsupported query value for mimetype: ' . $value . ', only values in the format "mime/type" or "mime/%" are supported'); |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - } else if ($field === 'favorite') { |
|
| 140 | - $field = 'tag.category'; |
|
| 141 | - $value = self::TAG_FAVORITE; |
|
| 142 | - } else if ($field === 'tagname') { |
|
| 143 | - $field = 'tag.category'; |
|
| 144 | - } |
|
| 145 | - return [$field, $value, $type]; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - private function validateComparison(ISearchComparison $operator) { |
|
| 149 | - $types = [ |
|
| 150 | - 'mimetype' => 'string', |
|
| 151 | - 'mtime' => 'integer', |
|
| 152 | - 'name' => 'string', |
|
| 153 | - 'size' => 'integer', |
|
| 154 | - 'tagname' => 'string', |
|
| 155 | - 'favorite' => 'boolean', |
|
| 156 | - 'fileid' => 'integer' |
|
| 157 | - ]; |
|
| 158 | - $comparisons = [ |
|
| 159 | - 'mimetype' => ['eq', 'like'], |
|
| 160 | - 'mtime' => ['eq', 'gt', 'lt', 'gte', 'lte'], |
|
| 161 | - 'name' => ['eq', 'like'], |
|
| 162 | - 'size' => ['eq', 'gt', 'lt', 'gte', 'lte'], |
|
| 163 | - 'tagname' => ['eq', 'like'], |
|
| 164 | - 'favorite' => ['eq'], |
|
| 165 | - 'fileid' => ['eq'] |
|
| 166 | - ]; |
|
| 167 | - |
|
| 168 | - if (!isset($types[$operator->getField()])) { |
|
| 169 | - throw new \InvalidArgumentException('Unsupported comparison field ' . $operator->getField()); |
|
| 170 | - } |
|
| 171 | - $type = $types[$operator->getField()]; |
|
| 172 | - if (gettype($operator->getValue()) !== $type) { |
|
| 173 | - throw new \InvalidArgumentException('Invalid type for field ' . $operator->getField()); |
|
| 174 | - } |
|
| 175 | - if (!in_array($operator->getType(), $comparisons[$operator->getField()])) { |
|
| 176 | - throw new \InvalidArgumentException('Unsupported comparison for field ' . $operator->getField() . ': ' . $operator->getType()); |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - private function getParameterForValue(IQueryBuilder $builder, $value) { |
|
| 181 | - if ($value instanceof \DateTime) { |
|
| 182 | - $value = $value->getTimestamp(); |
|
| 183 | - } |
|
| 184 | - if (is_numeric($value)) { |
|
| 185 | - $type = IQueryBuilder::PARAM_INT; |
|
| 186 | - } else { |
|
| 187 | - $type = IQueryBuilder::PARAM_STR; |
|
| 188 | - } |
|
| 189 | - return $builder->createNamedParameter($value, $type); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * @param IQueryBuilder $query |
|
| 194 | - * @param ISearchOrder[] $orders |
|
| 195 | - */ |
|
| 196 | - public function addSearchOrdersToQuery(IQueryBuilder $query, array $orders) { |
|
| 197 | - foreach ($orders as $order) { |
|
| 198 | - $query->addOrderBy($order->getField(), $order->getDirection()); |
|
| 199 | - } |
|
| 200 | - } |
|
| 35 | + static protected $searchOperatorMap = [ |
|
| 36 | + ISearchComparison::COMPARE_LIKE => 'iLike', |
|
| 37 | + ISearchComparison::COMPARE_EQUAL => 'eq', |
|
| 38 | + ISearchComparison::COMPARE_GREATER_THAN => 'gt', |
|
| 39 | + ISearchComparison::COMPARE_GREATER_THAN_EQUAL => 'gte', |
|
| 40 | + ISearchComparison::COMPARE_LESS_THAN => 'lt', |
|
| 41 | + ISearchComparison::COMPARE_LESS_THAN_EQUAL => 'lte' |
|
| 42 | + ]; |
|
| 43 | + |
|
| 44 | + static protected $searchOperatorNegativeMap = [ |
|
| 45 | + ISearchComparison::COMPARE_LIKE => 'notLike', |
|
| 46 | + ISearchComparison::COMPARE_EQUAL => 'neq', |
|
| 47 | + ISearchComparison::COMPARE_GREATER_THAN => 'lte', |
|
| 48 | + ISearchComparison::COMPARE_GREATER_THAN_EQUAL => 'lt', |
|
| 49 | + ISearchComparison::COMPARE_LESS_THAN => 'gte', |
|
| 50 | + ISearchComparison::COMPARE_LESS_THAN_EQUAL => 'lt' |
|
| 51 | + ]; |
|
| 52 | + |
|
| 53 | + const TAG_FAVORITE = '_$!<Favorite>!$_'; |
|
| 54 | + |
|
| 55 | + /** @var IMimeTypeLoader */ |
|
| 56 | + private $mimetypeLoader; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * QuerySearchUtil constructor. |
|
| 60 | + * |
|
| 61 | + * @param IMimeTypeLoader $mimetypeLoader |
|
| 62 | + */ |
|
| 63 | + public function __construct(IMimeTypeLoader $mimetypeLoader) { |
|
| 64 | + $this->mimetypeLoader = $mimetypeLoader; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Whether or not the tag tables should be joined to complete the search |
|
| 69 | + * |
|
| 70 | + * @param ISearchOperator $operator |
|
| 71 | + * @return boolean |
|
| 72 | + */ |
|
| 73 | + public function shouldJoinTags(ISearchOperator $operator) { |
|
| 74 | + if ($operator instanceof ISearchBinaryOperator) { |
|
| 75 | + return array_reduce($operator->getArguments(), function ($shouldJoin, ISearchOperator $operator) { |
|
| 76 | + return $shouldJoin || $this->shouldJoinTags($operator); |
|
| 77 | + }, false); |
|
| 78 | + } else if ($operator instanceof ISearchComparison) { |
|
| 79 | + return $operator->getField() === 'tagname' || $operator->getField() === 'favorite'; |
|
| 80 | + } |
|
| 81 | + return false; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + public function searchOperatorToDBExpr(IQueryBuilder $builder, ISearchOperator $operator) { |
|
| 85 | + $expr = $builder->expr(); |
|
| 86 | + if ($operator instanceof ISearchBinaryOperator) { |
|
| 87 | + switch ($operator->getType()) { |
|
| 88 | + case ISearchBinaryOperator::OPERATOR_NOT: |
|
| 89 | + $negativeOperator = $operator->getArguments()[0]; |
|
| 90 | + if ($negativeOperator instanceof ISearchComparison) { |
|
| 91 | + return $this->searchComparisonToDBExpr($builder, $negativeOperator, self::$searchOperatorNegativeMap); |
|
| 92 | + } else { |
|
| 93 | + throw new \InvalidArgumentException('Binary operators inside "not" is not supported'); |
|
| 94 | + } |
|
| 95 | + case ISearchBinaryOperator::OPERATOR_AND: |
|
| 96 | + return $expr->andX($this->searchOperatorToDBExpr($builder, $operator->getArguments()[0]), $this->searchOperatorToDBExpr($builder, $operator->getArguments()[1])); |
|
| 97 | + case ISearchBinaryOperator::OPERATOR_OR: |
|
| 98 | + return $expr->orX($this->searchOperatorToDBExpr($builder, $operator->getArguments()[0]), $this->searchOperatorToDBExpr($builder, $operator->getArguments()[1])); |
|
| 99 | + default: |
|
| 100 | + throw new \InvalidArgumentException('Invalid operator type: ' . $operator->getType()); |
|
| 101 | + } |
|
| 102 | + } else if ($operator instanceof ISearchComparison) { |
|
| 103 | + return $this->searchComparisonToDBExpr($builder, $operator, self::$searchOperatorMap); |
|
| 104 | + } else { |
|
| 105 | + throw new \InvalidArgumentException('Invalid operator type: ' . get_class($operator)); |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + private function searchComparisonToDBExpr(IQueryBuilder $builder, ISearchComparison $comparison, array $operatorMap) { |
|
| 110 | + $this->validateComparison($comparison); |
|
| 111 | + |
|
| 112 | + list($field, $value, $type) = $this->getOperatorFieldAndValue($comparison); |
|
| 113 | + if (isset($operatorMap[$type])) { |
|
| 114 | + $queryOperator = $operatorMap[$type]; |
|
| 115 | + return $builder->expr()->$queryOperator($field, $this->getParameterForValue($builder, $value)); |
|
| 116 | + } else { |
|
| 117 | + throw new \InvalidArgumentException('Invalid operator type: ' . $comparison->getType()); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + private function getOperatorFieldAndValue(ISearchComparison $operator) { |
|
| 122 | + $field = $operator->getField(); |
|
| 123 | + $value = $operator->getValue(); |
|
| 124 | + $type = $operator->getType(); |
|
| 125 | + if ($field === 'mimetype') { |
|
| 126 | + if ($operator->getType() === ISearchComparison::COMPARE_EQUAL) { |
|
| 127 | + $value = $this->mimetypeLoader->getId($value); |
|
| 128 | + } else if ($operator->getType() === ISearchComparison::COMPARE_LIKE) { |
|
| 129 | + // transform "mimetype='foo/%'" to "mimepart='foo'" |
|
| 130 | + if (preg_match('|(.+)/%|', $value, $matches)) { |
|
| 131 | + $field = 'mimepart'; |
|
| 132 | + $value = $this->mimetypeLoader->getId($matches[1]); |
|
| 133 | + $type = ISearchComparison::COMPARE_EQUAL; |
|
| 134 | + } |
|
| 135 | + if (strpos($value, '%') !== false) { |
|
| 136 | + throw new \InvalidArgumentException('Unsupported query value for mimetype: ' . $value . ', only values in the format "mime/type" or "mime/%" are supported'); |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + } else if ($field === 'favorite') { |
|
| 140 | + $field = 'tag.category'; |
|
| 141 | + $value = self::TAG_FAVORITE; |
|
| 142 | + } else if ($field === 'tagname') { |
|
| 143 | + $field = 'tag.category'; |
|
| 144 | + } |
|
| 145 | + return [$field, $value, $type]; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + private function validateComparison(ISearchComparison $operator) { |
|
| 149 | + $types = [ |
|
| 150 | + 'mimetype' => 'string', |
|
| 151 | + 'mtime' => 'integer', |
|
| 152 | + 'name' => 'string', |
|
| 153 | + 'size' => 'integer', |
|
| 154 | + 'tagname' => 'string', |
|
| 155 | + 'favorite' => 'boolean', |
|
| 156 | + 'fileid' => 'integer' |
|
| 157 | + ]; |
|
| 158 | + $comparisons = [ |
|
| 159 | + 'mimetype' => ['eq', 'like'], |
|
| 160 | + 'mtime' => ['eq', 'gt', 'lt', 'gte', 'lte'], |
|
| 161 | + 'name' => ['eq', 'like'], |
|
| 162 | + 'size' => ['eq', 'gt', 'lt', 'gte', 'lte'], |
|
| 163 | + 'tagname' => ['eq', 'like'], |
|
| 164 | + 'favorite' => ['eq'], |
|
| 165 | + 'fileid' => ['eq'] |
|
| 166 | + ]; |
|
| 167 | + |
|
| 168 | + if (!isset($types[$operator->getField()])) { |
|
| 169 | + throw new \InvalidArgumentException('Unsupported comparison field ' . $operator->getField()); |
|
| 170 | + } |
|
| 171 | + $type = $types[$operator->getField()]; |
|
| 172 | + if (gettype($operator->getValue()) !== $type) { |
|
| 173 | + throw new \InvalidArgumentException('Invalid type for field ' . $operator->getField()); |
|
| 174 | + } |
|
| 175 | + if (!in_array($operator->getType(), $comparisons[$operator->getField()])) { |
|
| 176 | + throw new \InvalidArgumentException('Unsupported comparison for field ' . $operator->getField() . ': ' . $operator->getType()); |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + private function getParameterForValue(IQueryBuilder $builder, $value) { |
|
| 181 | + if ($value instanceof \DateTime) { |
|
| 182 | + $value = $value->getTimestamp(); |
|
| 183 | + } |
|
| 184 | + if (is_numeric($value)) { |
|
| 185 | + $type = IQueryBuilder::PARAM_INT; |
|
| 186 | + } else { |
|
| 187 | + $type = IQueryBuilder::PARAM_STR; |
|
| 188 | + } |
|
| 189 | + return $builder->createNamedParameter($value, $type); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * @param IQueryBuilder $query |
|
| 194 | + * @param ISearchOrder[] $orders |
|
| 195 | + */ |
|
| 196 | + public function addSearchOrdersToQuery(IQueryBuilder $query, array $orders) { |
|
| 197 | + foreach ($orders as $order) { |
|
| 198 | + $query->addOrderBy($order->getField(), $order->getDirection()); |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | 201 | } |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | /** @var Folder $folder $results */ |
| 156 | 156 | $results = $folder->search($query); |
| 157 | 157 | |
| 158 | - return array_map(function (Node $node) { |
|
| 158 | + return array_map(function(Node $node) { |
|
| 159 | 159 | if ($node instanceof Folder) { |
| 160 | 160 | return new SearchResult(new \OCA\DAV\Connector\Sabre\Directory($this->view, $node, $this->tree, $this->shareManager), $this->getHrefForNode($node)); |
| 161 | 161 | } else { |
@@ -169,8 +169,8 @@ discard block |
||
| 169 | 169 | * @return string |
| 170 | 170 | */ |
| 171 | 171 | private function getHrefForNode(Node $node) { |
| 172 | - $base = '/files/' . $this->user->getUID(); |
|
| 173 | - return $base . $this->view->getRelativePath($node->getPath()); |
|
| 172 | + $base = '/files/'.$this->user->getUID(); |
|
| 173 | + return $base.$this->view->getRelativePath($node->getPath()); |
|
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | /** |
@@ -210,19 +210,19 @@ discard block |
||
| 210 | 210 | case Operator::OPERATION_LESS_THAN: |
| 211 | 211 | case Operator::OPERATION_IS_LIKE: |
| 212 | 212 | if (count($operator->arguments) !== 2) { |
| 213 | - throw new \InvalidArgumentException('Invalid number of arguments for ' . $trimmedType . ' operation'); |
|
| 213 | + throw new \InvalidArgumentException('Invalid number of arguments for '.$trimmedType.' operation'); |
|
| 214 | 214 | } |
| 215 | 215 | if (!is_string($operator->arguments[0])) { |
| 216 | - throw new \InvalidArgumentException('Invalid argument 1 for ' . $trimmedType . ' operation, expected property'); |
|
| 216 | + throw new \InvalidArgumentException('Invalid argument 1 for '.$trimmedType.' operation, expected property'); |
|
| 217 | 217 | } |
| 218 | 218 | if (!($operator->arguments[1] instanceof Literal)) { |
| 219 | - throw new \InvalidArgumentException('Invalid argument 2 for ' . $trimmedType . ' operation, expected literal'); |
|
| 219 | + throw new \InvalidArgumentException('Invalid argument 2 for '.$trimmedType.' operation, expected literal'); |
|
| 220 | 220 | } |
| 221 | 221 | return new SearchComparison($trimmedType, $this->mapPropertyNameToColumn($operator->arguments[0]), $this->castValue($operator->arguments[0], $operator->arguments[1]->value)); |
| 222 | 222 | case Operator::OPERATION_IS_COLLECTION: |
| 223 | 223 | return new SearchComparison('eq', 'mimetype', ICacheEntry::DIRECTORY_MIMETYPE); |
| 224 | 224 | default: |
| 225 | - throw new \InvalidArgumentException('Unsupported operation ' . $trimmedType . ' (' . $operator->type . ')'); |
|
| 225 | + throw new \InvalidArgumentException('Unsupported operation '.$trimmedType.' ('.$operator->type.')'); |
|
| 226 | 226 | } |
| 227 | 227 | } |
| 228 | 228 | |
@@ -247,7 +247,7 @@ discard block |
||
| 247 | 247 | case FilesPlugin::INTERNAL_FILEID_PROPERTYNAME: |
| 248 | 248 | return 'fileid'; |
| 249 | 249 | default: |
| 250 | - throw new \InvalidArgumentException('Unsupported property for search or order: ' . $propertyName); |
|
| 250 | + throw new \InvalidArgumentException('Unsupported property for search or order: '.$propertyName); |
|
| 251 | 251 | } |
| 252 | 252 | } |
| 253 | 253 | |
@@ -49,231 +49,231 @@ |
||
| 49 | 49 | use SearchDAV\XML\Order; |
| 50 | 50 | |
| 51 | 51 | class FileSearchBackend implements ISearchBackend { |
| 52 | - /** @var Tree */ |
|
| 53 | - private $tree; |
|
| 52 | + /** @var Tree */ |
|
| 53 | + private $tree; |
|
| 54 | 54 | |
| 55 | - /** @var IUser */ |
|
| 56 | - private $user; |
|
| 55 | + /** @var IUser */ |
|
| 56 | + private $user; |
|
| 57 | 57 | |
| 58 | - /** @var IRootFolder */ |
|
| 59 | - private $rootFolder; |
|
| 58 | + /** @var IRootFolder */ |
|
| 59 | + private $rootFolder; |
|
| 60 | 60 | |
| 61 | - /** @var IManager */ |
|
| 62 | - private $shareManager; |
|
| 61 | + /** @var IManager */ |
|
| 62 | + private $shareManager; |
|
| 63 | 63 | |
| 64 | - /** @var View */ |
|
| 65 | - private $view; |
|
| 64 | + /** @var View */ |
|
| 65 | + private $view; |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * FileSearchBackend constructor. |
|
| 69 | - * |
|
| 70 | - * @param Tree $tree |
|
| 71 | - * @param IUser $user |
|
| 72 | - * @param IRootFolder $rootFolder |
|
| 73 | - * @param IManager $shareManager |
|
| 74 | - * @param View $view |
|
| 75 | - * @internal param IRootFolder $rootFolder |
|
| 76 | - */ |
|
| 77 | - public function __construct(Tree $tree, IUser $user, IRootFolder $rootFolder, IManager $shareManager, View $view) { |
|
| 78 | - $this->tree = $tree; |
|
| 79 | - $this->user = $user; |
|
| 80 | - $this->rootFolder = $rootFolder; |
|
| 81 | - $this->shareManager = $shareManager; |
|
| 82 | - $this->view = $view; |
|
| 83 | - } |
|
| 67 | + /** |
|
| 68 | + * FileSearchBackend constructor. |
|
| 69 | + * |
|
| 70 | + * @param Tree $tree |
|
| 71 | + * @param IUser $user |
|
| 72 | + * @param IRootFolder $rootFolder |
|
| 73 | + * @param IManager $shareManager |
|
| 74 | + * @param View $view |
|
| 75 | + * @internal param IRootFolder $rootFolder |
|
| 76 | + */ |
|
| 77 | + public function __construct(Tree $tree, IUser $user, IRootFolder $rootFolder, IManager $shareManager, View $view) { |
|
| 78 | + $this->tree = $tree; |
|
| 79 | + $this->user = $user; |
|
| 80 | + $this->rootFolder = $rootFolder; |
|
| 81 | + $this->shareManager = $shareManager; |
|
| 82 | + $this->view = $view; |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Search endpoint will be remote.php/dav |
|
| 87 | - * |
|
| 88 | - * @return string |
|
| 89 | - */ |
|
| 90 | - public function getArbiterPath() { |
|
| 91 | - return ''; |
|
| 92 | - } |
|
| 85 | + /** |
|
| 86 | + * Search endpoint will be remote.php/dav |
|
| 87 | + * |
|
| 88 | + * @return string |
|
| 89 | + */ |
|
| 90 | + public function getArbiterPath() { |
|
| 91 | + return ''; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - public function isValidScope($href, $depth, $path) { |
|
| 95 | - // only allow scopes inside the dav server |
|
| 96 | - if (is_null($path)) { |
|
| 97 | - return false; |
|
| 98 | - } |
|
| 94 | + public function isValidScope($href, $depth, $path) { |
|
| 95 | + // only allow scopes inside the dav server |
|
| 96 | + if (is_null($path)) { |
|
| 97 | + return false; |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - try { |
|
| 101 | - $node = $this->tree->getNodeForPath($path); |
|
| 102 | - return $node instanceof Directory; |
|
| 103 | - } catch (NotFound $e) { |
|
| 104 | - return false; |
|
| 105 | - } |
|
| 106 | - } |
|
| 100 | + try { |
|
| 101 | + $node = $this->tree->getNodeForPath($path); |
|
| 102 | + return $node instanceof Directory; |
|
| 103 | + } catch (NotFound $e) { |
|
| 104 | + return false; |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - public function getPropertyDefinitionsForScope($href, $path) { |
|
| 109 | - // all valid scopes support the same schema |
|
| 108 | + public function getPropertyDefinitionsForScope($href, $path) { |
|
| 109 | + // all valid scopes support the same schema |
|
| 110 | 110 | |
| 111 | - //todo dynamically load all propfind properties that are supported |
|
| 112 | - return [ |
|
| 113 | - // queryable properties |
|
| 114 | - new SearchPropertyDefinition('{DAV:}displayname', true, false, true), |
|
| 115 | - new SearchPropertyDefinition('{DAV:}getcontenttype', true, true, true), |
|
| 116 | - new SearchPropertyDefinition('{DAV:}getlastmodified', true, true, true, SearchPropertyDefinition::DATATYPE_DATETIME), |
|
| 117 | - new SearchPropertyDefinition(FilesPlugin::SIZE_PROPERTYNAME, true, true, true, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER), |
|
| 118 | - new SearchPropertyDefinition(TagsPlugin::FAVORITE_PROPERTYNAME, true, true, true, SearchPropertyDefinition::DATATYPE_BOOLEAN), |
|
| 119 | - new SearchPropertyDefinition(FilesPlugin::INTERNAL_FILEID_PROPERTYNAME, true, true, false, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER), |
|
| 111 | + //todo dynamically load all propfind properties that are supported |
|
| 112 | + return [ |
|
| 113 | + // queryable properties |
|
| 114 | + new SearchPropertyDefinition('{DAV:}displayname', true, false, true), |
|
| 115 | + new SearchPropertyDefinition('{DAV:}getcontenttype', true, true, true), |
|
| 116 | + new SearchPropertyDefinition('{DAV:}getlastmodified', true, true, true, SearchPropertyDefinition::DATATYPE_DATETIME), |
|
| 117 | + new SearchPropertyDefinition(FilesPlugin::SIZE_PROPERTYNAME, true, true, true, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER), |
|
| 118 | + new SearchPropertyDefinition(TagsPlugin::FAVORITE_PROPERTYNAME, true, true, true, SearchPropertyDefinition::DATATYPE_BOOLEAN), |
|
| 119 | + new SearchPropertyDefinition(FilesPlugin::INTERNAL_FILEID_PROPERTYNAME, true, true, false, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER), |
|
| 120 | 120 | |
| 121 | - // select only properties |
|
| 122 | - new SearchPropertyDefinition('{DAV:}resourcetype', false, true, false), |
|
| 123 | - new SearchPropertyDefinition('{DAV:}getcontentlength', false, true, false), |
|
| 124 | - new SearchPropertyDefinition(FilesPlugin::CHECKSUMS_PROPERTYNAME, false, true, false), |
|
| 125 | - new SearchPropertyDefinition(FilesPlugin::PERMISSIONS_PROPERTYNAME, false, true, false), |
|
| 126 | - new SearchPropertyDefinition(FilesPlugin::GETETAG_PROPERTYNAME, false, true, false), |
|
| 127 | - new SearchPropertyDefinition(FilesPlugin::OWNER_ID_PROPERTYNAME, false, true, false), |
|
| 128 | - new SearchPropertyDefinition(FilesPlugin::OWNER_DISPLAY_NAME_PROPERTYNAME, false, true, false), |
|
| 129 | - new SearchPropertyDefinition(FilesPlugin::DATA_FINGERPRINT_PROPERTYNAME, false, true, false), |
|
| 130 | - new SearchPropertyDefinition(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, false, true, false, SearchPropertyDefinition::DATATYPE_BOOLEAN), |
|
| 131 | - new SearchPropertyDefinition(FilesPlugin::FILEID_PROPERTYNAME, false, true, false, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER), |
|
| 132 | - ]; |
|
| 133 | - } |
|
| 121 | + // select only properties |
|
| 122 | + new SearchPropertyDefinition('{DAV:}resourcetype', false, true, false), |
|
| 123 | + new SearchPropertyDefinition('{DAV:}getcontentlength', false, true, false), |
|
| 124 | + new SearchPropertyDefinition(FilesPlugin::CHECKSUMS_PROPERTYNAME, false, true, false), |
|
| 125 | + new SearchPropertyDefinition(FilesPlugin::PERMISSIONS_PROPERTYNAME, false, true, false), |
|
| 126 | + new SearchPropertyDefinition(FilesPlugin::GETETAG_PROPERTYNAME, false, true, false), |
|
| 127 | + new SearchPropertyDefinition(FilesPlugin::OWNER_ID_PROPERTYNAME, false, true, false), |
|
| 128 | + new SearchPropertyDefinition(FilesPlugin::OWNER_DISPLAY_NAME_PROPERTYNAME, false, true, false), |
|
| 129 | + new SearchPropertyDefinition(FilesPlugin::DATA_FINGERPRINT_PROPERTYNAME, false, true, false), |
|
| 130 | + new SearchPropertyDefinition(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, false, true, false, SearchPropertyDefinition::DATATYPE_BOOLEAN), |
|
| 131 | + new SearchPropertyDefinition(FilesPlugin::FILEID_PROPERTYNAME, false, true, false, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER), |
|
| 132 | + ]; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * @param BasicSearch $search |
|
| 137 | - * @return SearchResult[] |
|
| 138 | - */ |
|
| 139 | - public function search(BasicSearch $search) { |
|
| 140 | - if (count($search->from) !== 1) { |
|
| 141 | - throw new \InvalidArgumentException('Searching more than one folder is not supported'); |
|
| 142 | - } |
|
| 143 | - $query = $this->transformQuery($search); |
|
| 144 | - $scope = $search->from[0]; |
|
| 145 | - if ($scope->path === null) { |
|
| 146 | - throw new \InvalidArgumentException('Using uri\'s as scope is not supported, please use a path relative to the search arbiter instead'); |
|
| 147 | - } |
|
| 148 | - $node = $this->tree->getNodeForPath($scope->path); |
|
| 149 | - if (!$node instanceof Directory) { |
|
| 150 | - throw new \InvalidArgumentException('Search is only supported on directories'); |
|
| 151 | - } |
|
| 135 | + /** |
|
| 136 | + * @param BasicSearch $search |
|
| 137 | + * @return SearchResult[] |
|
| 138 | + */ |
|
| 139 | + public function search(BasicSearch $search) { |
|
| 140 | + if (count($search->from) !== 1) { |
|
| 141 | + throw new \InvalidArgumentException('Searching more than one folder is not supported'); |
|
| 142 | + } |
|
| 143 | + $query = $this->transformQuery($search); |
|
| 144 | + $scope = $search->from[0]; |
|
| 145 | + if ($scope->path === null) { |
|
| 146 | + throw new \InvalidArgumentException('Using uri\'s as scope is not supported, please use a path relative to the search arbiter instead'); |
|
| 147 | + } |
|
| 148 | + $node = $this->tree->getNodeForPath($scope->path); |
|
| 149 | + if (!$node instanceof Directory) { |
|
| 150 | + throw new \InvalidArgumentException('Search is only supported on directories'); |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - $fileInfo = $node->getFileInfo(); |
|
| 154 | - $folder = $this->rootFolder->get($fileInfo->getPath()); |
|
| 155 | - /** @var Folder $folder $results */ |
|
| 156 | - $results = $folder->search($query); |
|
| 153 | + $fileInfo = $node->getFileInfo(); |
|
| 154 | + $folder = $this->rootFolder->get($fileInfo->getPath()); |
|
| 155 | + /** @var Folder $folder $results */ |
|
| 156 | + $results = $folder->search($query); |
|
| 157 | 157 | |
| 158 | - return array_map(function (Node $node) { |
|
| 159 | - if ($node instanceof Folder) { |
|
| 160 | - return new SearchResult(new \OCA\DAV\Connector\Sabre\Directory($this->view, $node, $this->tree, $this->shareManager), $this->getHrefForNode($node)); |
|
| 161 | - } else { |
|
| 162 | - return new SearchResult(new \OCA\DAV\Connector\Sabre\File($this->view, $node, $this->shareManager), $this->getHrefForNode($node)); |
|
| 163 | - } |
|
| 164 | - }, $results); |
|
| 165 | - } |
|
| 158 | + return array_map(function (Node $node) { |
|
| 159 | + if ($node instanceof Folder) { |
|
| 160 | + return new SearchResult(new \OCA\DAV\Connector\Sabre\Directory($this->view, $node, $this->tree, $this->shareManager), $this->getHrefForNode($node)); |
|
| 161 | + } else { |
|
| 162 | + return new SearchResult(new \OCA\DAV\Connector\Sabre\File($this->view, $node, $this->shareManager), $this->getHrefForNode($node)); |
|
| 163 | + } |
|
| 164 | + }, $results); |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * @param Node $node |
|
| 169 | - * @return string |
|
| 170 | - */ |
|
| 171 | - private function getHrefForNode(Node $node) { |
|
| 172 | - $base = '/files/' . $this->user->getUID(); |
|
| 173 | - return $base . $this->view->getRelativePath($node->getPath()); |
|
| 174 | - } |
|
| 167 | + /** |
|
| 168 | + * @param Node $node |
|
| 169 | + * @return string |
|
| 170 | + */ |
|
| 171 | + private function getHrefForNode(Node $node) { |
|
| 172 | + $base = '/files/' . $this->user->getUID(); |
|
| 173 | + return $base . $this->view->getRelativePath($node->getPath()); |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | - /** |
|
| 177 | - * @param BasicSearch $query |
|
| 178 | - * @return ISearchQuery |
|
| 179 | - */ |
|
| 180 | - private function transformQuery(BasicSearch $query) { |
|
| 181 | - // TODO offset, limit |
|
| 182 | - $orders = array_map([$this, 'mapSearchOrder'], $query->orderBy); |
|
| 183 | - return new SearchQuery($this->transformSearchOperation($query->where), 0, 0, $orders, $this->user); |
|
| 184 | - } |
|
| 176 | + /** |
|
| 177 | + * @param BasicSearch $query |
|
| 178 | + * @return ISearchQuery |
|
| 179 | + */ |
|
| 180 | + private function transformQuery(BasicSearch $query) { |
|
| 181 | + // TODO offset, limit |
|
| 182 | + $orders = array_map([$this, 'mapSearchOrder'], $query->orderBy); |
|
| 183 | + return new SearchQuery($this->transformSearchOperation($query->where), 0, 0, $orders, $this->user); |
|
| 184 | + } |
|
| 185 | 185 | |
| 186 | - /** |
|
| 187 | - * @param Order $order |
|
| 188 | - * @return ISearchOrder |
|
| 189 | - */ |
|
| 190 | - private function mapSearchOrder(Order $order) { |
|
| 191 | - return new SearchOrder($order->order === Order::ASC ? ISearchOrder::DIRECTION_ASCENDING : ISearchOrder::DIRECTION_DESCENDING, $this->mapPropertyNameToColumn($order->property)); |
|
| 192 | - } |
|
| 186 | + /** |
|
| 187 | + * @param Order $order |
|
| 188 | + * @return ISearchOrder |
|
| 189 | + */ |
|
| 190 | + private function mapSearchOrder(Order $order) { |
|
| 191 | + return new SearchOrder($order->order === Order::ASC ? ISearchOrder::DIRECTION_ASCENDING : ISearchOrder::DIRECTION_DESCENDING, $this->mapPropertyNameToColumn($order->property)); |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - /** |
|
| 195 | - * @param Operator $operator |
|
| 196 | - * @return ISearchOperator |
|
| 197 | - */ |
|
| 198 | - private function transformSearchOperation(Operator $operator) { |
|
| 199 | - list(, $trimmedType) = explode('}', $operator->type); |
|
| 200 | - switch ($operator->type) { |
|
| 201 | - case Operator::OPERATION_AND: |
|
| 202 | - case Operator::OPERATION_OR: |
|
| 203 | - case Operator::OPERATION_NOT: |
|
| 204 | - $arguments = array_map([$this, 'transformSearchOperation'], $operator->arguments); |
|
| 205 | - return new SearchBinaryOperator($trimmedType, $arguments); |
|
| 206 | - case Operator::OPERATION_EQUAL: |
|
| 207 | - case Operator::OPERATION_GREATER_OR_EQUAL_THAN: |
|
| 208 | - case Operator::OPERATION_GREATER_THAN: |
|
| 209 | - case Operator::OPERATION_LESS_OR_EQUAL_THAN: |
|
| 210 | - case Operator::OPERATION_LESS_THAN: |
|
| 211 | - case Operator::OPERATION_IS_LIKE: |
|
| 212 | - if (count($operator->arguments) !== 2) { |
|
| 213 | - throw new \InvalidArgumentException('Invalid number of arguments for ' . $trimmedType . ' operation'); |
|
| 214 | - } |
|
| 215 | - if (!is_string($operator->arguments[0])) { |
|
| 216 | - throw new \InvalidArgumentException('Invalid argument 1 for ' . $trimmedType . ' operation, expected property'); |
|
| 217 | - } |
|
| 218 | - if (!($operator->arguments[1] instanceof Literal)) { |
|
| 219 | - throw new \InvalidArgumentException('Invalid argument 2 for ' . $trimmedType . ' operation, expected literal'); |
|
| 220 | - } |
|
| 221 | - return new SearchComparison($trimmedType, $this->mapPropertyNameToColumn($operator->arguments[0]), $this->castValue($operator->arguments[0], $operator->arguments[1]->value)); |
|
| 222 | - case Operator::OPERATION_IS_COLLECTION: |
|
| 223 | - return new SearchComparison('eq', 'mimetype', ICacheEntry::DIRECTORY_MIMETYPE); |
|
| 224 | - default: |
|
| 225 | - throw new \InvalidArgumentException('Unsupported operation ' . $trimmedType . ' (' . $operator->type . ')'); |
|
| 226 | - } |
|
| 227 | - } |
|
| 194 | + /** |
|
| 195 | + * @param Operator $operator |
|
| 196 | + * @return ISearchOperator |
|
| 197 | + */ |
|
| 198 | + private function transformSearchOperation(Operator $operator) { |
|
| 199 | + list(, $trimmedType) = explode('}', $operator->type); |
|
| 200 | + switch ($operator->type) { |
|
| 201 | + case Operator::OPERATION_AND: |
|
| 202 | + case Operator::OPERATION_OR: |
|
| 203 | + case Operator::OPERATION_NOT: |
|
| 204 | + $arguments = array_map([$this, 'transformSearchOperation'], $operator->arguments); |
|
| 205 | + return new SearchBinaryOperator($trimmedType, $arguments); |
|
| 206 | + case Operator::OPERATION_EQUAL: |
|
| 207 | + case Operator::OPERATION_GREATER_OR_EQUAL_THAN: |
|
| 208 | + case Operator::OPERATION_GREATER_THAN: |
|
| 209 | + case Operator::OPERATION_LESS_OR_EQUAL_THAN: |
|
| 210 | + case Operator::OPERATION_LESS_THAN: |
|
| 211 | + case Operator::OPERATION_IS_LIKE: |
|
| 212 | + if (count($operator->arguments) !== 2) { |
|
| 213 | + throw new \InvalidArgumentException('Invalid number of arguments for ' . $trimmedType . ' operation'); |
|
| 214 | + } |
|
| 215 | + if (!is_string($operator->arguments[0])) { |
|
| 216 | + throw new \InvalidArgumentException('Invalid argument 1 for ' . $trimmedType . ' operation, expected property'); |
|
| 217 | + } |
|
| 218 | + if (!($operator->arguments[1] instanceof Literal)) { |
|
| 219 | + throw new \InvalidArgumentException('Invalid argument 2 for ' . $trimmedType . ' operation, expected literal'); |
|
| 220 | + } |
|
| 221 | + return new SearchComparison($trimmedType, $this->mapPropertyNameToColumn($operator->arguments[0]), $this->castValue($operator->arguments[0], $operator->arguments[1]->value)); |
|
| 222 | + case Operator::OPERATION_IS_COLLECTION: |
|
| 223 | + return new SearchComparison('eq', 'mimetype', ICacheEntry::DIRECTORY_MIMETYPE); |
|
| 224 | + default: |
|
| 225 | + throw new \InvalidArgumentException('Unsupported operation ' . $trimmedType . ' (' . $operator->type . ')'); |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | - /** |
|
| 230 | - * @param string $propertyName |
|
| 231 | - * @return string |
|
| 232 | - */ |
|
| 233 | - private function mapPropertyNameToColumn($propertyName) { |
|
| 234 | - switch ($propertyName) { |
|
| 235 | - case '{DAV:}displayname': |
|
| 236 | - return 'name'; |
|
| 237 | - case '{DAV:}getcontenttype': |
|
| 238 | - return 'mimetype'; |
|
| 239 | - case '{DAV:}getlastmodified': |
|
| 240 | - return 'mtime'; |
|
| 241 | - case FilesPlugin::SIZE_PROPERTYNAME: |
|
| 242 | - return 'size'; |
|
| 243 | - case TagsPlugin::FAVORITE_PROPERTYNAME: |
|
| 244 | - return 'favorite'; |
|
| 245 | - case TagsPlugin::TAGS_PROPERTYNAME: |
|
| 246 | - return 'tagname'; |
|
| 247 | - case FilesPlugin::INTERNAL_FILEID_PROPERTYNAME: |
|
| 248 | - return 'fileid'; |
|
| 249 | - default: |
|
| 250 | - throw new \InvalidArgumentException('Unsupported property for search or order: ' . $propertyName); |
|
| 251 | - } |
|
| 252 | - } |
|
| 229 | + /** |
|
| 230 | + * @param string $propertyName |
|
| 231 | + * @return string |
|
| 232 | + */ |
|
| 233 | + private function mapPropertyNameToColumn($propertyName) { |
|
| 234 | + switch ($propertyName) { |
|
| 235 | + case '{DAV:}displayname': |
|
| 236 | + return 'name'; |
|
| 237 | + case '{DAV:}getcontenttype': |
|
| 238 | + return 'mimetype'; |
|
| 239 | + case '{DAV:}getlastmodified': |
|
| 240 | + return 'mtime'; |
|
| 241 | + case FilesPlugin::SIZE_PROPERTYNAME: |
|
| 242 | + return 'size'; |
|
| 243 | + case TagsPlugin::FAVORITE_PROPERTYNAME: |
|
| 244 | + return 'favorite'; |
|
| 245 | + case TagsPlugin::TAGS_PROPERTYNAME: |
|
| 246 | + return 'tagname'; |
|
| 247 | + case FilesPlugin::INTERNAL_FILEID_PROPERTYNAME: |
|
| 248 | + return 'fileid'; |
|
| 249 | + default: |
|
| 250 | + throw new \InvalidArgumentException('Unsupported property for search or order: ' . $propertyName); |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | - private function castValue($propertyName, $value) { |
|
| 255 | - $allProps = $this->getPropertyDefinitionsForScope('', ''); |
|
| 256 | - foreach ($allProps as $prop) { |
|
| 257 | - if ($prop->name === $propertyName) { |
|
| 258 | - $dataType = $prop->dataType; |
|
| 259 | - switch ($dataType) { |
|
| 260 | - case SearchPropertyDefinition::DATATYPE_BOOLEAN: |
|
| 261 | - return $value === 'yes'; |
|
| 262 | - case SearchPropertyDefinition::DATATYPE_DECIMAL: |
|
| 263 | - case SearchPropertyDefinition::DATATYPE_INTEGER: |
|
| 264 | - case SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER: |
|
| 265 | - return 0 + $value; |
|
| 266 | - case SearchPropertyDefinition::DATATYPE_DATETIME: |
|
| 267 | - if (is_numeric($value)) { |
|
| 268 | - return 0 + $value; |
|
| 269 | - } |
|
| 270 | - $date = \DateTime::createFromFormat(\DateTime::ATOM, $value); |
|
| 271 | - return ($date instanceof \DateTime) ? $date->getTimestamp() : 0; |
|
| 272 | - default: |
|
| 273 | - return $value; |
|
| 274 | - } |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - return $value; |
|
| 278 | - } |
|
| 254 | + private function castValue($propertyName, $value) { |
|
| 255 | + $allProps = $this->getPropertyDefinitionsForScope('', ''); |
|
| 256 | + foreach ($allProps as $prop) { |
|
| 257 | + if ($prop->name === $propertyName) { |
|
| 258 | + $dataType = $prop->dataType; |
|
| 259 | + switch ($dataType) { |
|
| 260 | + case SearchPropertyDefinition::DATATYPE_BOOLEAN: |
|
| 261 | + return $value === 'yes'; |
|
| 262 | + case SearchPropertyDefinition::DATATYPE_DECIMAL: |
|
| 263 | + case SearchPropertyDefinition::DATATYPE_INTEGER: |
|
| 264 | + case SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER: |
|
| 265 | + return 0 + $value; |
|
| 266 | + case SearchPropertyDefinition::DATATYPE_DATETIME: |
|
| 267 | + if (is_numeric($value)) { |
|
| 268 | + return 0 + $value; |
|
| 269 | + } |
|
| 270 | + $date = \DateTime::createFromFormat(\DateTime::ATOM, $value); |
|
| 271 | + return ($date instanceof \DateTime) ? $date->getTimestamp() : 0; |
|
| 272 | + default: |
|
| 273 | + return $value; |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + return $value; |
|
| 278 | + } |
|
| 279 | 279 | } |