1: <?php
2: /**
3: * Copyright 2000-2012 Horde LLC (http://www.horde.org/)
4: *
5: * See the enclosed file COPYING for license information (GPL). If you
6: * did not receive this file, see http://www.horde.org/licenses/gpl.
7: *
8: * @author Chuck Hagenbuch <chuck@horde.org>
9: * @package Chora
10: */
11: class Chora_Readme_Collection
12: {
13: protected $_readmes;
14:
15: const CHOOSE_A = -1;
16: const CHOOSE_B = 1;
17: const EQUAL = 0;
18:
19: public function __construct(array $readmes)
20: {
21: $this->_readmes = $readmes;
22: }
23:
24: public function chooseReadme()
25: {
26: $count = count($this->_readmes);
27: if ($count == 0) {
28: throw new Chora_Exception('No README files to choose from');
29: }
30:
31: if ($count > 1) {
32: usort($this->_readmes, array($this, 'compareReadmes'));
33: }
34:
35: return $this->_readmes[0];
36: }
37:
38: public function compareReadmes($a, $b)
39: {
40: if ($this->_isHtmlReadme($a)) { return self::CHOOSE_A; }
41: if ($this->_isHtmlReadme($b)) { return self::CHOOSE_B; }
42:
43: $a_len = Horde_String::length($a->getFileName());
44: $b_len = Horde_String::length($b->getFileName());
45: if ($a_len < $b_len) {
46: return self::CHOOSE_A;
47: } elseif ($b_len < $a_len) {
48: return self::CHOOSE_B;
49: } else {
50: return strcasecmp($a->getFileName(), $b->getFileName());
51: }
52: }
53:
54: protected function _isHtmlReadme(Horde_Vcs_File $readme)
55: {
56: $file = Horde_String::lower($readme->getFileName());
57: return ($file == 'readme.html' || $file == 'readme.htm' || ($file == 'readme' && $readme->mimeType == 'text/html'));
58: }
59: }
60: