Completed
Push — master ( 2387f6...83bda3 )
by Rasmus
02:08
created

ValidContent::minimumLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 16
    $this->filters = unserialize(CHP_TC_FILTERS);
30 16
    $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 2
	public function getUrlToContent($content) {
40 2
    if(isset($this->types[$content->type])){
41 2
      $type = $this->types[$content->type]; // Get type from type index
42
	  
43 2
      return $this->url->create($this->urlPrefix . "{$type['url']}{$type['prefix']}{$content->{$type['field']}}");
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<Chp\TextContent\ValidContent>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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 1
			$types[$key] = $value['title'];
60 1
		}
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        Type title
79
	 */
80 1
	public function getTypeTitle($type){
81 1
		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 3
	public function slugify($str) {
91 3
	  $str = mb_strtolower(trim($str));
92
		
93 3
		$str = str_replace(array("�","�","�"), array("a","a","o"), utf8_decode(utf8_encode($str)));
94
		
95 3
	  $str = preg_replace('/[^a-z0-9-_()]/', '_', $str);
96 3
	  $str = trim(preg_replace('/_+/', '_', $str), '_');
97
    
98 3
	  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 2
  public function checkIfAvailable($datetime){
108 2
    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
	 * @Returns 	Boolean      			Validate result
116
	 */
117 2
	public function checkType($type){
118 2
		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 2
    if(isset($datetime) && !empty($datetime)){
129 2
      $format = 'Y-m-d H:i:s';
130 2
      $d = \DateTime::createFromFormat($format, $datetime);
131 2
      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 2
  public function minimumLength($value){
143 2
    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 2
  public function validateSlug($url){
153 2
    return ($this->slugify($url) == $url);
154
  }
155
  
156
  /**
157
	 * Check so the select filters exist.
158
	 *
159
	 * @Param     Array 	  $filters  Array with select filters
160
	 * @Return    Boolean   $result   Return the result of test
161
	 */
162 2
	public function checkFilter($filter = null){
163 2
	  if(!empty($filter)){
164
      // For each filter, check if the filter exist
165 2
      foreach($this->filters as $val){
166 2
        if($val == $filter)
167 2
          return true;
168 2
      }
169 2
      return false;
170
    }
171 2
	  return true;
172
	}
173
}