NSString+XLFormAdditions.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // NSString+XLFormAdditions.m
  3. // XLForm ( https://github.com/xmartlabs/XLForm )
  4. //
  5. // Copyright (c) 2015 Xmartlabs ( http://xmartlabs.com )
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. #import "NSString+XLFormAdditions.h"
  26. @implementation NSString (XLFormAdditions)
  27. -(NSPredicate *)formPredicate
  28. {
  29. // returns an array of strings where the first one is the new string with the correct replacements
  30. // and the rest are all the tags that appear in the string
  31. NSString* separator = @"$";
  32. NSArray* tokens = [self componentsSeparatedByString:separator];
  33. NSMutableString* new_string = [[NSMutableString alloc] initWithString:tokens[0]];
  34. NSRange range;
  35. for (int i = 1; i < tokens.count; i++) {
  36. [new_string appendString:separator];
  37. NSArray* subtokens = [[tokens[i] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" <>!=+-&|()"]][0]
  38. componentsSeparatedByString:@"."];
  39. NSString* tag = subtokens[0];
  40. NSString* attribute;
  41. if ([subtokens count] >= 2) {
  42. attribute = subtokens[1];
  43. }
  44. [new_string appendString:tag];
  45. range = [tokens[i] rangeOfString:[NSString stringWithFormat:@"%@", tag]];
  46. if (!attribute || (![attribute isEqualToString:@"value"] && ![attribute isEqualToString:@"isHidden"] && ![attribute isEqualToString:@"isDisabled"])){
  47. [new_string appendString:@".value"];
  48. }
  49. [new_string appendString:[tokens[i] substringFromIndex:range.location + range.length]];
  50. }
  51. return [NSPredicate predicateWithFormat:new_string];
  52. }
  53. -(NSString *)formKeyForPredicateType:(XLPredicateType)predicateType
  54. {
  55. return [NSString stringWithFormat:@"%@-%@", self, (predicateType == XLPredicateTypeHidden ? @"hidden" : @"disabled") ];
  56. }
  57. @end