Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

eznamepatternresolver.php
Go to the documentation of this file.
1 <?php
38 class eZNamePatternResolver
39 {
40 const FIELD_NAME_MAX_SIZE = 255;
41
47 private $groupLookupTable;
48
54 private $origNamePattern;
55
62 private $namePattern;
63
69 private $contentObject;
70
76 private $version;
77
83 private $translation;
84
90 private $attributeArray;
91
92
98 private $metaString = 'EZMETAGROUP_';
99
110 public function __construct( $namePattern, eZContentObject $contentObject, $contentVersion = false, $contentTranslation = false )
111 {
112 $this->origNamePattern = $namePattern;
113 $this->contentObject = $contentObject;
114 $this->version = $contentVersion;
115 $this->translation = $contentTranslation;
116
117 $this->namePattern = $this->filterNamePattern( $namePattern );
118 }
119
127 public function resolveNamePattern( $limit = 0, $sequence = '' )
128 {
129 // Fetch attributes for present identifiers
130 $this->fetchContentAttributes();
131
132 // Replace tokens with real values
133 $objectName = $this->translatePattern();
134
135 $db = eZDB::instance();
136
137 $limit = $limit ?: self::FIELD_NAME_MAX_SIZE;
138
139 if ( $db->countStringSize( $objectName ) <= $limit )
140 {
141 return $objectName;
142 }
143
144 return preg_replace(
145 "/[\pZ\pC]+$/u",
146 '',
147 $db->truncateString( $objectName, $limit, 'name', $sequence )
148 ). $sequence;
149 }
150
158 private function fetchContentAttributes()
159 {
160 $returnAttributeArray = array();
161
162 $identifierArray = $this->getIdentifiers( $this->origNamePattern );
163
164 $attributes = $this->contentObject->fetchAttributesByIdentifier( $identifierArray, $this->version, array( $this->translation ) );
165
166 if ( is_array( $attributes ) )
167 {
168 foreach ( $attributes as $attribute )
169 {
170 $identifier = $attribute->contentClassAttributeIdentifier();
171 $returnAttributeArray[$identifier] = $attribute->title();
172 }
173 }
174 else
175 {
176 $returnAttributeArray = array();
177 }
178 $this->attributeArray = $returnAttributeArray;
179 }
180
181
187 private function translatePattern()
188 {
189 $tokenArray = $this->extractTokens( $this->namePattern );
190 $objectName = $this->namePattern;
191
192 foreach( $tokenArray as $token )
193 {
194 $string = $this->resolveToken( $token );
195 $objectName = str_replace( $token, $string, $objectName );
196 }
197
198 return $objectName;
199 }
200
212 private function extractTokens( $namePattern )
213 {
214 $foundTokens = preg_match_all( "|<([^>]+)>|U", $namePattern,
215 $tokenArray );
216
217 if ( $foundTokens === false || empty( $tokenArray[0] ) )
218 return [];
219
220 return $tokenArray[0];
221 }
222
231 private function resolveToken( $token )
232 {
233 $replaceString = "";
234 $tokenParts = $this->tokenParts( $token );
235
236 foreach ( $tokenParts as $tokenPart )
237 {
238 if ( $this->isTokenGroup( $tokenPart ) )
239 {
240 $groupTokenArray = $this->extractTokens( $this->groupLookupTable[$tokenPart] );
241 $replaceString = $this->groupLookupTable[$tokenPart];
242
243 foreach ( $groupTokenArray as $groupToken )
244 {
245 $replaceString = str_replace( $groupToken, $this->resolveToken( $groupToken ), $replaceString );
246 }
247 // We want to stop after the first matching token part / identifier is found
248 // <id1|id2> if id1 has a value, id2 will not be used.
249 // In this case id1 or id1 is a token group.
250 break;
251 }
252 else
253 {
254 if ( isset( $this->attributeArray[$tokenPart] ) && $this->attributeArray[$tokenPart] !== '' && $this->attributeArray[$tokenPart] !== false )
255 {
256 $replaceString = $this->attributeArray[$tokenPart];
257 // We want to stop after the first matching token part / identifier is found
258 // <id1|id2> if id1 has a value, id2 will not be used.
259 break;
260 }
261 }
262 }
263 return $replaceString;
264 }
265
266
273 private function isTokenGroup( $identifier )
274 {
275 if ( strpos( $identifier, $this->metaString ) === false )
276 {
277 return false;
278 }
279 return true;
280 }
281
295 private function tokenParts( $token )
296 {
297 $tokenParts = preg_split( '#\W#', $token, -1, PREG_SPLIT_NO_EMPTY );
298 return $tokenParts;
299 }
300
312 private function filterNamePattern( $namePattern )
313 {
314 $retNamePattern = "";
315 $foundGroups = preg_match_all( "/[<|\|](\‍(.+\‍))[\||>]/U", $namePattern, $groupArray );
316
317 if ( $foundGroups )
318 {
319 $i = 0;
320 foreach ( $groupArray[1] as $group )
321 {
322 // Create meta-token for group
323 $metaToken = $this->metaString . $i;
324
325 // Insert the group with its placeholder token
326 $retNamePattern = str_replace( $group, $metaToken, $namePattern );
327
328 // Remove the pattern "(" ")" from the tokens
329 $group = str_replace( array( '(', ')' ), '', $group );
330
331 $this->groupLookupTable[$metaToken] = $group;
332 ++$i;
333 }
334 return $retNamePattern;
335 }
336 return $namePattern;
337
338 }
339
346 private function getIdentifiers( $patternString )
347 {
348 $allTokens = '#<(.*)>#U';
349 $identifiers = '#\W#';
350
351 $tmpArray = array();
352 if ( preg_match_all( $allTokens, $patternString, $matches ) === false )
353 return [];
354
355 foreach ( $matches[1] as $match )
356 {
357 $tmpArray[] = preg_split( $identifiers, $match, -1, PREG_SPLIT_NO_EMPTY );
358 }
359
360 $retArray = array();
361 foreach ( $tmpArray as $matchGroup )
362 {
363 if ( is_array( $matchGroup ) )
364 {
365 foreach ( $matchGroup as $item )
366 {
367 $retArray[] = $item;
368 }
369 }
370 else
371 {
372 $retArray[] = $matchGroup;
373 }
374 }
375 return $retArray;
376 }
377 }
378 ?>
eZContentObject
Definition: ezcontentobject.php:17
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZNamePatternResolver
Definition: eznamepatternresolver.php:39
eZNamePatternResolver\$translation
$translation
Definition: eznamepatternresolver.php:83
eZNamePatternResolver\$metaString
$metaString
Definition: eznamepatternresolver.php:98
eZNamePatternResolver\$groupLookupTable
$groupLookupTable
Definition: eznamepatternresolver.php:47
eZNamePatternResolver\$namePattern
$namePattern
Definition: eznamepatternresolver.php:62
eZNamePatternResolver\fetchContentAttributes
fetchContentAttributes()
Definition: eznamepatternresolver.php:158
eZNamePatternResolver\translatePattern
translatePattern()
Definition: eznamepatternresolver.php:187
eZNamePatternResolver\$origNamePattern
$origNamePattern
Definition: eznamepatternresolver.php:54
eZNamePatternResolver\filterNamePattern
filterNamePattern( $namePattern)
Definition: eznamepatternresolver.php:312
eZNamePatternResolver\resolveNamePattern
resolveNamePattern( $limit=0, $sequence='')
Definition: eznamepatternresolver.php:127
eZNamePatternResolver\$contentObject
$contentObject
Definition: eznamepatternresolver.php:69
eZNamePatternResolver\$attributeArray
$attributeArray
Definition: eznamepatternresolver.php:90
eZNamePatternResolver\__construct
__construct( $namePattern, eZContentObject $contentObject, $contentVersion=false, $contentTranslation=false)
Definition: eznamepatternresolver.php:110
eZNamePatternResolver\FIELD_NAME_MAX_SIZE
const FIELD_NAME_MAX_SIZE
Definition: eznamepatternresolver.php:40
eZNamePatternResolver\$version
$version
Definition: eznamepatternresolver.php:76
eZNamePatternResolver\resolveToken
resolveToken( $token)
Definition: eznamepatternresolver.php:231
eZNamePatternResolver\extractTokens
extractTokens( $namePattern)
Definition: eznamepatternresolver.php:212
eZNamePatternResolver\tokenParts
tokenParts( $token)
Definition: eznamepatternresolver.php:295
eZNamePatternResolver\getIdentifiers
getIdentifiers( $patternString)
Definition: eznamepatternresolver.php:346
eZNamePatternResolver\isTokenGroup
isTokenGroup( $identifier)
Definition: eznamepatternresolver.php:273
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.