Completed
Branch master (a85611)
by Rasmus
01:49
created

ValidContent::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.037

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 6
cp 0.6667
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1.037
1
<?php
2
namespace Chp\TextContent;
3
4 1
include_once(__DIR__ . '/../../app/config/text-content.php');
5
6
/**
7
 * Validate and valid help class for TextContent
8
 * Made by Rasmus Berg (c) 2014-2017
9
 *
10
 * @property  object  $url  Anax-MVC url-handler class
11
 */
12
class ValidContent implements \Anax\DI\IInjectionAware
13
{
14
  use \Anax\DI\TInjectable;
15
  
16
  private $miniLength; // Minimum length on text-fields (ex. ingress, title etc)
17
  private $urlPrefix;
18
  private $filters;
19
  private $types;
20
  
21
  /**
22
   * Initialize the controller
23
   *
24
   * @return    void
25
   */
26 16
  public function initialize(){
27 16
    $this->miniLength = CHP_TC_MINILENGTH; // Minimum length on text-fields (ex. ingress, title etc)
28 16
    $this->urlPrefix  = CHP_TC_URLPREFIX;
29
    $this->filters = unserialize(CHP_TC_FILTERS);
30
    $this->types = unserialize(CHP_TC_TYPES);
31 16
  }
32
  
33
  /**
34
	 * Create a link to the content, based on its type.
35
	 *
36
	 * @param  	object  	$content	Content to link to
37
	 * @return 	string    	   	 	  With url for content
38
	 */
39 1
	public function getUrlToContent($content) {
40
    if(isset($this->types[$content->type])){
41 1
      $type = $this->types[$content->type]; // Get type from type index
42
	  
43
      return $this->url->create("{$this->urlPrefix}{$type['url']}{$type['prefix']}{$content->{$type['field']}}");
44
    }
45
    
46 1
    return null;
47
	}
48
	
49
  /**
50
	 * Return array with all content types title and keys (Use for content-type select) 
51
	 *
52
	 * @return		array		$types	Array with the types title and keys
53
	 */
54 1
	public function getTypes(){
55 1
    $types = array();
56
    
57
		// Loop through and save types key as key and title as value in a new array
58 1
		foreach($this->types AS $key => $value){
59
			$types[$key] = $value['title'];
60
		}
61
		
62 1
		return $types;
63
	}
64
  
65
  /**
66
	 * Return array with all filters 
67
	 *
68
	 * @return		array		$this->filters	Array with the filters
69
	 */
70 1
  public function getFilters(){
71 1
    return $this->filters; 
72
  }
73
  
74
	/**
75
	 * Return name of one specific type
76
	 *
77
	 * @params  string  $type                        Type key
78
	 * @return  string  $this->types[{key}]['title'] Type title
79
	 */
80
	public function getTypeTitle($type){
81
		return $this->types[$type]['title'];
82
	}
83
  
84
  /**
85
	 * Create a slug of a string, to be used as url.
86
	 *
87
	 * @param   string   $str  String to format as slug.
88
	 * @return  string   $str  Formatted slug. 
89
	 */
90
	public function slugify($str) {
91
	  $str = mb_strtolower(trim($str));
92
		
93
		$str = str_replace(array("å","ä","ö"), array("a","a","o"), utf8_decode(utf8_encode($str)));
94
		
95
	  $str = preg_replace('/[^a-z0-9-_()]/', '_', $str);
96
	  $str = trim(preg_replace('/_+/', '_', $str), '_');
97
    
98
	  return $str;
99
	}
100
  
101
  /**
102
   * Check if content is published
103
   *
104
   * @param   string      $datetime     When it will be published
105
   * @return  boolean     True/false    Validate result
106
   */
107
  public function checkIfAvailable($datetime){
108
    return ($datetime <= date('Y-m-d H:i:s')) ? true : false;
109
  }
110
  
111
	/**
112
	 * Check so the choosed type exist.
113
	 *
114
	 * @param   	string		$type		    Choosed type on content
115
	 * @return 	  boolean   True/false  Validate result
116
	 */
117
	public function checkType($type = ''){
118
		return isset($this->types[$type]);
119
	}
120
  
121
  /**
122
   * Validate posted datetime so it is correct
123
   *
124
   * @param   string    $datetime      Posted datetime to check  
125
   * @return  boolean   True/false     Validate status
126
   */
127 2
  public function checkDatetime($datetime){
128
    if(isset($datetime) && !empty($datetime)){
129
      $format = 'Y-m-d H:i:s';
130
      $d = \DateTime::createFromFormat($format, $datetime);
131
      return $d && $d->format($format) == $datetime;
132
    }
133 2
    return true;
134
  }
135
  
136
  /**
137
   * Minimum length
138
   *
139
   * @param   string    $value        Value from form-element to validate
140
   * @return  boolean   True/false    Validate result
141
   */
142
  public function minimumLength($value){
143
    return (strlen($value) >= $this->miniLength);
144
  }
145
  
146
  /**
147
   * Validate slug url
148
   *
149
   * @param   string    $url          Url to validate
150
   * @return  boolean   True/false    True if valid otherwish false
151
   */
152
  public function validateSlug($url){
153
    return ($this->slugify($url) == $url);
154
  }
155
  
156
  /**
157
   * Check so the selected filter exist.
158
   *
159
   * @param     string 	  $filter   Selected filter
160
   * @return    boolean   $result   Return the result of test
161
   */
162 2
  public function checkFilter($filter = null){
163
    if(!empty($filter)){
164
      // For each filter, check if the filter exist
165
      foreach($this->filters as $val){
166
        if($val == $filter)
167 2
          return true;
168
        }
169 2
        return false;
170
      }
171 2
    return true;
172
  }
173
}