PageRenderTime 13ms CodeModel.GetById 8ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/contact-form-7/includes/pipe.php

https://bitbucket.org/devbctph/futura_wp
PHP | 78 lines | 58 code | 20 blank | 0 comment | 5 complexity | 454e28daaabb2c6840dbad4283b7f332 MD5 | raw file
 1<?php
 2
 3class WPCF7_Pipe {
 4
 5	public $before = '';
 6	public $after = '';
 7
 8	public function __construct( $text ) {
 9		$text = (string) $text;
10
11		$pipe_pos = strpos( $text, '|' );
12
13		if ( false === $pipe_pos ) {
14			$this->before = $this->after = trim( $text );
15		} else {
16			$this->before = trim( substr( $text, 0, $pipe_pos ) );
17			$this->after = trim( substr( $text, $pipe_pos + 1 ) );
18		}
19	}
20}
21
22class WPCF7_Pipes {
23
24	private $pipes = array();
25
26	public function __construct( array $texts ) {
27		foreach ( $texts as $text ) {
28			$this->add_pipe( $text );
29		}
30	}
31
32	private function add_pipe( $text ) {
33		$pipe = new WPCF7_Pipe( $text );
34		$this->pipes[] = $pipe;
35	}
36
37	public function do_pipe( $before ) {
38		foreach ( $this->pipes as $pipe ) {
39			if ( $pipe->before == $before ) {
40				return $pipe->after;
41			}
42		}
43
44		return $before;
45	}
46
47	public function collect_befores() {
48		$befores = array();
49
50		foreach ( $this->pipes as $pipe ) {
51			$befores[] = $pipe->before;
52		}
53
54		return $befores;
55	}
56
57	public function collect_afters() {
58		$afters = array();
59
60		foreach ( $this->pipes as $pipe ) {
61			$afters[] = $pipe->after;
62		}
63
64		return $afters;
65	}
66
67	public function zero() {
68		return empty( $this->pipes );
69	}
70
71	public function random_pipe() {
72		if ( $this->zero() ) {
73			return null;
74		}
75
76		return $this->pipes[array_rand( $this->pipes )];
77	}
78}